R 图形:如何使 VennDiagram 中的对象与 cowplot plot_grid 兼容?
R graphics: How to make objects from VennDiagram compatible with cowplot plot_grid?
我正在使用 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,因为它可以很好地标记要发布的子图。
将这些图包装成 grobTree()
对我有用。我认为 VennDiagram 包是这里的罪魁祸首。它不应该 return 一个 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-06-23。
我正在使用 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,因为它可以很好地标记要发布的子图。
将这些图包装成 grobTree()
对我有用。我认为 VennDiagram 包是这里的罪魁祸首。它不应该 return 一个 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-06-23。