R | ggplot2拼圖 —— patchwork
塵世中一個迷途小書僮關(guān)注
22019.11.23 13:53:08字數(shù) 813閱讀 4,991
在Y叔公號(biobabble)看到cowplot乃舊愛,patchwork是新歡一文后,覺得甚是有趣,便動手復現(xiàn)了一下。
據(jù)patchwork的作者?Thomas Pedersen所介紹,他開發(fā)的初衷就是讓ggplots的組合可以ridiculously simple!
The goal of?patchwork?is to make it?ridiculously simple?to combine separate ggplots into the same graphic. As such it tries to solve the same problem as?gridExtra::grid.arrange()?and?cowplot::plot_grid?but using an API that incites exploration and iteration.
我使用后的感覺也是如此,?patchwork可讀性高、操作簡單、可操作性也高,真的太強了。下面簡單介紹一下這個包吧
安裝
由于這個包還沒發(fā)布到CRAN上,所以直接用install.packages()是安裝不了的,要使用以下方法安裝
devtools::install_github("thomasp85/patchwork")
更新于 2020-12-29
patchwork?已經(jīng)發(fā)布了有一段時間了,可以直接通過以下命令安裝
install.packages('patchwork')
當然也可以繼續(xù)用devtools::install_github("thomasp85/patchwork")的方法安裝開發(fā)版本(不建議)
語法
patchwork使用的語法十分簡單,就是用+就可以按行將圖拼起來,而/是按列拼圖,使用|分隔后可以同時使用兩種方法拼圖。
下面我們畫幾張圖試試
p1<-ggplot(mtcars)+geom_point(aes(mpg,disp,colour=mpg,size=wt))+ggtitle('p1')
p2<-ggplot(mtcars)+geom_boxplot(aes(gear,disp,group=gear))+ggtitle('p2')
p3<-ggplot(mtcars)+geom_point(aes(hp,wt,colour=mpg,size=wt))+scale_colour_viridis_c()+ggtitle('p3')
p4<-ggplot(mtcars)+geom_smooth(aes(disp,qsec))+ggtitle('p4')
p5<-ggplot(mtcars)+geom_bar(aes(carb))+ggtitle('p5')
橫拼
可以通過+或|實現(xiàn),兩者都是按row合并的
p1 + p2

p1 + p2
還可以通過plot_spacer()在圖與圖之間插入空白,實現(xiàn)ggplot的九宮格!
p1 + plot_spacer() + p2 +? plot_spacer() + p3 + plot_spacer() +? p4 + plot_spacer() + p5

Nine Palace Grid
豎拼
豎著拼也很簡單,就是一個/就完事了,有點分式的意思
p2/p4

p2/p4
一起拼
如果想同時實現(xiàn)兩種拼圖的語法,用|即可完成了
(p2/p4)|p3

(p2/p4)|p3
方便的注釋
patchwork還提供了一個plot_annotation的功能,可以允許我們在給拼起來的圖加title的前提下,還能為每一個subplot加上一個tag,效果如下:
p1 + p2 + plot_annotation(title = "combine", tag_levels = "A")

這個功能在生成文章的figure的時候就很有用了
復雜的拼圖
以上我們所做的都是在網(wǎng)格上一個個格子地拼圖的,當然,patchwork也可以通過設(shè)定layout實現(xiàn)指定每個圖所占的格子來拼,容許了更高的操作自由度
layout <- "BBBBAACC##CC"p4 + p2 + p3 +? plot_layout(design = layout)

以上的#是空白,ABC就代表了輸入中相應的圖
控制legend
patchwork可以通過指定plot_layout(guide = 'collect')將legend都放到圖的一側(cè)
(p2 | (p1/p3)) + plot_layout(guides = 'collect')

有趣的是,盡管我們的mpglegend都是對mpg上色,但僅因為顏色映射的方法不一樣,就分別列了出來。所以,這個guides = 'collect'不僅僅只是收集legend,它是在進行了legend的比較之后,才放出來的。
總而言之,patchwork是個十分方便且強大的ggplots拼圖工具,而且尚在開發(fā)之中,意味著以后可能還會有更多更強大的功能出現(xiàn),有需要的朋友就趕緊用起來吧!
參考:
2?https://www.rdocumentation.org/packages/patchwork/versions/0.0.1.9000
install.packages("patchwork")library(patchwork)??plot_layout
# 重要參數(shù)解釋plot_layout(??ncol = NULL, # 設(shè)置列數(shù)??nrow = NULL, # 設(shè)置行數(shù)??byrow = NULL, # 設(shè)置案列輸出方式 行 or 列??widths = NULL, # 設(shè)置寬度??heights = NULL, # 設(shè)置高度??guides = NULL,??tag_level = NULL,??design = NULL)
效果演示
library(ggplot2)library(patchwork)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_bar(aes(gear)) + facet_wrap(~cyl)
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
p5 <- ggplot(mtcars) + geom_violin(aes(cyl, mpg, group = cyl))
# The plots are layed out automatically by default# 默認按照行排序p1 + p2 + p3 + p4 + p5# 效果見下圖
# Use byrow to change how the grid is filled out# 設(shè)置按照列諸葛輸出p1 + p2 + p3 + p4 + p5 + plot_layout(byrow = FALSE)# 效果見下圖

上圖 p1 + p2 + p3 + p4 + p5

上圖 p1 + p2 + p3 + p4 + p5 + plot_layout(byrow = FALSE)
# Change the grid dimensions# 設(shè)置布局列數(shù)和寬度p1 + p2 + p3 + p4 + p5 + plot_layout(ncol = 2, widths = c(1, 2))# 效果見下圖

上圖 p1 + p2 + p3 + p4 + p5 + plot_layout(ncol = 2, widths = c(1, 2))
# Define layout at different nesting levels# 定義不同嵌套級別的布局p1 +??p2 +??(p3 +? ???p4 +? ???plot_layout(ncol = 1)??) +??p5 +??plot_layout(widths = c(2, 1))# 效果見下圖

# Complex layouts can be created with the `design` argument#可以使用“design”參數(shù)創(chuàng)建復雜的布局design <- c(??area(1, 1, 2),??area(1, 2, 1, 3),??area(2, 3, 3),??area(3, 1, 3, 2),??area(2, 2))p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)# 效果見下圖

圖 p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)
# The same can be specified as a character string:#可以將其指定為字符串:design <- "122153443"p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)# 效果見下圖

圖 p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)
# When using strings to define the design `#` can be used to denote empty#使用字符串定義設(shè)計時,`#`可用于表示空
# areas#區(qū)域design <- "1##123##3"p1 + p2 + p3 + plot_layout(design = design)# 效果見下圖

圖??p1 + p2 + p3 + plot_layout(design = design)
# Use guides="collect" to remove duplicate guides#使用guides=“collect”刪除重復的輔助線p6 <- ggplot(mtcars) + geom_point(aes(mpg, disp, color=cyl))p7 <- ggplot(mtcars) + geom_point(aes(mpg, hp, color=cyl))p6 + p7 + plot_layout(guides='collect')# 效果見下圖

圖 p6 + p7 + plot_layout(guides='collect')
# Guide position must be applied to entire patchwork#導向位置必須應用于整個拼接p6 + p7 + plot_layout(guides='collect') &??theme(legend.position='bottom')# 效果見下圖

安裝所需要的R包
BiocManager::install("patchwork")BiocManager::install("cowplot")
加載所需的R包,并將默認主題設(shè)置為theme_bw(),并將圖例放在圖的頂部
library(ggplot2)library(cowplot)library(patchwork)theme_set(theme_bw()+theme(legend.position="top"))
創(chuàng)建一些基本圖像:
library("ggplot2")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 + densbxp | dens

1.png
垂直布局
可以通過向plot_layout()來指定布局,可以定義網(wǎng)格的尺寸以及要分配給不同行和列的空間
bxp + dens + plot_layout(ncol = 1)

2.png
指定每個圖的尺寸
bxp+dens+plot_layout(ncol=1,heights=c(1,3))bxp+dens+plot_layout(ncol=2,width=c(1,2))

3.png

4.png
增加圖片之間的空間
bxp + plot_spacer() + dens

5.png
嵌套布局
您可以通過將部分圖用括號括起來來制作嵌套圖版面
lp + {? dens + {? ? bxp +? ? dp +? ? plot_layout(ncol = 1)? }} +? plot_layout(ncol = 1)

6.png
高級功能
bxp + dp - lp + plot_layout(ncol = 1)

7.png
patchwork同時提供|和/分別用于水平和垂直布局
(bxp|lp|dp)/dens

8.png
可以使用&或 * 向所有子圖添加元素,而不必單獨修改所有圖。兩者的不同之處 * 僅在于將影響當前嵌套級別上的圖:
(bxp + (dp + dens) + lp + plot_layout(ncol = 1)) * theme_gray()

9.png
而&將遞歸到嵌套級別:
(bxp + (dp + dens) + lp + plot_layout(ncol = 1)) & theme_gray()

10.png
添加標記
patchwork <- (bxp + (dp + dens) + lp + plot_layout(ncol = 1)) & theme_gray()patchwork + plot_annotation(tag_levels = 'A')

11.png
library("cowplot")plot_grid(bxp,dp,dens,labels=c("A","B","C"),ncol=2,nrow=2)

12.png
合并相同圖例(guides = 'collect')
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')p1a<-ggplot(mtcars)+geom_point(aes(mpg,disp,colour=mpg,size=wt))+ggtitle('Plot 1a')
p1a | (p2 / p3)

R1.png
(p1a | (p2 / p3)) + plot_layout(guides = 'collect')

R.png
作者:R語言數(shù)據(jù)分析指南
鏈接:http://m.itdecent.cn/p/f397eea4a3ee
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
https://blog.csdn.net/weixin_42350411/article/details/113316500