Ggplot2 Geom Related Settings--Add lines

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.