定义grid.arrange()使第三个图在中间?

z9zf31ra  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(117)

我用ggplot 2绘制了三个图。我想把它们放在同一个空间里。为了把它们放在同一个空间里,我用了grid.arrange(),有2列2行。不是让第三个图在位置(2,1)在左边,有没有办法让它在空间的中间居中?也欢迎grid.arrange()之外的建议。

library(ggplot2)
library(gridExtra)

P1 <- ggplot(mtcars, aes(x = mpg)) +
  geom_histogram()

P2 <- ggplot(mtcars, aes(x = wt)) +
  geom_histogram()

P3 <- ggplot(mtcars, aes(x = qsec)) +
  geom_histogram()

grid.arrange(P1, P2, P3, ncol = 2, nrow = 2)

字符串
x1c 0d1x的数据
非常感谢您的帮助!

gpnt7bae

gpnt7bae1#

不知道gridExtra,但既然你说替代品是好的,它真的很简单拼凑:)

library(ggplot2)
library(patchwork)

P1 <- ggplot(mtcars, aes(x = mpg)) +
  geom_histogram()

P2 <- ggplot(mtcars, aes(x = wt)) +
  geom_histogram()

P3 <- ggplot(mtcars, aes(x = qsec)) +
  geom_histogram()

(P1 + P2) / P3

字符串

46qrfjad

46qrfjad2#

以下是对使用grid.arrange()感兴趣的人的答案

library(ggplot2)
library(gridExtra)

P1 <- ggplot(mtcars, aes(x = mpg)) +
  geom_histogram()

P2 <- ggplot(mtcars, aes(x = wt)) +
  geom_histogram()

P3 <- ggplot(mtcars, aes(x = qsec)) +
  geom_histogram()

grid.arrange(P1, P2, P3, ncol = 2, nrow = 2)

#This will get you 2 plots on top and 1 plot in the middle at the bottom
grid.arrange(P1, P2, P3, ncol = 2, nrow = 2, layout_matrix= rbind(c(1,2), 3))

#This will get you 1 plot in the middle at top and 2 plots at the bottom
grid.arrange(P1, P2, P3, ncol = 2, nrow = 2, layout_matrix= rbind(c(1,1),c(2,3)))

字符串

相关问题