In the process of drawing, sometimes we may need to add some lines, so that the visualization of the graph becomes better, such as some trend lines and so on.
Let's take a look at some of the ways the lines are added.
Geom_abline (mapping = NULL, data = NULL, ..., slope, intercept, na.rm = FALSE, show.legend = NA) geom_hline (mapping = NULL, data = null, ..., yintercept, na.rm = FALSE, show.legend = NA) geom_vline (mapping = NULL, data = NULL, ..., xintercept, NA . RM = FALSE, show.legend = NA) Three functions are, geom_abline add a slash, geom_hline add a horizontal line, geom_vline add vertical lines parameter slope means slope intercept mean intercept parameter Y Intercept indicates that the Y-intercept or line position parameter xintercept represents the x-axis intercept or the position of the line below to see specific examples
Library (Ggplot2) P<-ggplot (Economics,aes (Pop,psavert)) P+geom_point ()
P+geom_point () +geom_abline (intercept=2.709e+01,slope=-7.447e-05)
Linear fitting lines can be added by slope and intercept, and the values of the two parameters can be obtained with the linear function lm ().
Of course the fitting line can also be used Geom_smooth (), relative to this method more direct, P+geom_point () +geom_smooth (method= "LM", Se=false) can draw the same.
P+geom_point () +geom_hline (yintercept = C (5,10,15))
P+geom_point () +geom_vline (xintercept = 245000)
The great thing about these functions is that you can draw the lines you want anywhere on the line.
Ggplot2 Geom Related Settings--Add lines