欢迎关注R语言数据分析指南
❝最近有朋友问R中拼图的方法,对于在R中如何拼图小编颇有体会,本节就来详细介绍一下R中一些「高阶的拼图方法」希望对各位观众老爷有所帮助;「数据及代码已经上传VIP群,加群的观众老爷请自行下载」主要介绍 「cowplot与patchwork」这两款包,当然也会用到「ggpubr」
❞
参考文档
https://patchwork.data-imaginist.com/
❝❞
有需要学习数据可视化的朋友欢迎加入小编2022年度VIP群,目前群内已经上传公众号文档「数据+代码约200篇」,VIP交流群(1)已经500人满员,随着内容不断增多,为了更好的创做内容现在进群需「付费149元」,若转发此文档到朋友圈集赞20个可优惠20元,早进早享受;「一定让你感受到物超所值」
加载R包
library(tidyverse)
library(patchwork)
library(cowplot)
library(ggpubr)
定义颜色
my3cols <- c("#E7B800", "#2E9FDF", "#FC4E07")
加载数据
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x = dose, y = len))
构建拼图素材
bxp <- p + geom_boxplot(aes(color = dose)) +
scale_color_manual(values = my3cols)
dp <- p + geom_dotplot(aes(color = dose, fill = dose),
binaxis='y', stackdir='center') +
scale_color_manual(values = my3cols) +
scale_fill_manual(values = my3cols)
lp <- ggplot(economics, aes(x = date, y = psavert)) +
geom_line(color = "#E46726")
dens <- ggplot(iris, aes(Sepal.Length)) +
geom_density(aes(color = Species)) +
scale_color_manual(values = my3cols)
拼图
patchwork同时提供 「|」 和 「/」 分别用于水平和垂直布局
bxp+dp+lp+dens
(bxp+lp)/(dp+dens)
自定义布局
-
plot_layout()来指定布局 -
guides = 'collect'合并相同图例
bxp+bxp+bxp+plot_spacer()+
plot_layout(nrow=2,ncol=2,widths = c(2,1),guides = 'collect')
❝通过上图看到合并图例后位置不合适,可以通过添加guide_area()函数来将图例调整到空白位置
❞
bxp+bxp+bxp + guide_area() +
plot_layout(nrow=2,ncol=2,
widths = c(2,1),
guides = 'collect')
嵌套布局
lp + {
dens + {
bxp +
dp +
plot_layout(ncol = 1)
}
} +
plot_layout(ncol = 1)
添加注释文本
通过「plot_annotation」自定义添加标题或角标,使用&或 * 向所有子图添加元素,而不必单独修改所有图。两者的不同之处 * 仅在于将影响当前嵌套级别上的图:
bxp+dp+lp+dens+plot_layout(nrow=3,ncol=2)+
plot_annotation(tag_levels ='A',
title = 'The surprising truth about mtcars',
subtitle = 'These 3 plots will reveal yet-untold secrets about our beloved data-set',
caption = 'Disclaimer: None of these plots are insightful')& theme_gray()&
theme(plot.tag = element_text(size =10,color="red",hjust=0.5,vjust=0.5),
plot.tag.position = c(0,1))
(patchwork <- bxp+dp+lp+dens+plot_layout(nrow=3,ncol=2)+
plot_annotation(tag_levels="A")& theme_gray()&
theme(plot.tag = element_text(size =10,color="red"))
)
patchwork[[1]] <- patchwork[[1]] + plot_layout(tag_level = 'new')
patchwork+plot_annotation(tag_levels = c('A','1'))
(bxp + (dp + dens) + lp + plot_layout(ncol = 1))* theme_minimal()
❝上面介绍了「patchwork」的拼图方式基本满足了一些常规的需求,但是如果有特殊的需求patchwork也许无法满足,此时就需要用到「cowplot」包含的「ggdraw」等相关函数,在ggplot上再设置绘图层
❞
案例一
p <- bxp+dp+lp+dens+plot_layout(nrow=3,ncol=2)
p %>% ggdraw()+
draw_plot(bxp,scale=0.3,x=0,y=-0.35)
案例二
❝通过「ggdraw」函数设置后续可以跟ggplot绘图一样再图层之上再添加新的图层,主要使用「draw_」 等系列函数将所要添加的元素以图层的形式进行叠加,如下所示可以自定义添加各种元素
❞
p <- bxp+dp+lp+dens+plot_layout(nrow=3,ncol=2)
p %>% ggdraw()+
theme(plot.margin=unit(c(1,0,0,0),units="cm"))+
draw_plot(bxp,scale=0.3,x=0,y=-0.35)+
draw_grob(grid::grid.rect(gp=grid::gpar(fill="#E64B35B2",col="#E64B35B2")),
x=0.076,y=0.98,height = 0.05,width=0.75)+
draw_line(x = c(0.076,0.82), y = c(1,1), arrow=arrow(), lineend = "butt",
size = 1, col = "black")+
draw_label(label="2000",x=0.1,y=0.95,color="#E64B35B2",size=15,fontface ="bold")
❝本节介绍到此结束,主要想介绍「cowplot」的两个案例;喜欢的观众老爷欢迎分享转发;「当然更推荐大家加入我的VIP交流群」扫描下方二维码加小编微信「付费149元」后邀请进群,「一定让你感受到物超所值」,「添加小编微信请备注来意,以便高效处理」
❞
小编微信


