3

你好 R 和 ggplot2 社区!

我想水平对齐两个图,用ggplot2. 一个有刻面(和刻面条!)和图例,但第二个没有。它们共享相同的 Y 轴,我想对齐。我可以使用它们的 grobs 宽度垂直对齐图,但我无法用高度来计算它。

这是一段代码来支持我的问题。

library( ggplot2 )
library( gridExtra )

plot_1 <- ggplot() +
  geom_point(
    data = iris,
    aes(
      x = Petal.Length,
      y = Petal.Width,
      colour = Sepal.Length 
      ) 
    ) +
  facet_grid(
    . ~ Species 
    ) +
  theme(
    legend.position = "bottom"
    )

plot_2 <- ggplot() +
  geom_point(
    data = iris,
    aes(
      x = Sepal.Width,
      y = Petal.Width
      )
    )

g1 <- ggplotGrob( plot_1 )
g2 <- ggplotGrob( plot_2 )

# Here, how to manipulate grobs to get plot_2 scaled to the plot_1's Y axis? (ie, with an empty area right of the plot_1's legend)

grid.arrange( g1, g2, ncol = 2 )

你以前知道如何操纵 grobs 的高度grid.arrange()吗?(欢迎任何其他建议!)此外,如何将总面积的三分之二分配给plot_1

另请注意,我的真实情节实际上具有 a coord_flip(),但我认为它与这个问题无关。

谢谢你的帮助!

4

1 回答 1

1

一个简单的解决方案是创建一个空白面板:

blank<-grid.rect(gp=gpar(col="white"))

然后使用 grid.arrange 创建一个两列网格,其中第二列实际上由三行组成。

grid.arrange(g1, arrangeGrob(blank,g2,blank,ncol=1,
heights=c(0.2,0.9,0.2)),ncol=2) 

玩弄这个heights论点应该为我们提供所需的结果。

关于您的其他问题,theheightswidths论点在grid.arrange那里也应该证明非常有用。

于 2015-03-23T14:58:31.313 回答