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