r - 带有内部标签 r 的优化 VennDiagram

标签 r venn-diagram

我正在尝试以优化的方式(见下文)绘制维恩图,并将案例作为内部标签(而不是每个交叉点的案例数)。我知道他们每个人都有帖子,但没有一个解决方案允许我同时做这两件事。

我有这个:

x <- list()
x$A <- as.character(c("Per_36","Cent","CeM","vDG","LAVL","RSGd"))
x$B <- as.character(c("vCA1","DLE","Per_36","vDG","DIE","Per_35"))
x$C <- as.character(c("vCA1","Cg1","LAVL", "RSGc", "RSGd","Per_35","Per_36"))
x$D <- as.character(c("Por","Cg1","RSGc","LAVL","Per_35","RSGd","Per_36"))


require(VennDiagram)
v0 <-venn.diagram(x, lwd = 3, col = c("red", "green", "orange", "blue"),
                fill = c("red", "blue", "green", "orange"), apha = 0.5, filename = NULL)
grid.draw(v0)
overlaps <- calculate.overlap(x)
overlaps <- rev(overlaps)
for (i in 1:length(overlaps)){
  v0[[i+8]]$label <- paste(overlaps[[i]], collapse = "\n")
}

grid.newpage()
grid.draw(v0)

我得到以下输出:

enter image description here

关于维恩图的组织方式我想这样做:

c <- venn(x, simplify = TRUE, small = 0.5, intersections = TRUE)

enter image description here

这是我从 gplots() 包中使用 venn 函数和 simplify = TRUE 获得的。但是,在 venn 函数中,我似乎无法用标签名称替换计数。我使用了 intersections = TRUE,根据参数的描述,它应该起作用,但它不起作用(尽管如果我查看变量 c,信息就在那里)。

    Logical flag indicating if the returned object should have the attribute 
"individuals.in.intersections" featuring for every set a list of individuals
 that are assigned to it.

问题:使用 VennDiagram 包,有没有办法做与 venn 函数中的 simplify 参数完全相同的事情 gplots 包?

问题 2:使用 gplots 包中的 venn 函数,有没有办法显示每个元素的名称而不是元素计数?就像我在“venn.diagram”函数中所做的那样?

提前致谢

最佳答案

这是我的方法,到目前为止还没有解决方案,而是一种 hack。

# Print a venn and save it to an object
a <- venn(list(letters[1:5], letters[3:8]))
# save the intersections
b <- attr(a, "intersections")

# find the coordinates
s <- seq(0,500,100); abline(h=s); text(s, y=s, x=0)
s <- seq(0,500,50);  abline(v=s); text(s, y=0, x=s)

enter image description here

# the hack, destroy the venn to avoid the plotting of the internal numbers 
rownames(a) <- letters[1:nrow(a)]
a
plot.venn(a)
>Error in data[n, 1] : subscript out of bounds

# include the internal labels
text(200,300,paste(b$`01`,collapse = "\n"))
text(200,200,paste(b$`11`,collapse = "\n"))
text(200,100,paste(b$`10`,collapse = "\n"))

enter image description here

多个 venns 很烦人。否则,您可以将 venn 保存为 .svg 并使用 inkscape 或类似软件对其进行编辑,或者通过电子邮件询问开发人员。

编辑: 如果您的绘图看起来总是一样,您可以检查维恩函数的源代码(在 RStudio 中按 F2)并复制粘贴 4 和 5 圆维恩的位置并替换标签函数 lab("1000", data) 加上你想要的标签。

对于 4 个圆圈:

      text(35, 250, lab("1000", data))
      text(140, 315, lab("0100", data))
      text(260, 315, lab("0010", data))
      text(365, 250, lab("0001", data))
      text(90, 280, lab("1100", data), cex = small)
      text(95, 110, lab("1010", data))
      text(200, 50, lab("1001", data), cex = small)
      text(200, 290, lab("0110", data))
      text(300, 110, lab("0101", data))
      text(310, 280, lab("0011", data), cex = small)
      text(130, 230, lab("1110", data))
      text(245, 75, lab("1101", data), cex = small)
      text(155, 75, lab("1011", data), cex = small)
      text(270, 230, lab("0111", data))
      text(200, 150, lab("1111", data))

编辑

现在我会改用 ggplot 解决方案

ggVennDiagram::ggVennDiagram(x)

enter image description here

关于r - 带有内部标签 r 的优化 VennDiagram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35457611/

相关文章:

r - 迭代 S4 对象槽 Rcpp

r - 在维恩图的每个圆圈内显示元素,而不仅仅是计数?

r - 维恩图中标签位置、形状和重叠颜色的问题

r - `venneuler` 中的维恩图 `R` : delete the name of the set from the plot and add elements name

r - 重叠两个基因集,找到它们的重叠意义并绘制它们

r - ggplot facet_wrap 不同的主题

r - 无需Sweave即可将标题页写入pdf的最简单方法

按大因子缩放大数和按小因子缩放小数的算法

r - 防止 R 中的函数覆盖

r - 添加带有列名的新列