删除两个ggplots之间的所有空间以及grid.arrange

时间:2013-03-21 19:07:21

标签: r ggplot2

我想在主题之间插入两个没有任何空格的图(因此它们共用一个轴)。

假设:

p1 <- qplot(1,1,xlab="")

p1 <- p1 +
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,0,1), "cm"),
        panel.margin=unit(c(1,1,0,1), "cm"))
p2 <- qplot(1,2)

grid.arrange(p1,p2)

产生:

enter image description here

我想消除两个地块之间的空白区域。

我有调整高度的印象,正如宽度所做的那样:left align two graph edges (ggplot)是解决方案,但无法弄明白。

2 个答案:

答案 0 :(得分:31)

您应为两个图提供plot.margin,并为p1的下边距和p2的上边距设置负值。这将确保两个绘图连接。

p1 <-  qplot(1,1,xlab="")+
  theme(legend.position="none",
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin=unit(c(1,1,-0.5,1), "cm"))
p2 <- qplot(1,2)+
  theme(legend.position="none",
        plot.margin=unit(c(-0.5,1,1,1), "cm"))


grid.arrange(p1,p2)

enter image description here

答案 1 :(得分:1)

+ labs(x=NULL)

OR

+ labs(x=NULL, y=NULL)

在使用grid.arrange之前删除图(p1,p2)周围的左边距和下边距

p1 <- qplot(1,1)+
 theme_bw() +
 theme(axis.text.x=element_blank(),
 axis.ticks.x=element_blank(),
 plot.margin = rep(unit(0,"null"),4),
 panel.margin = unit(0,"null"),
 axis.ticks.length = unit(0,"null"),
 axis.ticks.margin = unit(0,"null")) +
 labs(x=NULL)
p2 <- qplot(1,2)+
 theme_bw() +
 theme(
 plot.margin = rep(unit(0,"null"),4),
 panel.margin = unit(0,"null"),
 axis.ticks.length = unit(0,"null"),
 axis.ticks.margin = unit(0,"null"))

grid.arrange(p1,p2)