Formidable Ggplot Lm Line
Better place the lines in one single.
Ggplot lm line. The following code shows how to add a linear trend line to a scatterplot in ggplot2. I dont have your data but heres an example using the mtcars dataset. The theoretical structure behind how a graph is created is similar to how we might form a sentence.
The ggplot2 package is based on the Grammar of Graphics by Leland Wilkinson. To show different lines in different facets use aesthetics p. Aids the eye in seeing patterns in the presence of overplotting.
GGPlot2 Essentials for Great Data Visualization in R. Ggplot2 provides the geom_smooth function that allows to add the linear trend and the confidence interval around it if needed option seTRUE. The easiest option is to use geom_smooth and let ggplot2 fit the model for you.
Geom_hline for horizontal lines geom_abline for regression lines geom_vline for vertical lines geom_segment to add segments. They both use the same arguments. Geom_smooth and stat_smooth are effectively aliases.
To add a regression line on a scatter plot the function geom_smooth is used in combination with the argument method lm. Use geom_point function to plot the dataset in a scatter plot. The method argument allows to apply different smoothing method like glm loess and more.
What you need to do is use the fullrange parameter of stat_smooth and expand the x-axis to include the range you want to predict over. Use stat_smooth if you want to display the results with a non-standard geom. Add Linear Trend Line Specify Confidence Region.