!备忘,方便自己查询,随着使用更新,无参考价值。
Function reference
Top 50 ggplot2 Visualizations
The R Graph Gallery
RStudio Cheatsheets
ggplot2: elegant graphics for data analysis 3rd edition
《R语言数据可视化之美》配套代码
知乎 | ggplot2作图最全教程(上)
知乎 | ggplot2作图最全教程(中)
知乎 | ggplot2作图最全教程(下)
R_ggplot2基础(一)
R_ggplot2基础(二)
R_ggplot2基础(三)
R_ggplot2基础(四)
1. 基础
ggplot()
一些想法:
1. ggplot要有两个思想,图层和映射;
图层的前后,达到的效果可能不同
2. 有许多冗余的功能,但要注意这些功能的细微区别;
3. 多看别人的绘图代码,提高对ggplot的理解和学习如何呈现自己的数据;
4. 不需要掌握所有参数,不需要直接生成最终版本的图,哪怕用`AI`再修;
5. ggplot的扩展包很多,不可能都完全学会,总体把握下,学习自己需要的;
6. ......
-
aes()
x
y
alpha: 透明度
color: 颜色
fill: 填充
shape: 形状
width: 宽度
linetype: 线型
注意一些参数:
position: 参考:ggplot2位置调整position
dodge: 并排放置,避免重叠
fill: 堆叠图形元素并将高度标准为 1
identity: 不做调整
jitter: 扰动避免点重合
stack: 图形元素堆叠statfill,colorgroup
2. 绘图 geom_*
映射思想与变量:
一个连续型变量:密度图、直方图、Wikinson点图
一个离散型变量:
两个连续型变量:散点图、气泡图
两个离散型变量:条形图/柱状图
一个离散一个连续:箱线图、小提琴图
2.1 数据分布
直方图、密度图、Wilkinson点图
核密度曲线是基于样本数据对总体分布做出的一个估计。曲线的平滑程度取决于核函数的带宽:带宽越大,曲线越平滑。
geom_density()-
geom_line()
stat = "density"
adjust geom_dotplot()geom_histogram()
箱线图、小提琴图、云雨图
geom_boxplot()geom_violin()geom_dotplot()

boxplot, histogram, density.jpg
2.2 数值关系型
散点图、气泡图、曲面拟合图、瀑布图、边际线
-
geom_point()
shape: 默认点形 19(实心圆)
size: 默认大小为 2;映射时,范围为 1 - 6,不反应真实数值大小比例
color,fill: 面对不同点形,有所区别 -
geom_jitter()
R绘图系统可以调用的点形 -
geom_rug(): 向散点图添加边际地毯 -
geom_smooth()
method:lm时,调用lm()函数对数据拟合线性模型,此外:glm|gam|loess等
se: 逻辑型,为拟合线添加置信域
level: 设置置信区间,默认 95%
# 点的大小:范围调整、比例调整
ggplot() +
geom_point() +
scale_size_area() # 使数据点的面积与变量值成正比
# scale_size_continuous(range = )
geom_smooth()
2.3 类别比较型
柱状图、饼图、旭日图、坡度图、雷达图、玫瑰图
-
geom_bar(): 柱状图/条形图
stat = "identity"
width = .8
position = position_dodge(width = .5) -
geom_col(): 条形图/柱状图 geom_count()
2.4 添加文本
geom_label()-
geom_text(): 添加文本
2.5 添加线
geom_segment()-
geom_hline(): 添加横线 -
geom_vline(): 添加竖线 -
geom_abline(): 添加有角度的线
2.6 误差
geom_crossbar()geom_errorbar()geom_linerange()geom_pointrange()
2.7 其他
geom_area()geom_quantile()geom_qq()
3. 统计 stat_*
略
4. 标度scale_*
4.1 离散型变量颜色填充scale_fill_*、scale_color_*
-
scale_fill_discrete()
scale_colour_discrete(): 色轮周围均匀等距色(同hue) -
scale_fill_hue()
scale_colour_hue(): 色轮周围均匀等距色(discrete) -
scale_fill_grey()
scale_colour_grey(): 灰度调色板,
start,end: 标度范围 0 ~ 1(黑色 ~ 白色),默认 0.2 ~ 0.8 -
scale_fill_brewer()
scale_colour_brewer(): ColorBrewer调色板
palette
library(RColorBrewer)
display.brewer.all(n=NULL, type="all", select=NULL, exact.n=TRUE, colorblindFriendly=FALSE)

RColorBrewer
-
scale_fill_manual()
scale_colour_manual(): 自定义颜色
values = c("red", "blue")|values = c("#CC6666", "#7777DD")|values = c(female="red", male="blue") -
scale_alpha(): 透明度
4.2 连续型变量颜色填充scale_fill_*、scale_color_*
-
scale_fill_gradient()
scale_colour_gradient(): 两色渐变
low = "black"
high = "white" -
scale_fill_gradient2()
scale_colour_gradient2(): 三色渐变 -
scale_fill_gradientn()
scale_colour_gradientn(): 等间隔的 n 种颜色的渐变色
colours = c("", "", "", "") scale_alpha()
4.3 形状大小
scale_shape()scale_shape_manual()scale_size()scale_size_area()scale_linetype()scale_radius()
4.4 坐标相关
注意与
coord_*相区别。
-
scale_x_discrete()
scale_y_discrete()
limits
breaks: 设置刻度线的位置
labels: 修改刻度标签文本 -
scale_x_reverse()
scale_y_reverse() -
scale_x_continuous()
scale_y_continuous() -
xlim(): 设置连续型x坐标轴值域,scale_x_continuous()的简便写法 -
ylim(): 设置连续型y坐标轴值域,scale_y_continuous()的简便写法
5. 坐标 coord_*
-
coord_fixed(): 设置 x 轴和 y 轴的缩放比例
ratio = 1/2 -
coord_flip(): 翻转坐标轴
直接对掉
x和y的映射不行吗?不行,比如直方图、密度图这种只能映射x。反转时是 y = x 镜面对称。
-
coord_cartesian():
xlim
ylim
expand -
coord_trans(): -
coord_polar(): 极坐标
theta: 绕x|y轴
direction:-1|1, 顺时针 | 逆时针
start: 起始点
6. 图例
-
guides()
fill = guide_legend(reverse = TRUE): 反转图例的顺序
fill = guide_legend(title = "legend title"): 设置图例标题 - guide_colourbar()
- guide_coloursteps()
- guide_legend()
- guide_bins()
- guide_axis()
guides()我时常不能理解,也是功能冗余?scale_*中也可以设置guide参数啊。
7. 分面 facet_*
-
facet_wrap()
scale:"fixed"(default) |"free_x"|"free_y"|"free" facet_grid()
8. 主题 theme_*
-
theme_gray(): 默认主题 -
theme_bw(),theme_classic(),theme_void() -
theme_dark(),theme_light(),theme_linedraw(),theme_minimal() -
theme_set(): 设置当前R会话下的默认主题,如:theme_set(theme_bw())
9. 细节
9.1 细节设置
-
annotate()
geom:"text"|"rect"|"segment"|"pointrange"...
其他参数与
geom_*对应。
-
labs()
title: 主标题
subtitle: 副标题
caption: 设置图例标题
colour/fill ggtitle()xlab()ylab()
有非常多的功能冗余?比如
labs(title, x, y)等价于ggtitle() + xlab() + ylab()。
9.2 主题元素
theme()
- 文本元素,如,所有文本元素
text = element_text()
family:Helvetica(无衬线)、Times(衬线)、Courier(等宽)
face:plain(普通)、bold(粗体)、italic(斜体)、bold.italic(粗斜体)
colour: 文字颜色
size: 字体大小
hjust: 横向对齐,0(左对齐)、0.5(居中)、1(右对齐)
vjust: 纵向对齐,0(底部对齐)、0.5(居中)、1(顶部对齐)
angle: 旋转角度
lineheight: 行间距倍数
注意:文本几何对象的文本属性与主题元素的基本一致。除了字体为fontface。
其他文本元素:
plot.title`: 标题文本外观
legend.text` 图例项文本的外观;legend.title(): 图例标题
axis.title: 双轴标签的外观;axis.title.x: x轴;axis.title.y: y轴
axis.text: 双轴刻度标签的外观;axis.text.x: x轴;axis.text.y: y轴
strip.text: 双向分面标签的外观;strip.text.x: 横向;strip.text.y: 纵向
在书上看到
axis.ticks与axis.text一样,也用于双轴刻度标签外观的设置,可能书上错了吧。
- 矩形元素,如所有矩形元素
rect = element_rect()
colour
fill
size
其他矩形元素:
plot.background: 整个图形的背景
panel.background: 绘图区域背景;panel.border: 绘图区域边框
legend.background: 图例的背景;legend.key: 图例边框
strip.background: 分面标签的背景
- 线条元素,如所有线条元素
line = element_line()
其他线条元素:
axis.line`: 坐标轴线
panel.grid.major`: 主网格线;panel.grid.major.x: 纵向;panel.grid.major.y: 横向
panel.grid.minor`: 次网格线;panel.grid.minor.x: 纵向;panel.grid.minor.y: 横向
-
legend.position = "right": 图例的位置,还可设置为"top"、"bottom"、"left"和c(1, 0)这样的位置坐标
设置为"none"移除所有图例 -
legend.justification = c(1, 0)
原图地址:https://henrywang.nl/ggplot2-theme-elements-demonstration/
10. 其他重要的相关的包
很多,其他文章中慢慢补充。


