library(ggplot2)
Appendix D — ggplot2 速查表
资料来源:https://rstudio.github.io/cheatsheets/
D.1 基础
ggplot2 基于图形语法理念,即所有图形都可以通过相同组件构建:数据集、坐标系和几何对象(代表数据点的视觉标记)。
要展示数值,需要将数据中的变量映射到几何对象的视觉属性(美学映射),如大小、颜色以及x和y位置。
使用以下模板构建图形:
ggplot(data = <数据>) +
<几何对象函数>(mapping = aes(<映射关系>),
stat = <统计变换>,
position = <位置调整>) +
<坐标系函数> +
<分面函数> +
<比例尺函数> +
<主题函数>
数据、几何对象函数和美学映射是必需项。统计变换、位置调整以及坐标系、分面、比例尺和主题函数为可选项,会提供合理的默认值。
ggplot(data = mpg, aes(x = cty, y = hwy))
: 初始化绘图,后续通过添加图层完成。每个图层添加一个几何对象函数。
ggplot(data = mpg, aes(x = cty, y = hwy)) +
geom_point()
last_plot()
: 返回上一次绘制的图形。
last_plot()
ggsave("plot.png", width = 5, height = 5)
: 将最后一次绘制的图形保存为5英寸x5英寸的”plot.png”文件。文件类型自动匹配扩展名。
ggsave("plot.png", width = 5, height = 5)
D.2 美学映射
常用美学属性值:
color
和fill
: 字符串("red"
,"#RRGGBB"
)linetype
: 整数或字符串(0="blank"
,1="solid"
,2="dashed"
,3="dotted"
,4="dotdash"
,5="longdash"
,6="twodash"
)size
: 整数(点的大小和文本尺寸,单位为毫米)linewidth
: 整数(线的宽度,单位为毫米)shape
: 整数/形状名称或单个字符("a"
)- 形状整数/名称对应关系:0=
"square open"
,1="circle open"
,2="triangle open"
,3="plus"
,4="cross"
,5="diamond open"
,6="triangle down open"
,7="square cross"
,8="asterisk"
,9="diamond plus"
,10="circle plus"
,11="star"
,12="square plus"
,13="circle cross"
,14="square triangle"
,15="square"
,16="circle"
,17="triangle"
,18="diamond"
,19="circle small"
,20="bullet"
,21="circle filled"
,22="square filled"
,23="diamond filled"
,24="triangle filled"
,25="triangle down filled"
- 形状整数/名称对应关系:0=
D.3 几何对象
使用几何对象函数表示数据点,通过几何对象的美学属性映射变量。每个函数返回一个图层。
D.3.1 图形基元
<- ggplot(economics, aes(date, unemploy))
a <- ggplot(seals, aes(x = long, y = lat)) b
a + geom_blank()
和a + expand_limits()
: 确保所有图形的范围包含所有值。
+ geom_blank() a
b + geom_curve(aes(yend = lat + 1, xend = long + 1), curvature = 1)
: 绘制从(x, y)
到(xend, yend)
的曲线。aes()
参数:x
,xend
,y
,yend
,alpha
,angle
,color
,curvature
,linetype
,size
+ geom_curve(aes(yend = lat + 1, xend = long + 1), curvature = 1) b
a + geom_path(lineend = "butt", linejoin = "round", linemitre = 1)
: 按数据出现顺序连接观测点。aes()
参数:x
,y
,alpha
,color
,group
,linetype
,size
+ geom_path(lineend = "butt", linejoin = "round", linemitre = 1) a
a + geom_polygon(aes(alpha = 50))
: 将点连接成多边形。aes()
参数:x
,y
,alpha
,color
,fill
,group
,subgroup
,linetype
,size
+ geom_polygon(aes(alpha = 50)) a
b + geom_rect(aes(xmin = long, ymin = lat, xmax = long + 1, ymax = lat + 1))
: 通过四个角点绘制矩形。aes()
参数:xmax
,xmin
,ymax
,ymin
,alpha
,color
,fill
,linetype
,size
+ geom_rect(aes(xmin = long, ymin = lat, xmax = long + 1, ymax = lat + 1)) b
a + geom_ribbon(aes(ymin = unemploy - 900, ymax = unemploy + 900))
: 为每个x
绘制从ymin
到ymax
的区间。aes()
参数:x
,ymax
,ymin
,alpha
,color
,fill
,group
,linetype
,size
+ geom_ribbon(aes(ymin = unemploy - 900, ymax = unemploy + 900)) a
D.3.1.1 线段
常用美学属性:x
, y
, alpha
, color
, linetype
, size
, linewidth
b + geom_abline(aes(intercept = 0, slope = 1))
: 绘制指定斜率和截距的参考线
+ geom_abline(aes(intercept = 0, slope = 1)) b
b + geom_hline(aes(yintercept = lat))
: 绘制水平参考线
+ geom_hline(aes(yintercept = lat)) b
b + geom_vline(aes(xintercept = long))
: 绘制垂直参考线
+ geom_vline(aes(xintercept = long)) b
b + geom_segment(aes(yend = lat + 1, xend = long + 1))
: 绘制两点间直线
+ geom_segment(aes(yend = lat + 1, xend = long + 1)) b
b + geom_spoke(aes(angle = 1:1155, radius = 1))
: 使用极坐标绘制线段
+ geom_spoke(aes(angle = 1:1155, radius = 1)) b
D.3.2 单变量-连续型
<- ggplot(mpg, aes(hwy))
c <- ggplot(mpg) c2
c + geom_area(stat = "bin")
: 面积图
+ geom_area(stat = "bin") c
c + geom_density(kernel = "gaussian")
: 核密度估计曲线
+ geom_density(kernel = "gaussian") c
c + geom_dotplot()
: 点图
+ geom_dotplot() c
c + geom_freqpoly()
: 频率多边形
+ geom_freqpoly() c
c + geom_histogram(binwidth = 5)
: 直方图
+ geom_histogram(binwidth = 5) c
c2 + geom_qq(aes(sample = hwy))
: Q-Q图
+ geom_qq(aes(sample = hwy)) c2
D.3.3 单变量-离散型
<- ggplot(mpg, aes(fl)) d
d + geom_bar()
: 条形图
+ geom_bar() d
D.3.4 双变量-连续型
<- ggplot(mpg, aes(cty, hwy)) e
e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1)
: 带背景的文字标注
+ geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1) e
e + geom_point()
: 散点图
+ geom_point() e
e + geom_quantile()
: 分位数回归线
+ geom_quantile() e
e + geom_rug(sides = "bl")
: 地毯图
+ geom_rug(sides = "bl") e
e + geom_smooth(method = lm)
: 平滑条件均值
+ geom_smooth(method = lm) e
e + geom_text(aes(label = cty), nudge_x = 1, nudge_y = 1)
: 文字标注
+ geom_text(aes(label = cty), nudge_x = 1, nudge_y = 1) e
D.3.5 双变量-离散型与连续型
<- ggplot(mpg, aes(class, hwy)) f
f + geom_col()
: 柱状图
+ geom_col() f
f + geom_boxplot()
: 箱线图
+ geom_boxplot() f
f + geom_dotplot(binaxis ="y", stackdir = "center")
: 点图
+ geom_dotplot(binaxis ="y", stackdir = "center") f
f + geom_violin(scale = "area")
: 小提琴图
+ geom_violin(scale = "area") f
D.3.6 双变量-离散型
<- ggplot(diamonds, aes(cut, color)) g
g + geom_count()
: 计数点图
+ geom_count() g
e + geom_jitter(height = 2, width = 2)
: 抖动点图
+ geom_jitter(height = 2, width = 2) e
D.3.7 双变量-连续型双变量分布
<- ggplot(diamonds, aes(carat, price)) h
h + geom_bin2d(binwidth = c(0.25, 500))
: 二维分箱热图
+ geom_bin2d(binwidth = c(0.25, 500)) h
h + geom_density_2d()
: 二维核密度等高线
+ geom_density_2d() h
h + geom_hex()
: 六边形分箱热图
+ geom_hex() h
D.3.8 双变量-连续型函数
<- ggplot(economics, aes(date, unemploy)) i
i + geom_area()
: 面积图
+ geom_area() i
i + geom_line()
: 折线图
+ geom_line() i
i + geom_step(direction = "hv")
: 阶梯图
+ geom_step(direction = "hv") i
D.3.9 双变量-误差可视化
<- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
df <- ggplot(df, aes(grp, fit, ymin = fit - se, ymax = fit + se)) j
j + geom_crossbar(fatten = 2)
: 交叉条
+ geom_crossbar(fatten = 2) j
j + geom_errorbar()
: 误差条
+ geom_errorbar() j
j + geom_linerange()
: 线条范围
+ geom_linerange() j
j + geom_pointrange()
: 点范围
+ geom_pointrange() j
D.3.10 双变量-地图
根据数据中的简单要素绘制几何对象:
<- sf::st_read(system.file("shape/nc.shp", package = "sf")) nc
Reading layer `nc' from data source
`/Users/gaoch/Library/Caches/org.R-project.R/R/renv/cache/v5/macos/R-4.4/aarch64-apple-darwin20/sf/1.0-19/fe02eec2f6b3ba0e24afe83d5ccfb528/sf/shape/nc.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS: NAD27
ggplot(nc) +
geom_sf(aes(fill = AREA))
D.3.11 三变量
$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
seals<- ggplot(seals, aes(long, lat)) l
l + geom_contour(aes(z = z))
: 等高线图
+ geom_contour(aes(z = z)) l
l + geom_contour_filled(aes(fill = z))
: 填充等高线图
+ geom_contour_filled(aes(z = z)) l
l + geom_raster(aes(fill = z), hjust = 0.5, vjust = 0.5, interpolate = FALSE)
: 栅格图
+ geom_raster(aes(fill = z), hjust = 0.5, vjust = 0.5, interpolate = FALSE) l
l + geom_tile(aes(fill = z))
: 瓦片图
+ geom_tile(aes(fill = z)) l
D.4 统计变换
通过统计变换构建图层的另一种方式。统计变换会生成新的绘图变量(如计数、比例)。使用after_stat(变量名)
语法将统计变量映射到美学属性。
+ stat_density_2d(aes(fill = after_stat(level)), geom = "polygon") i
D.5 比例尺
使用scales包覆盖默认设置。比例尺将数据值映射到美学属性的视觉值。
<- d + geom_bar(aes(fill = fl))
n + scale_fill_manual(
n values = c("skyblue", "royalblue", "blue", "navy"),
limits = c("d", "e", "p", "r"),
breaks =c("d", "e", "p", "r"),
name = "fuel",
labels = c("D", "E", "P", "R")
)
D.6 坐标系
<- d + geom_bar() u
u + coord_cartesian(xlim = c(0, 5))
: 笛卡尔坐标系
+ coord_cartesian(xlim = c(0, 5)) u
u + coord_fixed(ratio = 1/2)
: 固定比例坐标系
+ coord_fixed(ratio = 1/2) u
u + coord_polar(theta = "x", direction = 1)
: 极坐标系
+ coord_polar(theta = "x", direction = 1) u
D.7 位置调整
位置调整决定如何排列重叠的几何对象。
<- ggplot(mpg, aes(fl, fill = drv)) s
s + geom_bar(position = "dodge")
: 并列排列
+ geom_bar(position = "dodge") s
s + geom_bar(position = "fill")
: 堆叠标准化
+ geom_bar(position = "fill") s
D.8 主题
u + theme_bw()
: 白底网格主题
+ theme_bw() u
u + theme_classic()
: 经典无网格主题
+ theme_classic() u
D.9 分面
分面根据离散变量值将图形划分为子图。
<- ggplot(mpg, aes(cty, hwy)) + geom_point() t
t + facet_grid(. ~ fl)
: 列分面
+ facet_grid(. ~ fl) t
t + facet_wrap(~ fl)
: 环绕分面
+ facet_wrap(~ fl) t
D.10 标签与图例
使用labs()
添加图形标签:
+ labs(x = "城市油耗", y = "高速油耗",
t title = "油耗关系图",
subtitle = "城市与高速公路油耗对比",
caption = "数据来源: mpg数据集")
D.11 缩放
t + coord_cartesian(xlim = c(0, 100), ylim = c(10,20))
: 无损缩放
+ coord_cartesian(xlim = c(0, 30), ylim = c(10,40)) t