Getting started with ggplot2: Layer Control and Histogram

Source: Internet
Author: User
Tags ggplot

As described above, ggplot2 uses layers to gradually add and combine various graphic elements to form the final result. The first layer must be the raw data layer, where the data parameter controls the data source. Note that the data format can only be the data frame format. The AES parameter controls which variables are mapped and the ing method. AES is short for aesthetic.

Next we will plot a histogram as an example. The dataset still uses MPG to draw a histogram for the hwy variable. First, the extended package is loaded, and then the first layer is created using the ggplot function. The hwy data is mapped to the X axis. The second layer is added using the plus sign (+), that is, the histogram Object layer. P is regarded as a layer object. You can use the summary function to obtain more information about it. The print (p) command can be used for plotting.

 1 library(ggplot2) 2 p <- ggplot(data = mpg,aes(x = hwy)) 3 p <- p + geom_histogram() 4 summary(p) 5 data: manufacturer, model, displ, year, cyl, trans, 6   drv, cty, hwy, fl, class [234x11] 7 mapping:  x = hwy 8 faceting: facet_grid(. ~ ., FALSE) 9 -----------------------------------10 geom_histogram:11 stat_bin:12 position_stack: (width = NULL, height = NULL)

 

The above information tells us that the P object contains two layers. The first layer of Data describes the variables and ing methods, the second layer is the histogram object (geom_histogram), and Geom indicates the geometric object, it is an important layer control object in ggplot because it is responsible for the type of graphic rendering. Geom_histogram is a type of graphic rendering. For other types, see the official website.

Each Geom object requires data input, which can be automatically read from the first layer or set directly in the AES parameter. In addition, each Geom is also matched with a certain statistical Transformation (STAT) by default. The default statistical transformation of geom_histogram is stat_bin. It is responsible for grouping and counting data.

Next we will try two more complex histograms. First, we will divide the data into two groups based on the year variable, draw the histogram with different colors, and use the frequency instead of the count to portray the Y axis, and add the density curve.

p <- ggplot(mpg,aes(hwy))p + geom_histogram(position = ‘identity‘,    alpha=0.5,    aes(y = ..density..,    fill = factor(year))) +    stat_density(geom = ‘line‘,    position = ‘identity‘,    aes(colour = factor(year)))

 

If you want to separate the two histograms, you can also use the facet_grid parameter, as shown in the result.

 

Reproduced in: http://r-ke.info/2012/02/06/ggplot2-intro-2.html

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.