常用可视化R包
*加粗为重点掌握
1. 作图
1.1 基础包:略显陈旧,了解一下
(1) 绘图函数
高级绘图函数
plot() #散点图等多种图形,根据数据分类,调用相应函数绘图
hist() #频率直方图
boxplot() #箱线图
stripchart() #点图
barplot() #柱状图
dotplot() #饼图
piechart() #点图
matplot() #数学图形
低级绘图函数
lines() #添加线
curve() #添加曲线
abline() #添加给定斜率的线
points() #添加点
segments() #折线
arrows() #箭头
axis() #坐标轴
box() #外框
title() #标题
text() #文字
mtext() #图边文字
绘图参数
(2)举例
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello')
同样数据,ggplot、ggpubr输出区别
dev.off() #关闭画板
1.2 ggplot2:中坚力量,学起来有点难
(1)入门级绘图模板:作图数据,横纵坐标
ggplot(data = <DATA>)+
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
#模板
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
(2)属性设置(颜色、大小、透明度、点的形状,线型等)
- color(外框颜色)&fill(填充颜色)
R色卡:https://evvail.com/2020/02/01/477.html
RColorBrewer://www.greatytc.com/p/194765c8c17e
可以输入颜色名称、16色、字符串(颜色名称or16进制颜色代码需加引号,按列映射直接输入列名可不加引号) - size:自动单位mm
- alpha:透明度,0.5代表50%
-
shape:形状
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy),
color = "blue") #手动设置,要加引号
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy),
size = 5, # 点的大小5mm,单位自带mm
alpha = 0.5, # 透明度 50%
shape = 8) # 点的形状,自带体系
1.3 映射:按照数据框的某一列来定义图的某个属性
- 映射:领导思维,只说按照某列分配颜色,不必说具体是哪几种颜色
- 手动设置:把所有的点设置为同一个颜色,直接指定是哪个颜色
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species)) #aes参数,列名,颜色自动设置,不加引号
aes_string(x = ,y = )
#字符转变为映射
自行指定映射的具体颜色
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_manual(values = c("blue","grey","red"))
区分color和fill两个属性
- 空心形状和实心形状都用color设置颜色
- 既有边框又有内心的,才需要color和fill两个参数
- color管边框,fill管实心
1.4 分面
单分面
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_wrap(~ Species) #用species来分面
双分面
test$Group = sample(letters[1:5],150,replace = T)
ggplot(data = test) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
facet_grid(Group ~ Species) #用species和group来分图
循环中存储多个图形
- 可以建立列表,将图形存储在列表里
- 再用patchwork中wrap_plots进行图片分割
1.5 几何对象
局部设置和全局设置
#局部设置
ggplot(data = test) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#全局设置
ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()
- 局部映射:仅对当前图层有效,单独函数后aes()
- 全局映射:对所有图层有效
- 当局部设置和全局设置冲突,按局部设置结果运行
2.6 统计变换-直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
- geom_开头的函数是几何对象函数
- stat_开头函数是统计函数
不统计,数据直接做图
ggplot(data = fre) +
geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
#只有写stat=identity才会不统计
不统计count,统计prop
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
#group=1是把所有对象作为一个组
1.7 位置关系
抖动的点图
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_point()
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_jitter()
#jitter可以让点不重合
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")
#dotplot散点很整齐,点不重合,也不奔放
直方图
- 叠放直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
- 并列直方图
position = "dodge"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
1.8 坐标系
- 翻转坐标系,类似转置:coord_flip()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
- 极坐标系:coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
完整绘图模板
ggplot(data = <DATA>)+
<geom_function>( #绘图函数
mapping = aes(<MAPPINGS>), #x、y坐标
stat = <STAT> #直方图中若需手动y坐标
position = <POSITION>
)+
<COORDINATE_FUNCTION>+ #极坐标
<FACET_FUNCTION> #单分面、双分面
1.3 ggpubr:江湖救急,ggplot2简化和美化,褒贬不一
- ggpubr 搜代码直接用,基本不需要系统学习
- 基础包不可以赋值,ggpubr图可以赋值
- sthda有大量ggpubr出的图:www.sthda.com
- 不可替代功能:comparisons(比较两组之间)
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
p
my_comparisons <- list( c("setosa", "versicolor"),
c("setosa", "virginica"),
c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
stat_compare_means(label.y = 9)
图片的保存和导出
1.基础包
pdf("iris_box_ggpubr.pdf")
#保存格式及文件名格式一一对应
2.ggplot系列图(包括ggpubr)
- ggsave()
- 通用三段论:保存的格式及文件名,作图代码,关闭画板
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")
#ggplot2可以赋值
3.eoffice包
- 导出为ppt,全部元素都是可编辑模式
topptx(p,"iris_box_ggpubr.pptx")
拼图
大佬包patchwork
https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ