R语言作图-ggplot2
画图是展示数据的方法
常用可视化R包和函数
作图:base、ggplot2、ggpubr
拼图:par里的mfrow、gridarrange、cowplot、patchwork
导出:经典三段论、ggsave、eoffice(topptx)
作图
基础包绘图
高级绘图函数+补充的低级绘图函数(可选)=完整图
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello') ##紧接着运行即可加在图上
ggplot2
library(ggplot2)
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
ggpubr
是ggplot2的简化版本,沿用其另一种默认设置,新手友好
library(ggpubr)
ggscatter(iris,
x="Sepal.Length",
y="Petal.Length",
color="Species")
ggplot2-绘图模板
-
绘图模板:ggplot(数据)+FUNCTION(mapping= aes(映射))+属性
#模板 ggplot(data = <DATA>)+ geom_FUNCTION(mapping = aes(<MAPPING>)) #代码 ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length))
特殊语法:行末写
+
,列名单独出现,不加""
。 -
属性设置:是geom_FUNCTION的参数
- 颜色:color
- 大小:size
- 形状:shape
- 透明度:alpha
- 填充颜色:fill
ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), size = 6, # 点的大小5mm alpha = 0.3, # 透明度 50% shape = 20, color = 'orangered') # 点的形状
-
映射:按照数据框的某一列来定义图的某个属性,是aes()的参数
- x轴:x
- y轴:y
- 颜色:color
- 大小:size
- 形状:shape
- 透明度:alpha
- 填充颜色:fill
ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species))
-
如何自行指定映射的具体颜色
ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species))+ scale_color_manual(values = c("blue","grey","red"))
-
配色R包
rcolorBrewer
已经内置进ggplot2
ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length, color = Species))+ scale_color_brewer(palette = 'Set1')
更多样的颜色:使用十六进制颜色编码
-
区分color和fill两个属性
-
实心图形:使用color控制颜色
-
空心图形:color控制外框,fill控制填充
ggplot(data = iris)+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), shape = 24, fill = "yellow", color = 'red')
-
-
几何对象:以geom_funciton()画出的图形均称为几何对象
几何对象可以叠加,类似ps图层的概念
##局部设置,对所在的图层/几何对象生效 ggplot(data = iris) + geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) ##全局设置,对全部的图层/几何对象生效 ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+ geom_smooth()+ geom_point()
-
位置:
ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species))+ geom_boxplot()+ geom_point() # ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species))+ geom_boxplot()+ geom_point(position = 'jitter')/#geom_jitter()
jitter的替代—dotplot和蜜蜂图
-
坐标系:
coord_flip()
反转坐标系。 -
主题:ggplot2有很多种主题,如+
theme_bw()
可以将背景改为白色。
-
图层的叠放顺序:先写在下,后写在上
ggplot(data = iris, mapping = aes(x = Species, y = Sepal.Width))+ geom_violin(mapping = aes(fill = Species))+ geom_boxplot()+ geom_point(mapping = aes(shape = Species),position='jitter')+ coord_flip()
ggpubr
搜代码直接使用即可,不需要系统学习
代码示例:
# sthda上有大量ggpubr出的图
library(ggpubr)
p = ggboxplot(iris, x = "Species", y = "Sepal.Length",
color = "Species", shape = "Species",add = "jitter")
p
ggpubr的实用小功能:实现组间比较
#要求是长度为2的向量组成的列表,内容是由横坐标组成。
my_comparisons <- list( c("setosa", "versicolor"),
c("setosa", "virginica"),
c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons,
aes(label = after_stat(p.signif)))
- ggpubr和ggplot2的图可以赋值,基础包画图不可赋值。
图片保存
-
ggplot2系列的保存方法:
ggsave(p,filename = "iris_box_ggpubr.png") #文件类型根据后缀决定 #一些参数支持修改分辨率、长宽比例等,有需要时详细学习。
-
通用保存方法:三段论
-
第一段:保存函数及文件名
-
第二段:作图代码
-
第三段:关闭画板
#第一段 pdf("iris_box_ggpubr.pdf") #第二段 boxplot(iris[,1]~iris[,5]) text(6.5,4, labels = 'hello') #第三段 dev.off()
-
-
eoffice包:将图片保存至ppt
library(eoffice) topptx(p,"iris_box_ggpubr.pptx")
拼图
patchwork包:完美兼容ggplot2
- 支持p1+p2直接拼图
- 遇到需要的功能再学习,小洁老师的patch包教程:大佬新包patchwork:可能是迄今为止最优秀的拼图包 (qq.com)
- 画板被占用:
dev.off()
关闭画板,救不了就dev.new()
开个新的~
如何搜索画图代码和资料
- 可以用bing/百度/搜狗微信搜索画图教程~
- STHDA:一个画图网站
- 小洁老师的画图合集: 画图合辑
- 画图代码+数据+解决问题的能力 = 成品图
画图的正确思维
- 我的数据适合什么图展示?
- 搜画图代码
- 代码可复制,图片美观
- 提供了示例数据
- 仿制示例数据
- 数据类型、数据结构
- 组织方式、对应关系
- 套代码,调细节
- 随机抽样:
sample(向量,n)
,随机种子:set.seed()
,每个随机种子对应一个抽样结果。
引用自
生信技能树
课程。给小洁老师比心~~