In geom_smooth:
method – the smoothing function (e.g. lm, glm, gam, loess, rlm)
se – whether to display the confidence interval (default = TRUE)
level – the level of confidence interval (default = 0.95)
fullrange – whether to extend the fit span to the full range of the plot (default = FALSE)
For more details, see ggplot2:Add a smoother
library(ggplot2)
p1 <- ggplot(iris, aes(Petal.Width, Petal.Length, colour = Species)) +
geom_point(size = 3) + geom_smooth(method='lm') +
labs(x = "Petal Width", y = "Petal Length") + theme_bw() +
theme(legend.position = "top", legend.text = element_text(size = 12),
legend.title = element_text(size = 14))
#print(p1)
Do not show the confidence intervals:
p2 <- ggplot(iris, aes(Petal.Width, Petal.Length, colour = Species)) +
geom_point(size = 3) + geom_smooth(method='lm', se = FALSE) +
labs(x = "Petal Width", y = "Petal Length") + theme_bw() +
theme(legend.position = "top", legend.text = element_text(size = 12),
legend.title = element_text(size = 14))
#print(p2)
Extend the fit span to full range:
p3 <- ggplot(iris, aes(Petal.Width, Petal.Length, colour = Species)) +
geom_point(size = 3) + geom_smooth(method='lm', fullrange = TRUE) +
labs(x = "Petal Width", y = "Petal Length") + theme_bw() +
theme(legend.position = "top", legend.text = element_text(size = 12),
legend.title = element_text(size = 14))
#print(p3)
The plot above shows that the standard error gets larger as the X variable moves farther away from its mean.