将额外的文本添加到使用VennDiagram R包绘制的维恩图中

时间:2014-05-21 22:30:15

标签: r venn-diagram

我有一个从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"
);

venn digrams:

enter image description here

由于

1 个答案:

答案 0 :(得分:2)

可以帮助您入手:

我使用了您的示例,但对于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()

enter image description here

根据这些数字,您可以看到必须按照标签的顺序来实现您想要的目标。

希望这会对你有所帮助,

亚历