【问题标题】:Title background with grid.arrange() of ggplot objects带有 ggplot 对象的 grid.arrange() 的标题背景
【发布时间】:2016-09-21 14:25:24
【问题描述】:

我用 ggplot2 制作了几个简单的图,并将它们与grid.arrange()(来自包 grid.extra)分组,以将这些图连接到一个图中。 我通过grid.arrange() 函数和参数top 创建标题,如下所示:

library(ggplot2)
library(gridExtra)

...

tiff("Figure5.tiff",
     height = 20, width = 16, units = "cm", 
     compression = "lzw", res = 300)
Fig5 <- grid.arrange(plot5, plot5Ran, ncol = 2,
                     top = textGrob("Comparison of Correlation Output",
                                  gp = gpar(fill='snow')))

plot(Fig5)
dev.off()

但是,正如您在上面的部分屏幕截图中看到的那样,标题的背景(图中顶部的图像部分,由参数top 添加,从 ggplot 主题获取背景,而不是像边缘一样的白色。 我试过使用参数fill,但没有效果。

如何在白色背景上显示标题? 我知道使用facets 会更直接并且避免grid.arrange,但在这种情况下它没有意义。我想我也可以将ggplot 主题更改为白色,但在这种情况下这不是一个选项。 谢谢

【问题讨论】:

  • 如果您提供一些可重现的数据,我们会更容易使用它来帮助您
  • 如果您提供生成 plot5 和 plot5Ran 的实际代码也会有所帮助
  • 另外,top=) 这似乎不正确...可能与您的问题无关...另外,使用虚构的数据(虹膜)我无法重现您的问题.如果您发布您的sessionInfo 也会有所帮助。有关提供可重现示例的更多帮助,请参阅here
  • 感谢您的提示。 @user20650 是正确的,将 plot(Fig5) 替换为 Fig5 解决了这个问题。
  • grid.arrange() 直接绘制,不需要 print() 它,或者更糟糕的是 plot() 它(gtable::plot.gtable 是你看到奇怪背景的原因——它显然是为了调试目的)

标签: r ggplot2


【解决方案1】:

正如 user20650 所建议的,这是一个打印问题。 下面的两个选项都有效。

首先将图形保存为R对象和tiff文件:

tiff("Figure5X.tiff",
     height = 20, width = 16, units = "cm", 
     compression = "lzw", res = 300)
Fig5 <- grid.arrange(plot5, plot5Ran, ncol = 2,
                     top = "Comparison of Correlation Output")
Fig5
dev.off()

或者,按照 user20650 的建议,仅保存 tiff 文件:

tiff("Figure5Y.tiff",
     height = 20, width = 16, units = "cm", 
     compression = "lzw", res = 300)
     grid.arrange(plot5, plot5Ran, ncol = 2,
                     top = "Comparison of Correlation Output")
dev.off()

【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多