2017-12-03 162 views
3

我正在做一个2x2图的安排。这些图共享相同的轴,所以我想把它们放在一起,例如,如何使用plot_grid放置没有任何空间的地块?

此代码:

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

我觉得这是从eggggarrange()功能的情况。这样做与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. 我不是很喜欢共享轴标题,就像你的例子。我认为每个轴应该有一个标题。如果您想共享轴标题,为什么不使用分面机制?

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

相关问题