![](/img/trans.png)
[英]How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R?
[英]How to add one common y and x label for an arrange of plots and also a label for several columns of this arrange in R?
我安排了 16 个地块(4x4)。 此排列的最后一列是图例,每行通用。 我在下面添加了一个假代码来创建类似于我所拥有的东西:
library(ggplot2)
library(cowplot)
library(ggpubr)
theme_set(theme_cowplot())
df1 <- data.frame(x = 1:12, y = (1:12)^2)
df1$grp = c('A', 'B', 'C')
df2 <- data.frame(x = 1:12, y = (1:12)^2)
df2$grp = c('D', 'E')
df3 <- data.frame(x = 1:12, y = (1:12)^2)
df3$grp = c('F', 'G', 'H','I')
df4 <- data.frame(x = 1:12, y = (1:12)^2)
df4$grp = c('J', 'K')
p1 <- ggplot(df1, aes(x, y, color=grp)) + geom_point()
leg1 <- get_legend(p1)
leg1 <- as_ggplot(leg1)
p1 = p1 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())
p2 <- ggplot(df2, aes(x, y, color=grp)) + geom_point()
leg2 <- get_legend(p2)
leg2 <- as_ggplot(leg2)
p2 = p2 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())
p3 <- ggplot(df3, aes(x, y, color=grp)) + geom_point()
leg3 <- get_legend(p3)
leg3 <- as_ggplot(leg3)
p3 = p3 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())
p4 <- ggplot(df4, aes(x, y, color=grp)) + geom_point()
leg4 <- get_legend(p4)
leg4 <- as_ggplot(leg4)
p4 = p4 + theme(legend.position = "none",axis.title.x =element_blank(),axis.title.y =element_blank())
plot_grid(
p1, p1, p1, leg1,
p2, p2, p2, leg2,
p3, p3, p3, leg3,
p4, p4, p4, leg4,
ncol = 4, rel_widths=c(2,2,2,0.5),align = "hv"
)
所有的图共享相同的 Y 轴和 X 轴单位,所以我想为 x 轴( X
)创建一个 label ,为 y 轴( y
)创建一个 label 。 此外,我还想为我的排列的前三列(从左到右)添加一个 label(第一列: Column1
,第二列: Column2
,第三列: Column3
)。
有谁知道该怎么做?
您可以使用textGrob
:
library(gridExtra)
library(grid)
plot <- plot_grid(
p1, p1, p1, leg1,
p2, p2, p2, leg2,
p3, p3, p3, leg3,
p4, p4, p4, leg4,
ncol = 4, rel_widths=c(2,2,2,0.5),align = "hv"
)
y.grob <- textGrob("Common Y",
gp=gpar(fontface="bold", col="blue", fontsize=15), rot=90)
x.grob <- textGrob("Common X",
gp=gpar(fontface="bold", col="blue", fontsize=15))
grid.arrange(arrangeGrob(plot, left = y.grob, bottom = x.grob))
Output:
我找到了使用来自cowplot
package 的add_sub
的解决方案。 然而,我不得不使用不同的参数来获得我想要的东西,我不太明白为什么有些参数会做他们做的事情。 所以这个解决方案很棘手,除非你非常了解 function,否则你需要一些时间才能得到你想要的东西。
这是我使用的代码:
P <-plot_grid(
p1, p1, p1, leg1,
p2, p2, p2, leg2,
p3, p3, p3, leg3,
p4, p4, p4, leg4,
ncol = 4, rel_widths=c(2,2,2,0.5),align = "hv"
) + theme(plot.margin = margin(55, 10, 30, 30)) # I added margins to the arrange
P1 <- add_sub(P, "x axis text", x=0.5, y=-0.5, vpadding = grid::unit(0, "lines"))
ggdraw(P1)
P2 <- add_sub(P1, "y axis text", x=-0.02, y=-23, vpadding = grid::unit(-5, "lines"), angle = 90)
ggdraw(P2)
P3 <- add_sub(P2, "row 1", x=0.17, y=45, vpadding = grid::unit(0, "lines"),vjust = 1)
ggdraw(P3)
P4 <- add_sub(P3, "row 2", x=0.47, y=46, vpadding = grid::unit(0, "lines"),vjust = 1)
ggdraw(P4)
P5 <- add_sub(P4, "row 3", x=0.81, y=-25.4, vpadding = grid::unit(-2, "lines"),vjust = 1)
ggdraw(P5)
奇怪的是,我花了一段时间才意识到环境中的 plot 看起来不像您保存的 plot。 看看我在运行上述代码时在 Rstudio 中看到的内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.