调整 r 中 grob 对象的边距

Adjust margins of grob object in r

我使用 cowplot 包制作了一个 grob 对象。我正在将 grid.lines()grid.text() 对象添加到完成的 grob,但是当它从 cowplot 出来时它会填满整个页面。如何调整 grob 对象的边距以在边缘周围添加一些白色 space?下面的示例代码。

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()
p3 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p4 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()
plot_grid(p1, p2, p3, p4, ncol = 2 ,nrow = 2,align="hv")

您只需像在 ggplot 中那样使用 theme(plot.margin = ...)

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()
p3 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()
p4 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point()
plot_grid(p1, p2, p3, p4, ncol = 2 ,nrow = 2,align="hv")


plot_grid(p1, p2, p3, p4, ncol = 2 ,nrow = 2,align="hv") + 
  theme(plot.margin = unit(c(20,20,20,20), "points"))

reprex package (v0.3.0)

于 2020-04-29 创建