如果现在你还是将自己制作的图表放在PS或者PPT中进行随意组合的化,那么这篇文章你就得好好看看了,今天小编就给大家安利一个超强的突变自由组合包-patchwork,让你轻松实现多图的自由组合。
更多详细的数据可视化教程,可订阅我们的店铺课程:
下面小编结合实例介绍一下该包的主要用法:
patchwork 基本功能介绍
-
使用自带的数据进行单个图表的绘制:
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
p4 <- ggplot(mtcars) +
geom_bar(aes(gear)) +
facet_wrap(~cyl) +
ggtitle('Plot 4')
-
patchwork的基础用法
patchwork合并不同图表的基本操作十分简单,使用 + 号进行不同图表之间的组合,如下:
p1 + p2
p1 + p2
当突变进行合并之后,我们可对最后一个(p2)进行其他ggplot2图层元素的添加,如添加标题(title):
p1 + p2 + labs(subtitle = 'This will appear in the last plot',caption = "Visualization by DataCharm")
p1 + p2_with_title_caption
-
控制布局(Controlling layout)
默认情况下,patchwork将按行顺序排列图表,并使网格保持正方形。如下:
p1 + p2 + p3 + p4
by row
如果不想按照默认的行排列的化,你可以通过使用plot_layout() 函数进行组合布局的调整。如下:
p1 + p2 + p3 + p4 + plot_layout(nrow = 3, byrow = FALSE)
nrow_byrow_set
注意这次结果和上一个结果的不同(图中红圈标出),plot_layout()函数还有很多其他功能,小伙伴们可自行探索哈~~
-
堆叠和包装不同图表(Stacking and packing plots)
这个功能也是小编最喜欢的功能之一了,而patchwork包提供了 |- 并排图表,和 / -堆叠图表 操作,来灵活的对不同图表进行排列组合。下面通过实例进行说明:
p1 / p2
p1 / p2
p1 | (p2 / p3)
p1 | (p2 / p3)
-
添加注释文本信息(Annotating the composition)
在对图表进行合并后,我们通常需要添加一些注释文本(标题、序号等),这时,可通过 plot_annotation()进行快速添加:
(p1 | (p2 / p3)) +
plot_annotation(title = 'The surprising story about mtcars', caption = "Visualization by DataCharm")
exam of plot_annotation
此外,plot_annotation()还可以自动添加标签功能,这个在绘制多幅图表时可是经常用的方法哦~
p1 + p2 + p3 +
plot_annotation(tag_levels = 'a')
tag_levels
标签功能这里只介绍基本的,还有跟多用法,小伙伴们可去官网进行了解。
以上就完成了对patchwork包基本功能的介绍,当然,这些都是基础,想要探索更多功能,小伙伴们可去官网查看:https://patchwork.data-imaginist.com/articles/patchwork.html,接下来,还是老样子,小编再进行一幅定制化图表的操作(会用上以上全部方法)
patchwork 定制化出图
单独绘制每个可视化图表:
library(ggpol)
library(ggpubr)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Comparisons against reference
stat.test <- compare_means(
len ~ dose, data = ToothGrowth, group.by = "supp",
method = "t.test", ref.group = "0.5"
)
# plot01
bp <- ggbarplot(ToothGrowth, x = "supp", y = "len",
fill = "dose", palette = "jco",
add = "mean_sd", add.params = list(group = "dose"),
position = position_dodge(0.8))
plot_01 <- bp + stat_pvalue_manual(
stat.test, x = "supp", y.position = 33,
label = "p.signif",
position = position_dodge(0.8)
)
#plot02
library(ggsci)
df <- data.frame(score = rgamma(150, 4, 1),
gender = sample(c("M", "F"), 150, replace = TRUE),
genotype = factor(sample(1:3, 150, replace = TRUE)))
plot_02 <- ggplot(df) +
geom_boxjitter(aes(x = genotype, y = score, fill = gender),
jitter.shape = 21, jitter.color = NA,
jitter.height = 0, jitter.width = 0.04,
outlier.color = NA, errorbar.draw = TRUE) +
ggsci::scale_fill_jco()
#plot03
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
plot_03 <- ggdensity(wdata, x = "weight", fill = "lightgray",
add = "mean", rug = TRUE)
# plot04
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
plot_04 <- gghistogram(wdata, x = "weight", fill = "sex",
add = "mean", palette = c("lightgray", "gray50"),add_density = TRUE,rug = TRUE)
# 使用patchwork进行组合并添加主题
patchwork_example <- plot_01 + plot_02 + plot_03 + plot_04 +
plot_annotation(tag_levels = 'A',
title = "Example of <span style='color:#D20F26'>patchwork Process</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>plot_annotation & theme()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") &
theme_minimal(base_family = "Roboto Condensed") &
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
)
最终的可视化结果如下:
可以看出,patchwork包提供了一键设置全部图表主题风格的(& 添加图层即可),这个功能再对绘制多图时可是非常便利的。 https://patchwork.data-imaginist.com/
总结
今天小编推荐的这个绘图技巧对需绘制多图且需要合并的小伙伴来说,可谓是一大绘图利器,希望感兴趣的小伙伴可以学习下哦~~