Ggplot2 is a set of graphic grammar proposed in Grammar of Graphics, which abstracts graphic elements into elements that can be freely combined, similar to layer stacking in Photoshop. ggplot2 stacks the specified elements/mapping relationship layer by layer, and finally forms a graphic. For a more in-depth study of ggplot2, please refer to ggplot2: Data Analysis and Graphic Arts.
catalogue
Introduction: Basic elements of ggplot2.
1. Data and mapping
2. Geometric objects
3. Proportion): filling, color and shape.
4. Statistical transformation
5. Coordinate system (NTE coordinate system)
6. aspects
7. Theme
Attachment: ggplot2 function fast lookup table
Introduction: Basic elements of ggplot2.
+and%+%
Data and drawing: The data of ggplot 2 must be data frames.
Geometric objects: Geom represents the elements you actually see in the diagram, such as points, lines, polygons, etc.
Statistics: Statistical transformation (stat) is a summary of data.
Scale: The function of scale is to map the values of data to the graphic space, for example, to represent different values by color, size or shape.
Coordinate system: Coordinate system describes how the data is mapped to the plane where the drawing is located, and also provides coordinate axes and grid lines needed to view the drawing.
Layer: A layer consists of four parts: data and graphic attribute mapping; Statistical transformation; Geometric objects; A way of position adjustment.
Facet: Facet describes how to decompose data into subsets, and how to draw and jointly display subsets.
Among them, each element is bonded in the form of a layer with "+"(which can be simply understood as the symbol of element/layer superposition); In addition, in ggplot2, the data set must be in data.frame format, and the existing data set can be adjusted with%+%symbol (ggplot2 guidance document clearly stipulates that "to overwrite data,%+%must be used", that is, the data must be overwritten by%+%). Take mpg data set as an example.
p 1 & lt; -base+geom _ smooth ()+labs (title = "figure 1") # as shown in figure1?
# Adjust the data in the mapping relationship with%+%
base & lt- ggplot(mpg,aes(displ,hwy)) + geom_point()
# To overwrite data, you must use%+%?
# In other words, overwriting the original data must pass%+%
p2 & lt-base%+%subset (mpg,fl = = " p ")+labs(title = " Figure 2 ")# Figure 2?
# The second way to adjust the data list
# Alternatively, you can add multiple components to a list.
# This is useful for returning from a function.
p3 & lt-base+list (subset (mpg,fl = = "p "),geom _ smooth(),labs (title = "Figure 3")) # Figure 3?
# # # # # # More than one page # # # # #
# Library (Grid)
grid.newpage()? # # New page
Push viewport (viewport (layout = grid.layout (2,2)) # divides the page into 2*2 matrices.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}
print(p 1,vp = vplayout( 1, 1))? #( 1, 1) Location Map 1
print(p2,vp = vplayout( 1,2))? #( 1, 2) Location Figure 2
print(p3,vp = vplayout(2, 1))? #(2 1) Location Figure 3
1. data and mapping as mentioned above, in ggplot2, the data set must be in data.frame format, and the existing data set can be adjusted by the%+%symbol.
Mapping is to associate discrete or continuous data in a variable with different parameters in a graphic attribute. Setting can unify all data in this variable into a graphic attribute. The aes () function is a mapping function in ggplot2, and the so-called mapping is a corresponding relationship in the process of associating the data in the dataset with the corresponding graphic attributes (note line 10). It can be found that in p2, horizontal coordinates and vertical coordinates are designated as wt and hp by aes (), respectively.
& gtp 1 & lt; -ggplot (data = mtcars
& gt summary (p 1)
Data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x 1 1]
Facet: & ltggproto object: class FacetNull, facet, gg>
..... # Part of this part is omitted?
& gtp2 & lt- ggplot (data = mtcars, mapping = aes(x = wt, y = hp))
& gt summary (page 2)
Data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x 1 1]
Mapping:? X = ~wt, y = ~hp Facet: & ltggproto object: class FacetNull, facet, gg>.
..................................................................................................................................................................................
In addition, in ggplot2, the ggplot () function declares the global data and mapping relationship. If there is no reset data and mapping relationship in the subsequent geometric objects, the geometric objects will follow the data and mapping relationship declared in ggplot (); Of course, the geometric object can reset the relationship between data and mapping, and act on this geometric object (compare Figure 4 and Figure 7), but it does not affect the initial graphics layer (compare Figure 4 and Figure 6, Figure 6 redefines the Y variable as carb in geometry, but the ordinate is still wt).
# library (ggolot2)
P & lt-ggplot (mtcars, AES (x = mpg, y = wt)) # sets the default mapping relationship.
P4<-p+geom _ point ()+labs (title = "figure4") # Figure 4 draws a scatter plot using the default mapping relationship.
P5<-p+geom _ point (AES (shape = factor (carb))+Labs (title = "Figure5") # Figure5 Add the mapping relationship of shapes in the layer?
P6<-p+geom _ point (AES (y = carb))+Labs (title = "Figure 6") # Figure 6 Modify the default mapping relationship of Y, pay attention to the name of the Y axis in the figure or use the default wt?
df & lt-mt cars[which(mt cars $ am = = 1),]
p7 & lt-p+geom _ point (data = df,AES (x = mpg,y = wt))+labs (title = "Figure 7 ")。
# Redefine the relationship between data and mapping in point geometry objects?
# # # # # # More than one page # # # # #
# Library (Grid)
grid.newpage()? # # New page
Push viewport (viewport (layout = grid.layout (2,2)) # divides the page into 2*2 matrices.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}?
print(p4,vp = vplayout( 1, 1))? #( 1, 1) Location Figure 4
print(p5,vp = vplayout( 1,2))? #( 1, 2) Location Figure 5
print(p6,vp = vplayout(2, 1))? #(2 1) Location Figure 6
print(p7,vp = vplayout(2,2))? # (2,2) Location Figure 7
2. Geometric objects In the above example, the data mapping relationship is set by the ggplot () function. Use geom_point () to add a geometric layer, tell ggplot to draw a point map, and map the layer properties to scatter points.
In addition to geom_point, ggplot2 also provides a variety of geometric object mappings, such as geom_histogram, geom_bar histogram, geom_boxplot box diagram and so on. Different geometric objects need different attributes, which can also be provided when mapping geometric objects.
& gt library (ggplot2)
& gtls ("package: ggplot2", pattern = "geom _.+")
[ 1]" geom _ abline " " geom _ area " " geom _ bar " " geom _ bin2d " " geom _ blank "
[6] "Geometry _ rectangle" "Geometry _ column" "Geometry _ outline" "Geometry _ count" "Geometry _ vertical and horizontal"
[ 1 1]" geom _ curve " " geom _ density " " geom _ density _ 2d " " geom _ dot plot "
[ 16]" geom _ error bar " " geom _ error barh " " geom _ freq poly " " geom _ hex " " geom _ histogram "
[2 1]" geom _ hline " " geom _ jitter " " geom _ label " " geom _ line " " geom _ line range "
[26] "Geometric Diagram", "Geometric Path", "Geometric Point", "Geometric Point Range" and "Geometric Polygon"
[3 1] "geometry _qq" "geometry _qq_line" "geometry _ quantile" "geometry _ grating" "geometry _ rectangle"
[36]" geom _ ribbon " " geom _ rug " " geom _ segment " " geom _ SF " " geom _ SF _ label "
[4 1]" geom _ SF _ text " " geom _ smooth " " geom _ spoke " " geom _ step " " geom _ text "
[46] "Geometry _ Tiles", "Geometry _ Violin" and "Geometry _ Violin"
# library (ggplot2)
p & lt- ggplot(mtcars,aes(x = mpg,y = wt))
P8<-p+geom _ point ()+labs (title = "Figure 8") # Figure 8 Scatter chart?
P<- ggplot(mtcars, aes(x = factor (carbohydrate), y = weight))
P9<-p+geom _ bar (stat =' identity')+Labs (title = "Figure9") # Figure9 Bar chart?
# # # # # # More than one page # # # # #
# Library (Grid)
grid.newpage()? # # New page
Pushviewport (viewport (layout = grid.layout (1,2)) # divides the page into 2*2 matrices.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}
print(p8,vp = vplayout( 1, 1))? #( 1, 1) Location Figure 8
print(p9,vp = vplayout( 1,2))? #( 1, 2) Location Figure 9
3. Proportion): filling, color and shape. After mapping graphic attributes, you can control the display mode of these attributes, such as color attributes and shape attributes. Comparing figure 10 with figure 1 1, we can find that when the variables corresponding to colors are factors, the legends in figure 10 display different colors in groups. But if the variable corresponding to factor is a numerical value, ggplot recognizes it as a continuous variable, and the numerical value determines the color depth; Comparing 12 with figure 13, the effect is the same whether the color is defined in ggplot function or in geometric object.
# library (ggplot2)
p & lt- ggplot(mtcars,aes(x = mpg,y = wt))
p 10 & lt; -p+geom _ point (AES (color = factor (gear))+labs (title = "graph 10") # graph 10.
p 1 1 & lt; -p+geom _ point(AES(color = gear))+labs(title = " Figure 1 1 ")# Figure 1 1?
p & lt- ggplot(mtcars,aes(x = mpg,y = wt,color = factor(gear)))
p 12 & lt; -p+geom _ point(AES(shape = factor(cyl))+labs(title = " Figure 12 ")# Figure 12?
p & lt- ggplot(mtcars,aes(x = mpg,y = wt))
p 13 & lt; -p+geom _ point(AES(color = factor(gear),shape = factor(cyl))+labs(title = " figure 13 ")# figure 13?
# # # # # # More than one page # # # # #
Library (grid)
grid.newpage()? # # New page
push viewport(viewport(layout = grid。 Layout (2 2,2)) # Divides the page into a 2*2 matrix.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}
print(p 10,vp = vplayout( 1, 1))? Draw the position of #( 1, 1) 10.
print(p 1 1,vp = vplayout( 1,2))? #( 1, 2) Location Map 1 1
print(p 12,vp = vplayout(2, 1))? #(2 1) Location Map 12
print(p 13,vp = vplayout(2,2))? # (2,2) Location Map 13
4. Statistical transformation (Stat) Statistical transformation is to calculate the original data and then display it on the chart, such as adding a regression line to the scatter chart.
ggplot(mtcars,aes(x = mpg,y = wt))+geom _ point()+scale _ y _ log 10()+stat _ smooth(method = " auto ",formula = y ~ x)
Parameters provided by aes are provided through ggplot, not to geom_point, because parameters in ggplot are equivalent to global variables, and geom_point () and stat_smooth () both know the mapping of X and Y. If they are only provided to geom_point (), they are equivalent to local variables. Ggplot2 provides a variety of statistical transformation methods:
& gt library (ggplot2)
& gtls("package:ggplot2 ",pattern="stat_。 +")
[ 1]" stat _ bin " " stat _ bin _ 2d " " stat _ bin _ hex " " stat _ bin2d " " stat _ binhex "
[6] "Statistical boxplot", "Statistical Profile", "Statistical Count", "Statistical Density" and "Statistical Density _ 2D"
[ 1 1]" stat _ density 2d " " stat _ ecdf " " stat _ ellipse " " stat _ function " " stat _ identity "
[ 16]" stat _ QQ " " stat _ QQ _ line " " stat _ quantile " " stat _ SF " " stat _ SF _ coordinates "
[2 1]" stat _ smooth " " stat _ spoke " " stat _ sum " " stat _ summary " " stat _ summary _ 2d "
[26]" stat _ summary _ bin " " stat _ summary _ hex " " stat _ summary 2d " " stat _ unique " " stat _ y density "
[3 1] "Update _ Statistics _ Default"
5. Coordinate NTE coordinate system controls coordinate axis transformation, such as XY axis flip, Cartesian coordinate and polar coordinate transformation.
# Set basic mapping relationship
p & lt- ggplot(mtcars)
p 14 & lt; -p+geom _ bar (AES (x = factor (carbohydrate))+coord _ flip ()+labs (title = "figure14")? # Figure 14 Original?
# Axis flip is realized by coord_flip ().
p 15 & lt; -p+geom _ bar(AES(x = factor(carb))+coord _ flip()+labs(title = " Figure 15 ")? # Figure 15?
# Converting to polar coordinates can be realized by coord_polar (): windrose.
p 16 & lt; -p+geom _ bar (AES (x = factor (1), fill = factor (gear))+coord _ polar ()+labs (title = "Figure16")? # Figure 16?
# Converting to polar coordinates can be realized by coord_polar (): windrose.
p 17 & lt; -p+geom _ bar (AES (x = factor (carbohydrate), fill = factor (gear))+coord _ polar ()+labs (title = "figure17")? # Figure 17?
# # # # # # More than one page # # # # #
# Library (Grid)
grid.newpage()? # # New page
push viewport(viewport(layout = grid。 Layout (2 2,2)) # Divides the page into a 2*2 matrix.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}?
print(p 14,vp = vplayout( 1, 1))? Draw the position of #( 1, 1) 14.
print(p 15,vp = vplayout( 1,2))? #( 1, 2) Location Map 15
print(p 16,vp = vplayout(2, 1))? #(2 1) Location Map 16
print(p 17,vp = vplayout(2,2))? # (2,2) Location Map 17
6. Faceted Faces allow us to group data according to given conditions and then draw pictures separately.
# Facet _ Mesh
mt & lt- ggplot(mtcars,aes(mpg,wt,colour = factor(cyl))+? Geometric point ()
mt + facet_grid(。 ~ cyl, scales = "freedom")
#facet_wrap
ggplot(mpg,aes(displ,hwy)) +? geom_point() +? facet_wrap(~class,scales = "free ")
7. Theme
p 1 & lt; - ggplot(mtcars,aes(wt,mpg)) +? geom_point() +? Laboratory (title = "fuel economy line when weight increases")+laboratory (title = "Figure 20")? # Figure 20
p 17 & lt; -p 1+theme(plot . title = element _ text(size = rel(2)))? +laboratory (title= "Figure 17 ")? # Figure 17
p 18 & lt; -p 1+theme(plot . background = element _ rect(fill = " green "))? +laboratory (title= "Figure 18 ")? # Figure 18
p 19 & lt; -p 1+ theme (panel. background = element _ rect(fill = " white ",colour = " grey 50 ")+labs(title = " figure 19 ")? # Figure 19
# # # # # # More than one page # # # # #
# Library (Grid)
grid.newpage()? # # New page
push viewport(viewport(layout = grid。 Layout (2 2,2)) # Divides the page into a 2*2 matrix.
vplayout & lt- function(x,y){ viewport(layout.pos.row = x,layout.pos.col = y)}?
print(p 1,vp = vplayout( 1, 1))? #( 1, 1) Location Map 20
print(p 17,vp = vplayout( 1,2))? #( 1, 2) Location Map 17
print(p 18,vp = vplayout(2, 1))? #(2 1) Location Map 18
print(p 19,vp = vplayout(2,2))? # (2,2) Location Map 19