20210830
iris數(shù)據(jù)
data(iris)
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
以下幾個(gè)函數(shù)可以實(shí)現(xiàn)圖片排版,各有特色
1)facet_grid()
# 這個(gè)比較簡(jiǎn)單,不再贅述
ggplot(iris, aes(Petal.Width, Sepal.Length))+
geom_point(size = 4)+
facet_grid(~Species)
Note: 不知是否注意到x軸卓舵,3個(gè)分面的坐標(biāo)軸區(qū)間一致励幼,然后各個(gè)plot有很大留白,目前還不知道這里怎么解決(ps:對(duì)于強(qiáng)迫癥來(lái)說(shuō)菊碟,這個(gè)不能忍)
2)layout()
# matrix定義3個(gè)圖片位置, 2*2網(wǎng)格在刺,第一列一分為二(上下各一)
layout.show(3)
layout(matrix(c(1,2,3,3),2,2,byrow=FALSE),widths=c(1,1),height=c(1,1))
plot(iris$Sepal.Length, iris$Sepal.Width, pch = 19, cex=0.8, cex.axis=0.8, cex.lab=1.2)
plot(iris$Sepal.Length, iris$Petal.Length, pch = 19, cex=0.8, cex.axis=0.8, cex.lab=1.2)
plot(iris$Sepal.Length, iris$Petal.Width, pch = 19, cex=0.8, cex.axis=0.8, cex.lab=1.2)
dev.off()
Note:layout函數(shù)只能組合R內(nèi)置函數(shù)所出的圖逆害,ggplot出圖不能用此函數(shù)排版
3)pushViewport
pushViewport(viewport(layout = grid.layout(2, 2)))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(p1, vp = vplayout(1, 1))
print(p2, vp = vplayout(1, 2))
print(p3, vp = vplayout(2, 1))
print(p4, vp = vplayout(2, 2))
Note: 坐標(biāo)軸未對(duì)齊,無(wú)標(biāo)簽
4)grid.arrange
# library(grid)
# library(gridExtra)
p1 = ggplot(iris, aes(Petal.Width, Sepal.Length))+geom_point(size = 4)
p2 = ggplot(iris, aes(Petal.Width, Sepal.Width))+geom_point(size = 4)
p3 = ggplot(iris, aes(Petal.Length, Sepal.Width))+geom_point(size = 4)
p4 = ggplot(iris, aes(Petal.Length, Petal.Width))+geom_point(size = 4)
lay <- rbind(c(1,2),
c(3,4)) # 排版同上
grid.arrange(p1, p2, p3, p4, layout_matrix = lay)
Note: 僅邊框?qū)R蚣驼,而坐標(biāo)軸未對(duì)齊(第一列Y軸錯(cuò)開(kāi))魄幕,并且上述函數(shù)貌似不能添加標(biāo)簽(a, b, c....)
5)ggarrange()
# library(ggpubr)
ggarrange(p1, p2, p3, p4,
ncol=2,
nrow=2,
labels=LETTERS[1:4])
# 通過(guò)參數(shù) common.legend = TRUE 設(shè)置共同圖例
Note: 可加標(biāo)簽,坐標(biāo)軸未對(duì)齊
6)plot_grid()
# library(cowplot)
plot_grid(p1,p2,p2,p4,
ncol = 2, labels=c("a","b","c","d"),
axis = c( "tblr"), align = "hv")
Note: 可加標(biāo)簽颖杏,可對(duì)齊梅垄,nice。就是common legend比較麻煩
圖例添加输玷,參考簡(jiǎn)書 [名本無(wú)名] 文章
“R 數(shù)據(jù)可視化 —— 圖形排列之 cowplot”
# 提取圖例
legend <- get_legend(
plot[[1]] + guides(color = guide_legend(nrow = 1)) +
theme(legend.position="bottom",
legend.key.size = unit(20, "pt"),
legend.text = element_text(size = 10)
)
)
# 將圖例作為第i個(gè)圖片(即第9個(gè))队丝,然后設(shè)置其高度比例0.1
# 導(dǎo)出pdf,用AI調(diào)整其位置居中
plot_grid(plot[[1]],plot[[2]],plot[[3]],plot[[4]],
plot[[5]],plot[[6]],plot[[7]],plot[[8]],
legend, rel_heights = c(1, 1, 1, 1, 0.1),
ncol = 2, labels=c("a","b","c","d","e","f","g","h"),
axis = c( "tblr"), align = "hv")
上述各個(gè)函數(shù)欲鹏,只有plot_grid提供了坐標(biāo)軸對(duì)齊机久,但是common legend的添加也是比較麻煩;而ggarrange的label赔嚎, legend 設(shè)置比較方便膘盖,但是y軸區(qū)間不一致,坐標(biāo)軸就很難對(duì)齊; 其余函數(shù)對(duì)label都不支持尤误。侠畔。。
Refs:
http://www.reibang.com/p/8bd1808c8ce7
https://zhuanlan.zhihu.com/p/161401082
更新 20210831
分面坐標(biāo)軸區(qū)間處理
# 設(shè)置scales 參數(shù)
# ref https://blog.csdn.net/weixin_39950867/article/details/109920012
facet_grid(var1 ~ var2, scales = "free_x")
更新 20210912
今天偶然發(fā)現(xiàn) ggarrange()函數(shù)下有一個(gè) align參數(shù)损晤,查了幫助文章软棺,也是對(duì)齊坐標(biāo)軸的
Nice !!!!
ps:要多看幫助文檔啊S妊喘落!
#align
# (optional) Specifies whether graphs in the grid should be horizontally ("h") or vertically ("v") aligned.
# Options are "none" (default), "hv" (align in both directions), "h", and "v".
ggpubr::ggarrange(a, b, ncol = 1, nrow = 2, labels = letters[1:2], common.legend = TRUE,
legend = "bottom", align = "hv")
更新 20221212
一直頭疼plot_layout函數(shù)的圖例問(wèn)題茵宪,無(wú)論在top/bottom/right/left,圖例都會(huì)影響你設(shè)定好的排版比例瘦棋,今天偶然發(fā)現(xiàn)稀火,plot_layout可以在一側(cè)放置所有的圖例,這個(gè)就比較方便了赌朋。感謝簡(jiǎn)書“迷途小書童”凰狞,下面ref是其簡(jiǎn)書blog地址。做個(gè)一個(gè)案例圖沛慢,作為大家參考
plot_layout(guides = 'collect')
ref:http://www.reibang.com/p/73057774b4cb
更新20230722
今天作圖赡若,突然覺(jué)得每個(gè)分面大小都是一樣的,但是每個(gè)分面的元素個(gè)數(shù)不一樣颠焦。這樣看著很別扭(如下圖)
仔細(xì)查閱了“facet_grid”的各個(gè)參數(shù),發(fā)現(xiàn)一個(gè)“space”的參數(shù)往枣,R幫助文件這樣解釋伐庭,
If "fixed", the default, all panels have the same size. If "free_y" their height will be proportional to the length of the y scale; if "free_x" their width will be proportional to the length of the x scale; or if "free" both height and width will vary. This setting has no effect unless the appropriate scales also vary.
完美解決facet分面的比例問(wèn)題。
facet_grid(tp~.,scales = "free_y", space ="free_y")