如何使用plot_grid放置没有任何空间的绘图?

时间:2017-12-03 01:13:27

标签: r ggplot2 r-grid cowplot

我正在安排2x2地块。这些图共享相同的轴,所以我想将它们放在一起,例如

此代码:

library(ggplot2)
library(cowplot)

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)


plot <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme(aspect.ratio = 0.5)

plot_grid(plot, plot, plot, plot, align = "hv", ncol = 2)

产生

enter image description here

但我喜欢这样的事情:

enter image description here

我如何获得类似的结果?

2 个答案:

答案 0 :(得分:3)

我认为这是ggarrange()包中的egg函数的情况。使用plot_grid()执行此操作需要无休止的摆弄,并且不值得。

(技术原因是plot_grid()保持网格中每个地块的总面积不变,但如果某些地块有x轴而其他地块没有,那么它们占据不同的区域。人们可以尝试通过使用rel_heights参数来避免这种情况,但没有好方法来计算rel_heights的正确值,因此这将是反复试验。相比之下,ggarrange()分别查看了绘图面板和周围的元素,并确保绘图面板具有相同的大小。)

以下是使用ggarrange()的代码:

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)


pbase <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_bw()

ptopleft <- pbase +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 0, 0, 5.5),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

ptopright <- pbase +
  scale_y_continuous(position = "right") +
  scale_x_continuous(position = "top") +
  theme(plot.margin = margin(5.5, 5.5, 0, 0),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

pbottomleft <- pbase +
  theme(plot.margin = margin(0, 0, 5.5, 5.5))

pbottomright <- pbase +
  scale_y_continuous(position = "right") +
  theme(plot.margin = margin(0, 5.5, 5.5, 0))

library(egg)      
ggarrange(ptopleft, ptopright,
          pbottomleft, pbottomright,
          ncol = 2)

enter image description here

两条评论:

  1. 要删除顶部图上绘图面板下方的最后一些空格,我们需要将x轴移动到顶部,即使我们没有显示它。这是主题机制的一个奇怪的限制。我们不能完全摆脱一个轴。

  2. 我不是共享轴标题的忠实粉丝,就像你的例子一样。我认为每个轴都应该有一个标题。如果您想要共享轴标题,为什么不使用分面机制?

答案 1 :(得分:2)

您可以为每个情节设置微妙的plot.margin,然后grid.arrange并添加实验。

library(ggplot2)
library(grid)
library(gridExtra)

Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)

plot1 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.text.x = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(5.5, 5.8, -50, 5.5), "pt")) 

plot2 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.text.x = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(5.5, 5.5, -50, 5.5), "pt")) +
  scale_y_continuous(position = "right")

plot3 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(-50, 5.8, -50, 5.5), "pt")) 

plot4 <- ggplot(DF, aes(x = Index, y = Value)) +
  geom_line(linetype = 2) +
  theme_minimal() +
  theme(aspect.ratio = 0.5, 
    panel.border = element_rect(fill = NA),
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.margin = unit(c(-50, 5.5, -50, 5.5), "pt")) +
  scale_y_continuous(position = "right")

grid.arrange(grobs = list(plot1, plot2, plot3, plot4), ncol = 2, bottom = 'Index', left = 'Value', right = 'Value')

最终情节 enter image description here