首页 文章

Headers 背景与grid.arrange()的ggplot对象

提问于
浏览
0

我用ggplot2制作了几个简单的图,并用 grid.arrange() (来自包grid.extra)将它们分组,以便在一个图中加入图 . 我通过带有参数 topgrid.arrange() 函数创建 Headers ,如下所示:

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()

Partial screenshot, background of title is same as plot, rather than white margin

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

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

1 回答

  • 2

    正如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()
    

    或者,仅保存tiff文件,如user20650所示:

    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()
    

相关问题