【问题标题】:Vertically align of plots of different heights using cowplot::plot_grid() when using coord_equal()使用 coord_equal() 时使用 cowplot::plot_grid() 垂直对齐不同高度的图
【发布时间】:2018-08-02 02:45:25
【问题描述】:

我正在尝试使用 cowplot::plot_grid() 组合两个 ggplot 对象并垂直对齐它们。这通常使用align = "v" 非常简单。

dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")

但是,当 ggplots 使用 coord_equal() 时,此方法会失败,因为在强制纵横比时 plot_grid() 无法修改轴。相反,默认设置是保持每个绘图的高度相同。

plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v")

我可以通过使用 rel_heights 参数来强制我的目标,但这不是一个可行的解决方案,因为我有很多动态的情节要构建。这里y轴是对齐的,所有轴的坐标还是相等的。

cowplot::plot_grid(plot1, plot2, ncol = 1, align = "v", rel_heights = c(2, 1.07))

我已经看到许多使用ggplot2::ggplotGrob()grid::grid_draw() 来解决类似问题的方法,但是当使用coord_equal() 时,没有什么能完全解决这个问题。也许最好的解决方案根本不使用cowplot::plot_grid(),或者解决方案可能以某种方式动态确定并将正确的值传递给rel_heights。我想我更喜欢后面的选项,以便能够轻松使用 cowplot::plot_grid() 附带的其他功能。或许可以在this related approach中找到一些有用的灵感。

【问题讨论】:

  • egg::ggarrange(plot1, plot2) 你想要的那种东西?
  • 我试过egg::ggarrange(plot1, plot2, ncol = 1),它确实做了垂直对齐,但是y轴坐标不保持等于x轴的坐标。 y 轴在顶部图中被拉伸了一点。另外,我似乎在 github 上找不到 egg - 担心它可能不再维护了。
  • 很遗憾,但也许该包中还有其他功能可以提供帮助(有点奇怪,它不在 github 上,而是在 it is on cran 上)
  • 并不是所有的软件都是在 github 上开发的。 egg的维护者不在github上开发,但是这个库目前维护的很好。
  • @ClausWilke ;有点奇怪的是 baptiste/egg 正在 github 上开发(并且有很多关于从 github 安装它的答案),但它已经从 github 中删除了。嗯 gridExtra 也被删除了,所以似乎改变了方法

标签: r ggplot2 gtable cowplot


【解决方案1】:

默认情况下,轴的范围实际上会超出 ggplot 中的限制。函数scale_continuous/discrete() 中的expand 参数用于设置扩展。如scale_continuous() 文档中所述:

一个长度为 2 的数值向量,给出乘法和加法展开 常数。这些常数确保数据放置在远离轴的位置。连续变量的默认值为 c(0.05, 0),离散变量的默认值为 c(0, 0.6)。

library(ggplot2)
dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()

拳头,我们可以计算这两个地块的实际高度,this post 解释了expand 参数的工作原理。

# The defaults are c(0.05, 0) for your continuous variables
limity1 <- max(dat1$y) - min(dat1$y)
y1 <- limity1 + 2 * limity1 * 0.05
limity2 <- max(dat2$y) - min(dat2$y)
y2 <- limity2 + 2 * limity2 * 0.05

然后,使用patchwork 组成这两个图

library(patchwork)
#  actual heights of plots was used to set the heights argment
plot1 + plot2 + plot_layout(ncol = 1, heights = c(y1, y2))

【讨论】:

    【解决方案2】:

    cowplot::plot_grid() 的作者在这里。当您尝试将绘图与指定的纵横比对齐时它不起作用,这是您在使用 coord_equal() 时生成的。解决方案是使用 egg 库或 patchwork 库。 Patchwork 仍在开发中,但应该很快就会发布到 CRAN。同时,您可以从 github 安装。

    这是一个使用鸡蛋的解决方案。在我看来,它工作得很好。

    library(ggplot2)
    library(egg)
    
    dat1 <- data.frame(x = rep(1:10, 2), y = 1:20)
    dat2 <- data.frame(x = 1:10, y = 1:10)
    plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
    plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
    ggarrange(plot1, plot2, ncol = 1)
    

    我看到的两个小问题是 (1) 两个 y 轴的轴刻度不同,这使得间距看起来不同,以及 (2) 轴扩展到不同的限制。您可以通过手动设置刻度和扩展来解决这两种情况。

    plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + 
      scale_y_continuous(limits = c(0, 21), breaks = 5*(0:4), expand = c(0, 0)) +
      coord_equal()
    plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + 
      scale_y_continuous(limits = c(0, 11), breaks = 5*(0:4), expand = c(0, 0)) +
      coord_equal()
    ggarrange(plot1, plot2, ncol = 1)
    

    【讨论】:

    • 克劳斯,感谢您重申 egg::ggarrange() 确实有效。正如您所指出的,之前欺骗我认为轴仍然不太正确的是它们的扩展方式不同。 @user20650,很抱歉我错过了这个并怀疑你的建议!但是,当包含 faceting 时, egg::ggarrange() 解决方案会崩溃。有关如何将其概括为刻面的任何建议?我已经更新了原始问题以反映这一点。
    • Kevin,请不要通过编辑来不断增加这个问题的复杂性。最好在这里接受其中一个答案作为原始问题的正确答案,然后针对更复杂的情况(带有分面)发布一个新问题。已经回答的人不会想追逐移动的目标,而尚未看到问题的人也不太可能看到编辑。
    • 好的,知道了。新问题在这里:stackoverflow.com/questions/48961927/…
    猜你喜欢
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2014-03-30
    • 2021-09-06
    相关资源
    最近更新 更多