I am trying to insert a text box in my ggplot to show regression equation and p-value but it is giving me a weird output. The graph only shows on one side. please look at the image.
I also tried to use:
scale_x_continuous(name = "Year", limits=c(1890, 2020)
but it does not work.
I adopted the following code and added the text in "geom_label" instead of in the title.
https://sejohnston.com/2012/08/09/a-quick-and-easy-function-to-plot-lm-results-in-r/
if anyone can find my error. i will be greatful.
Here is the function that I tried to use:
ggplotRegression3 <- function (fit) {
require(ggplot2)
ggplot(fit$model,
aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
geom_line() +
geom_label(aes(3, 40, hjust = 1,
"Adj R2 = ",
signif(summary(fit)$adj.r.squared, 3),"\n",
"Intercept =",signif(fit$coef[[1]],3 ),"\n",
" Slope =",signif(fit$coef[[2]], 3),"\n",
" P-value =",signif(summary(fit)$coef[2,4], 3))) +
stat_smooth(method = "lm", col = "red") +
labs(title = "Average annual") +
scale_x_continuous(name = "Year", limits=c(1890, 2020))
}