Building images with layers _ggplot2

Source: Internet
Author: User
Tags ggplot


The limitation of Qplot () is that it can only use one dataset and a set of graphical property mappings, and the solution to this problem is to use layers. Each layer can have its own data set graphical property map, and additional data elements can be added to the layer through layers.
A layer consists mainly of 5 parts: data, a set of graphical attribute mappings, geometric objects, statistical transformations, and position adjustments. 1. Create drawing objects



When calling Qplot (), it actually does a lot of behind-the-scenes work: Create a graphic object, add graphics, and show the results.
To create a drawing object manually, you use the Ggplot () function. The function has two main parameters: Data and graphical property mappings. These two parameters are set as the default parameters for the drawing, and the default values are modified only if new parameters are set in the newly added layer.
Parameter data specifies the default dataset (must be a data box) used by the drawing;
The parameter mapping method is very similar to the Qplot (), simply by placing the graphic properties and variable names inside the parentheses of the function Aes ().


P<-ggplot (Diamonds,aes (carat,price,colour=cut))


This drawing object is 2 that cannot be displayed until the layer is added. Layer



The default parameters for layers are: layer (geom,geom_params,stat,stat_params,data,mapping,position)
The simplest layer is to set only one geometric object, example P<-p+layer (geom= "point")
The more complex can be set to:


P<-ggplot (Diamonds,aes (X=carat))
P<-p+layer (
  geom= "Bar",
  geom_params=list (fill= "Steelblue"),
  stat= "Bin",
  stat_params=list (binwidth=2)
)
p


You can see that the above code is cumbersome and simplifies the code above. Because each geometric object corresponds to a default statistic change and positional parameter, each statistic change corresponds to a default geometric object parameter, so just set stat or Geom parameters, the above code can be written as: Geom_histogram (binwidth=2,fill= "Steelblue") 3. Data 4. Data graphic mapping 5. Collection Object 6. Statistic Transformation 7. Position Adjustment 8. Consolidate


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.