【问题标题】:Adding extra texts to a venn diagram drawn using VennDiagram R package向使用 VennDiagram R 包绘制的维恩图添加额外文本
【发布时间】:2014-05-21 22:30:15
【问题描述】:

我有一个来自 VennDiagram 包的代码,用于绘制维恩图(如下)。我能够实现此代码来绘制维恩图,但我还想为每个分区区域添加额外的文本(I、II、II、IV 的唯一和通用)。例如,运行此代码后,唯一部分“I”为 60,我想在 60 的底部添加类似 XX XU 的内容,其他分区区域也如此。 R中是否有办法修改此代码以实现这些更改?

library(VennDiagram)

venn.plot <- venn.diagram(
   x = list(
       I = c(1:60, 61:105, 106:140, 141:160, 166:175, 176:180, 181:205, 206:220),
       IV = c(531:605, 476:530, 336:375, 376:405, 181:205, 206:220, 166:175, 176:180),
       II = c(61:105, 106:140, 181:205, 206:220, 221:285, 286:335, 336:375, 376:405),
       III = c(406:475, 286:335, 106:140, 141:160, 166:175, 181:205, 336:375, 476:530)
       ),
filename = "1D-quadruple_Venn.tiff",
col = "black",
lty = "dotted",
lwd = 4,
fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
alpha = 0.50,
label.col = c("orange", "white", "darkorchid4", "white", "white", "white",
 "white", "white", "darkblue", "white",
  "white", "white", "white", "darkgreen", "white"),
cex = 2.5,
fontfamily = "serif",
fontface = "bold",
cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"),
cat.cex = 2.5,
cat.fontfamily = "serif"
);

维恩图:

谢谢

【问题讨论】:

  • 不清楚你想要实现什么。最好在您的问题中添加一个情节,并手动(使用油漆或其他工具)将文本添加到情节中,以阐明您想要做什么。
  • 我认为这(很容易)不可能。看到这篇文章stackoverflow.com/questions/12803390/… - 虽然是不久前的事了。也就是说,您可以手动更改 grob 标签。

标签: r venn-diagram


【解决方案1】:

帮助您开始的事情:

我使用了您的示例,但对于 filename,我使用了 NULL,因为我想绘制维恩图以及额外的标签。

## Function that creates a grid.text object
## The offset sets the distance from existing label 
addlab <- function(lab, x, y, offset = 0) {
    grid.text(lab, unit(as.numeric(x), "npc"), 
                   unit(as.numeric(y) - offset, "npc"), 
              draw = FALSE)
}

## Adding a number under each label
lbls <- gList()
o <- 1 ## counter
for(i in seq(along.with=venn.plot)) {
  ## Check if it is a grid.text object
  if(regexpr("text", venn.plot[[i]]$name) > 0) {
    ## Write counter value under the existing label
    lbls <- gList(lbls, addlab(o, venn.plot[[i]]$x, venn.plot[[i]]$y, 0.03))
    ## Increase the counter
    o <- o + 1
  }
}

绘制维恩图和标签:

## tiff("out.tiff")
grid.draw(venn.plot)
grid.draw(lbls)
## dev.off()

从数字中您可以看到您必须放置标签的顺序才能达到您想要的效果。

希望对你有帮助,

亚历克斯

【讨论】:

  • 非常感谢 Alex 提供的代码让我朝着正确的方向前进。我感谢您的帮助。我现在正在努力让代码在每个标签下写两个数字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多