Mavershang
Mavershang

Reputation: 1278

Venn diagram in R label mismatch

I was generating a venn diagram with label using the code as below:

v <- venn.diagram(list(ISG15=d1, USP18=d2),
                  fill = c("orange", "blue"),
                  alpha = c(0.5, 0.5), cat.cex = 3, cex=1.5,
                  filename=NULL)
# have a look at the default plot
grid.newpage()
grid.draw(v)

# have a look at the names in the plot object v
lapply(v,  names)
# We are interested in the labels
lapply(v, function(i) i$label)

# Over-write labels (5 to 7 chosen by manual check of labels)
# in foo only
v[[5]]$label  <- paste(setdiff(d1, d2), collapse="\n")  
# in baa only
v[[6]]$label <- paste(setdiff(d2, d1)  , collapse="\n")  
# intesection
v[[7]]$label <- paste(intersect(d1, d2), collapse="\n") 

Ideally I assume d1 (ISG15 in my dataset) will be on the left side, and d2 (UPS18) on the right. But actually d2 is on the left side, which cause my label mismatched. I don't understand the reason (probably because d1 has 25 element and d2 has 21 element? I don't know). Did I miss sth there?

But

Upvotes: 0

Views: 219

Answers (1)

vqf
vqf

Reputation: 2628

I had a look at the source code and saw that the sets are indeed sorted by size:

if (!inverted) {
    tmp1 <- max(area1, area2);
    tmp2 <- min(area1, area2);
    if (tmp1 != area1) { list.switch <- TRUE; }
    area1 <- tmp1;
    area2 <- tmp2;
    r1 <- sqrt(area1 / pi);
    r2 <- sqrt(area2 / pi);
    if (r2 == 0) {r2 <- 0.5*r1 }
    shrink.factor <- max.circle.size / r1;
    }
else {
    tmp1 <- max(area1, area2);
    tmp2 <- min(area1, area2);
    if (tmp1 != area1) { list.switch <- TRUE; }
    area1 <- tmp1;
    area2 <- tmp2;
    r1 <- sqrt(area1 / pi);
    r2 <- sqrt(area2 / pi);
    if (r1 == 0) {r1 <- 0.5*r2 }
    shrink.factor <- max.circle.size / r2;
    }

In a very convoluted way, this sets a list.switch variable to true if area1 is not the largest. You can pass inverted=true to the function, but that means you have to keep track of which set is larger and apply the flag only when appropriate. However, you may also want to consider other options, as detailed in this thread.

Upvotes: 0

Related Questions