【问题标题】:R graphics: How to make objects from VennDiagram compatible with cowplot plot_grid?R图形:如何使VennDiagram中的对象与cowplot plot_grid兼容?
【发布时间】:2018-06-22 00:42:20
【问题描述】:

我正在使用 VennDiagram 包制作维恩图。他们出来了:

library(VennDiagram)
library(cowplot)


png("p.png")
p = draw.pairwise.venn(30,20,11, category = c("Dogs", "Cats"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))
dev.off()

png("q.png")
draw.pairwise.venn(15, 20, 10, category = c("Rats", "Mice"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))
dev.off()    

但如果我尝试使用 cowplot 的 plot_grid() 将它们并排绘制,就会发生不好的事情:

p = draw.pairwise.venn(30,20,11, category = c("Dogs", "Cats"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))

q = draw.pairwise.venn(15, 20, 10, category = c("Rats", "Mice"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))

png('pq.png')
plot_grid(p, q, labels = "AUTO")
dev.off()

Error: Argument needs to be of class "ggplot", "gtable", "grob", "recordedplot", or a function that plots to an R graphicsdevice when called, but is a gList
Calls: plot_grid -> align_plots -> lapply -> FUN -> plot_to_gtable
Execution halted

draw.pairwise.venn() 没有使对象与plot_grid() 兼容。

class(p)
[1] "gList"

所以我想我需要将 gList 变成 ggplot 对象或其他兼容的对象,即使 gList 被列为合适的类型。我找不到任何东西。我想使用cowplot,因为它能够很好地标记要发布的子图。

【问题讨论】:

    标签: r r-grid venn-diagram cowplot grob


    【解决方案1】:

    将这些图包装成grobTree() 对我有用。我认为 VennDiagram 包应该归咎于此。它不应该返回gList,它应该将gList 包装成一个grob。无论如何,这可以在cowplot中修复。随时提出问题here.

    library(VennDiagram)
    #> Loading required package: grid
    #> Loading required package: futile.logger
    library(cowplot)
    library(grid)
    
    p = draw.pairwise.venn(30,20,11, category = c("Dogs", "Cats"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))
    
    q = draw.pairwise.venn(15, 20, 10, category = c("Rats", "Mice"), lty = rep("blank", 2), fill = c("light blue", "pink"), alpha = rep(0.5, 2), cat.pos = c(-45, 45), cat.dist = rep(0.025, 2))
    
    plot_grid(grobTree(p), grobTree(q), labels = "AUTO")
    

    reprex package (v0.2.0) 于 2018 年 6 月 23 日创建。

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2019-11-25
      • 2019-07-31
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多