## Reference book: ggplot2: data analysis and graphic art

A statistical graph is a mapping from data to the graphical attributes (aesthetic attributes, abbreviated as aes, including color, shape, size, etc.) of geom etric objects. In addition, the graph may also contain statistical transformation (abbreviated to stats) of data, which is finally drawn in a specific coordinate system (abbreviated to coordinate), while facet (which refers to dividing the drawing window into several sub windows) can be used to generate graphs with different subsets of data. All in all, a statistical graph is composed of these independent graphic components.

### Getting started with qplot

qplot() -- quick plot is a quick mapping. Qplot can be seen as the middle transition from plot to ggplot2. Qplot is very similar to plot. It has both the characteristics of plot and the concept of ggplot. Through qplot, you can quickly get started and learn some terms of ggplot2, such as:

- The graph attribute names (such as colour, shape and size) in ggplot2 are more intuitive and easier to remember than those in the basic drawing system (such as col, pch and cex).
- . in the basic drawing system, you can add more elements to the existing drawings through the points(), lines() and text() functions. In ggplot2, additional layers need to be added to the current drawing.

library("ggplot2") diamonds#Diamond information data set names(diamonds) #"carat" "cut" "color" "clarity" "depth" "table" "price" "x" "y" "z" #carat weight, cut, color, clarity, price and five physical indexes depth, table, x, y and z qplot() qplot(x = carat,y = price,data = diamonds)

qplot(x = log(carat),y = log(price),data = diamonds)

set.seed(0)#seed d=diamonds[sample(nrow(diamonds),100),] qplot(x = carat,y = price,data = d,colour=color)

qplot(x = carat,y = price,data = d,shape=cut)#shape

qplot(x = carat,y = price,data = d,alpha=I(1/10))#transparency

Geometry object | b |
---|---|

geom="point" | Draw a scatter diagram. It is the default option when x,y are provided |

geom="smooth" | Drawing smooth curve and standard error |

geom="boxplot" | Drawing box diagram |

geom="path" or geom = "line" | Draw line |

geom="histogram" | Draw histogram, the default when only x is provided |

geom="fregpoly" | Draw frequency polygon |

geom="density" | Draw density curve |

geom="bar" | Column drawing |

qplot(x = carat,y = price,data = diamonds,geom = c("point","smooth")) #Shadow part is error

qplot(x = carat,y = price,data = d,geom = c("point","smooth")，span=0.2) #span control the smoothness and curvature of the curve

1ibrary(mgcv) qplot(x = carat,y = price,data = d,geom = c("point","smooth"), method="gam",formula=y~s(x)) #Many different smoothers can be selected with the method parameter: method="1oess", when n is small, it is the default option, and the local regression method is used Use method="gam", formula=y~s(x) to call mgcv package to fit a generalized additive model.

qplot(x = carat, data = diamonds,geom = "histogram",binwidth=0.01,xlim=c(0,3))#histogram

qplot(x = carat, data = diamonds,geom = "histogram",binwidth=0.1,xlim=c(0,3),fill=color)#Set color #colour controls the color of the outline, sets the fill color with fi11, and adjusts the thickness of the line with size.

qplot(carat,data=diamonds,geom="density")#Density curve #The adjust parameter controls the smoothness of the curve (the larger the adjust value is, the smoother the curve is) #The binwidth parameter adjusts the smoothness by setting the group distance. #breaks parameter segmentation position

economics data set qplot(x = date,y = unemploy/pop,data = economics,geom = "line")#time series

#Facet #Faceting is to divide the data into several subsets, and then create a graph matrix, and draw each subset to the graph matrix pane. #All the subgraphs are of the same type and designed to facilitate the comparison. qplot(x =carat,data = diamonds, facets = color~.,geom = "histogram",binwidth=0.1,xlim = c(0,3))

#Some parameters of qplot are the same as plot #xlim,ylim,xlab,ylab,main,log qplot(x=carat,y=price,data=d,colour=color,xlab="x axis", ylab="y axis",main="Title",log="xy")