模板
ggplot(data = <data> ) +
<geom_xxxx> ( mapping = aes(<mapping>),
stat = <stat>,
position = <position>) +
<coord_xxxx>(...) +
<facet_xxxx>(...)
ggplot2基础
- 定义数据(data),创建空图(plot object)。
ggplot()
- 指定几何图形(geoms – graphics shapes)展现数据。
geom_xxxx()
- 指定几何图形的特征(aesthetics – features)表现数值。
aes()
In brief, the grammar tells us that a statistical graphic is a mapping from data to aesthetic attributes (colour, shape, size) of geometric objects (points, lines, bars). The plot may also contain statistical transformations of the data and is drawn on a specific coordinate system. Facetting can be used to generate the same plot for different subsets of the dataset. It is the combination of these independent components that make up a graphic.
geom & aes
常用geom以及aes如下表,许多geom还是有color、size、group等aes。
geom | description | aesthetics |
---|---|---|
geom_point() | Data symbols | x, y, shape, fill |
geom_line() | Line (ordered on x) | x, y, linetype |
geom_path() | Line (original order) | x, y, linetype |
geom_text() | Text labels | x, y, label, angle, hjust, vjust |
geom_rect() | Rectangles | xmin, xmax, ymin, ymax, fill,linetype |
geom_polygon() | Polygons | x, y, fill, linetype |
geom_segment() | Line segments | x, y, xend, yend, linetype |
geom_bar() | Bars | x, fill, linetype, weight |
geom_histogram() | Histogram | x, fill, linetype, weight |
geom_boxplot() | Boxplots | x, y, fill, weight |
geom_density() | Density | x, y, fill, linetype |
geom_contour() | Contour lines | x, y, fill, linetype |
geom_smooth() | Smoothed line | x, y, fill, linetype |
data -> aesthetic -> geom
scale
color fill x y linetype shape size
Scale | Description | Parameters |
---|---|---|
scale_x_continuous() | Continuous axis | expand, trans |
scale_x_discrete() | Categorical axis | |
scale_x_date() | Date axis | major, minor, format |
scale_shape() | Symbol shape legend | |
scale_linetype() | Line pattern legend | |
scale_color_manual() | Symbol/line color legend | values |
scale_fill_manual() | Symbol/bar ll legend | values |
scale_size() | Symbol size legend | trans, to |
Common to most scales | name, breaks, labels, limits |
Statistical transformation
Stat Description Parameters
stat_identity() No transformation -
stat_count() Counts -
stat_bin() Binning binwidth, origin
stat_smooth() Smoother method, se, n
stat_boxplot() Boxplot statistics width
stat_contour() Contours breaks
data -> scale -> stat -> aesthetic -> geom -> cord
geom | stat | aes |
---|---|---|
geom_abline | abine | colour,linetype,size |
geom_area | identity | colour,fill,linetype,size,x,y |
geom_bar | bin | colour,fill,linetype,size,weight,x |
geom_bin2d | bin2d | colour,fill,linetype,size,weight,xmax,xmin,ymax,ymin |
geom_blank | identity | |
geom_boxplot | boxplot | colour,fill,lower,middle,size,upper,weight,x,ymax.ymin |
geom_contour | contour | colour,linetype,size,weight,x |
geom_crossbar | identity | colour,fill,linetype,size,x,y,ymax,ymin |
geom_density | density | colour,fill,linetype,size,weight,x,y |
geom_density2d | density2d | colour,linetype,size,weight,x,y |
geom_dotplot | bindot | colour,fill,x,y |
geom_errorbar | identity | colour,linetype,size,width,x,ymax,ymin |
geom_errorbarh | identity | colour,linetype,size,width,x,ymax,ymin |
geom_freqpoly | bin | colour,linetype,size |
geom_hex | binhex | colour,fill,size,x,y |
geom_histogram | bin | colour,fill,linetype,size,weight,x |
geom_hline | hline | colour,linetype,size |
geom_jitter | identity | colour,fill,shape,size,x,y |
geom_line | identity | colour,linetype,size,x,y |
geom_linerange | identity | colour,linetype,size,size,x,ymax,ymin |
geom_map | identity | colour,fill,linetype,size,x,y,map_id |
geom_path | identity | colour,linetype,size,x,y |
geom_point | identity | colour,fill,shape,size,x,y |
geom_pointrange | identity | colour,fill,linetype,shape,size,x,ymax,ymin |
geom_polygon | identity | colour,fill,linetype,size,x,y |
geom_quantile | quantile | colour,linetype,size,weight,x,y |
geom_raster | identity | colour,fill,linetype,size,x,y |
geom_rect | identity | colour,fill,linetype,size,xmax,xmin,ymax,ymin |
geom_ribbon | identity | colour,fill,linetype,size,x,ymax,ymin |
geom_rug | identity | colour,linetype,size |
geom_segment | identity | colour,linetype,size,x,xend.y.yend |
geom_smooth | smooth | aplha,colour,fill,linetype,size,weight,x,y |
geom_step | identity | colour,linetype,size,x,y |
geom_text | identity | angle,colour,hjust,label,size,size,vjust,x,y |
geom_tile | identity | colour,fill,linetype,size,x,y |
geom_violin | ydensity | weigth,colour,fill,size,linetype,x,y |
geom_vline | vline | colour,linetype,size |