小潔老師的文章 覺(jué)得好 轉(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)入門(mé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.圖例管理
走開(kāi)未舟,圖例都給我去右邊
patchwork+plot_layout(guides='collect')
一樣的圖例可以只顯示一個(gè)圈暗,但是要讓閾值一致
patchwork<-patchwork&scale_fill_continuous(limits=c(0,60))patchwork+plot_layout(guides='collect')
嗯,不是一般的香裕膀!
轉(zhuǎn)自小潔老師