有时候我们可能会希望,像文章一样,给图片一些A,B 的label:
其实这样的操作,在[[88-R可视化20-R的几种基于ggplot的拼图解决方案]] 中,就已经提到了。这里来进行一下总结。
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1') + theme_classic()
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2') + theme_classic()
p3 <- ggplot(mtcars) +
geom_histogram(aes(hp)) +
ggtitle('Plot 3') + theme_classic()
p4 <- ggplot(mtcars) +
geom_bar(aes(gear)) +
facet_wrap(~cyl) +
ggtitle('Plot 4') + theme_classic()
wrap_plots(p1,p2,p3,p4, nrow = 2)
在cowplot 中,plot_grid 是提供了一系列的选项的:
比如:
plot_grid(
p1, p2,p3,p4,
labels = "AUTO", label_size = 15,
label_fontfamily = "serif",
label_fontface = "bold",
label_colour = "black"
)
因为我是个patchwork 爱好者,后面主要以patchwork 为主。
详细了解参考:Arranging plots in a grid • cowplot (wilkelab.org)[2]
patchwork 也可以像cowplot一样,非常方便的自动添加:
wrap_plots(p1,p2,p3,p4, nrow = 2) +
plot_annotation(tag_levels = 'A')
不单单是A 开头的标记,这个tag_levels
选项还是有不少的:
A character vector defining the enumeration format to use at each level. Possible values are `'a'` for lowercase letters, `'A'`for uppercase letters, `'1'` for numbers, `'i'` for lowercase Roman numerals, and `'I'` for uppercase Roman numerals. It can also be a list containing character vectors defining arbitrary tag sequences. If any element in the list is a scalar and one of `'a'`, `'A'`, `'1'`, `'i`, or `'I'`, this level will be expanded to the expected sequence.
比如'a'
, 'A'
, '1'
, 'i
, or 'I'
,都可以被tag_levels
识别。
除此以外,我们还可以进行缝合。
比如,让AB 一组,让CD 一组:
(p1 + p2 + plot_layout(tag_level = 'new')) /
(p3 + p4 + plot_layout(tag_level = 'new')) +
plot_annotation(tag_levels = c("A",'1'))
再或者对tag 想要增加一些风格,比如开头、分割、结尾:
(p1 + p2 + plot_layout(tag_level = 'new')) /
(p3 + p4 + plot_layout(tag_level = 'new')) +
plot_annotation(tag_levels = c('A', '1'), tag_prefix = 'Fig. ',
tag_sep = '.', tag_suffix = ':')
可能会有同学会问,那么patchwork 如何调控tag 的属性呢?
不同于cowplot 的label 参数,patchwork 借助于theme 函数,但又不同于一般的ggplot 的数学表达。
其实这一部分更应该在[[88-R可视化20-R的几种基于ggplot的拼图解决方案]] 介绍,算是一种补充了。
在一般的ggplot 中,通过+
对对象属性进行修改。
比如:
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2') + theme_classic()
但如果是patchwork呢?
wrap_plots(p1,p2,p3,p4, nrow = 2) + theme_dark()
如果是全局调整,就需要&
了:
wrap_plots(p1,p2,p3,p4, nrow = 2) & theme_dark()
同样,如果我们需要对tag 的theme 调整:
patchwork <- wrap_plots(p1,p2,p3,p4, nrow = 2) & theme_dark()
patchwork +
plot_annotation(tag_levels = 'A') &
theme(plot.tag = element_text(size = 14, face = "italic")
)
虽然plot_annotation 内置了theme
选项,但其使用受限:
★A ggplot theme specification to use for the plot. Only elements related to the titles as well as plot margin and background is used. ”
但我们可以利用其专门调控patchwork title 的特点,加以使用:
(p1 + p2 + plot_layout(tag_level = 'new')) /
(p3 + p4 + plot_layout(tag_level = 'new')) +
plot_annotation(
tag_levels = c('A', '1'), tag_prefix = 'Fig. ',
tag_sep = '.', tag_suffix = ':',
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 = theme(plot.title = element_text(size = 18))
) & theme(
plot.tag = element_text(size = 10, face = "italic")
)
不过需要注意的是,&
的修改同样会作用于全局,比如先前修改了patchwork title:
wrap_plots(p1,p2,p3, nrow = 2) +
plot_annotation(title = "Fig.1",
theme = theme(plot.title = element_text(size = 18))) & theme_dark()
此外,patchwork 语法还加入了-
, *
,个人觉得徒增不必,就不展开了,可以了解:Plot Assembly • patchwork (data-imaginist.com)[3]
如何实现下图的效果呢?
然而“一山不容二虎”,并不能容纳两个plot_annotation :
patchwork1 <- wrap_plots(p1,p2,p3, nrow = 2) + plot_annotation(title = "TEST01") & theme_dark()
patchwork2 <- wrap_plots(p1,p2,p4, nrow = 2) +
plot_annotation(title = "TEST02")
patchwork1 / patchwork2
如果是结合cowplot呢?
patchwork1 <- wrap_plots(p1,p2,p3, nrow = 2) +
plot_annotation(title = "Fig.1",
theme = theme(plot.title = element_text(size = 18)))
patchwork2 <- wrap_plots(p1,p4, nrow = 1) +
plot_annotation(title = "Fig.2", theme = theme(plot.title = element_text(size = 18)))
cowplot::plot_grid(patchwork1,patchwork2, ncol = 1, rel_heights = c(2,1))
甚至还可以借助label 元素,以假乱真再加一个大的“title”:
cowplot::plot_grid(patchwork1,patchwork2, ncol = 1, rel_heights = c(2,1),
labels = "TEST 2 PLOT",
label_size = 20, hjust = -2.5, vjust = 1)
ps: 这个title 没有独立凸出,有点点丑。
上图有没有更好的解决方案呢?
如果是先cowplot,再反过来用patchwork呢?
读者朋友们,你们试试吧。Arranging plots in a grid • cowplot (wilkelab.org)[4]
[1]
Adding Annotation and Style • patchwork (data-imaginist.com): https://patchwork.data-imaginist.com/articles/guides/annotation.html
[2]
Arranging plots in a grid • cowplot (wilkelab.org): https://wilkelab.org/cowplot/articles/plot_grid.html
[3]
Plot Assembly • patchwork (data-imaginist.com): https://patchwork.data-imaginist.com/articles/guides/assembly.html
[4]
Arranging plots in a grid • cowplot (wilkelab.org): https://wilkelab.org/cowplot/articles/plot_grid.html