patchwork是真的香

小潔老師的文章 覺得好 轉(zhuǎn)過(guò)來(lái)看看

簡(jiǎn)介

tidyverse可以說(shuō)是追求代碼“易讀”的典范,今天介紹的拼圖包名為“patchwork”,作者是tidyverse團(tuán)隊(duì)成員--Thomas Lin Pedersen,昨天(2019年12月1日)剛剛登上CRAN,新鮮熱乎!這樣的熱點(diǎn)我是很愿意追的,八卦什么的就算啦,雖然能帶來(lái)閱讀量,可我學(xué)不到東西。

長(zhǎng)得帥的大佬可以擁有寄幾的大照,雖然是黑白的:

(不能繼續(xù)夸了,再夸晚飯就都被豆豆吃了)

博客地址:https://www.data-imaginist.com/2019/patch-it-up-and-send-it-out/

patchwork包官方教程https://patchwork.data-imaginist.com/index.html

總結(jié)一下他的功能和優(yōu)點(diǎn)

(1)支持直接p1+p2拼圖,比任何一個(gè)包都簡(jiǎn)單

(2)復(fù)雜的布局代碼易讀性更強(qiáng)

(3)可以給子圖添加標(biāo)記(例如ABCD, I II III IV 這樣)

(4)可以統(tǒng)一修改所有子圖

(5)可以將子圖的圖例移到一起,整體性特別好

簡(jiǎn)單好用,功能強(qiáng)大,真香!

1.極簡(jiǎn)入門-拼兩張圖

橫著就"+",豎著就"/"

library(ggplot2)library(patchwork)p1<-ggplot(mpg)+geom_point(aes(hwy,displ))p2<-ggplot(mpg)+geom_bar(aes(manufacturer,fill=stat(count)))+coord_flip()#就是這么優(yōu)秀:p1+p2

p1/p2

2.多幾張圖

p3<-ggplot(mpg)+geom_smooth(aes(hwy,cty))+facet_wrap(~year)p1+p2+p3#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p4<-ggplot(mpg)+geom_tile(aes(factor(cyl),drv,fill=stat(count)),stat='bin2d')p1+p2+p3+p4#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

繼承了矩陣的兩個(gè)參數(shù)”nrow“和"byrow"

p1+p2+p3+p4+plot_layout(nrow=4)#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

再難一點(diǎn)也不在話下

(p1|p2)/p3#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p1|(p2/p3)#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

(p1|(p2/p3))+plot_annotation(title='The surprising story about mtcars')#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

3.可以再?gòu)?fù)雜一點(diǎn)

大招:layout不用坐標(biāo),不用寬高比例,直接用ABCD就行

layout<-'ABBCCD'p1+p2+p3+p4+plot_layout(design=layout)#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

4.給子圖添加標(biāo)記

啥也不說(shuō)了,大佬怎么就這么優(yōu)秀呢?

p1+p2+p3+plot_annotation(tag_levels='I')#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

patchwork<-(p4|p2)/p1patchwork+plot_annotation(tag_levels='A')

嵌套式的子標(biāo)題也能搞定

patchwork<-((p4|p2)+plot_layout(tag_level='new'))/p1patchwork+plot_annotation(tag_levels=c('A','1'))

5.統(tǒng)一修改所有子圖

用”&“符號(hào),簡(jiǎn)單的很

patchwork&theme_minimal()

6.圖例管理

走開,圖例都給我去右邊

patchwork+plot_layout(guides='collect')

一樣的圖例可以只顯示一個(gè),但是要讓閾值一致

patchwork<-patchwork&scale_fill_continuous(limits=c(0,60))patchwork+plot_layout(guides='collect')

嗯,不是一般的香!

轉(zhuǎn)自小潔老師

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。