结合 grid.arrange 删除两个 ggplots 之间的所有空间

标签 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)是解决方案,但无法弄清楚。

最佳答案

您应该提供 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

关于结合 grid.arrange 删除两个 ggplots 之间的所有空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556068/

相关文章:

r - 当美学填充基于两个因素的相互作用时更改堆叠条形顺序

r - 如何对齐不同高度的 Axis 标签?

r - ggplot错误: cannot coerce class “c(” gg“,”ggplot“)” to a data.frame

r - dplyr:如何对group_by的结果应用do()?

r - 如何在 kable pdf 输出中跨行分割整个单词以防止文本与下一个单元格重叠

r - 带有多个回归线的 ggplot 以显示随机效应

r - 如何解决 'ymax not defined'?

r - 添加空格/增加 ggplot 中 X 轴和 Y 轴之间的间距

r - 使用美国县级数据创建 Choropleth map

r - 在每个组的分类 x 轴内叠加 geom_line - ggplot2