Manual Ordering of Sets in R/VennDiagram

我们两清 提交于 2019-12-09 21:28:11

问题


I'm using VennDiagram to illustrate the overlap between distinct sets of customers -- in total and for a particular sub-segment. The problem that I'm having is that it appears VennDiagram automatically orders the circles in the output from largest to smallest. In the two diagrams I'm creating the relative size of the two populations is flipping, so in the output the populations/colors of the diagram are reversed. I want to put these side by side in a document, and the flipping of population order makes side by side comparison a little confusing.

Sample code for each is below -- is there a way to manually force the ordering of sets in the output, so that populations are ordered in the same sequence?

Thank you -

venn.plot <- venn.diagram(
x = list(
"AD" = 1:703814,
"WM" = 672279:1086933
),
height = 4000 ,
width = 4000 ,
units = 'px',
filename = "H:\\AD_vs_WM_Total.tiff",
scaled = TRUE,
ext.text = TRUE,
lwd = 1, 
ext.line.lwd = 1,
ext.dist = -0.15,
ext.length = 0.9,
ext.pos = -4,
fill = c("cornflowerblue", "darkorchid1"),
cex = 1.5,
cat.cex = 2,
cat.col = c("black", "black"),
cat.pos = c(120,300) ,
rotation.degree = 45,
main = "AD vs. WM",
sub = "Total Populations",
main.cex = 2,
sub.cex = 1.5
);

venn.plot <- venn.diagram(
x = list(
"AD" = 1:183727,
"WM" = 173073:383052
),
height = 4000 ,
width = 4000 ,
units = 'px',
filename = "H:\\AD_vs_WM_Target.tiff",
scaled = TRUE,
ext.text = TRUE,
lwd = 1, 
ext.line.lwd = 1,
ext.dist = -0.15,
ext.length = 0.9,
ext.pos = -4,
fill = c("cornflowerblue", "darkorchid1"),
cex = 1.5,
cat.cex = 2,
cat.col = c("black", "black"),
cat.pos = c(120,300) ,
rotation.degree = 45,
main = "AD vs. WM",
sub = "Target Populations",
main.cex = 2,
sub.cex = 1.5
);

回答1:


use an if statement to see if the second set is larger than the first. if yes, then add 180 degrees to rotation.




回答2:


If statement would do the trick but it seems a bit heavy handed for such a small task. I think you could simply add the following line into your script

inverted=length(x$AD) < length(x$WM)

And that should do the trick (when WM is longer than AD statement is True and plot is inverted).




回答3:


This didn't work for me. The data in the circles flipped, but the labels on the circles stayed in place. The rotation approach worked, though.




回答4:


It can be solved by altering the gList object from draw.pairwise.venn() function. venn.diagram() is a wrapper for many draw.____.venn() functions (Eg: pairwise, quad, quintuple, single etc.,)

Use an if statement to find any switch in the labels of population in your example. If true, then change inverted = TRUE and switch labels.

Also use ind = FALSE to prevent drawing the diagram.

I used your example to show solution for one of two venn diagrams you posted in your question.

packageVersion("VennDiagram")
# [1] ‘1.6.17’

AD <-  1:703814
WM <-  672279:1086933
overlap <- calculate.overlap(x = list('AD' = AD, 'WM' = WM))
area_overlap <- sapply(overlap, length)

g <- draw.pairwise.venn(area1 = area_overlap[1],
                        area2 = area_overlap[2],
                        cross.area = area_overlap[3],
                        category = c("AD", "WM"),
                        ind = FALSE,
                        inverted = FALSE,
                        scaled = TRUE,
                        ext.text = TRUE,
                        lwd = 1, 
                        ext.line.lwd = 1,
                        ext.dist = -0.15,
                        ext.length = 0.9,
                        ext.pos = -4,
                        fill = c("cornflowerblue", "darkorchid1"),
                        cex = 6,
                        cat.cex = 6,
                        cat.col = c("black", "black"),
                        cat.dist = c(-0.16, -0.09),
                        cat.pos = c(0,10),
                        rotation.degree = 35)

# check for switch in labels
if(area_overlap[1] != (as.integer(g[[5]]$label) + as.integer(g[[7]]$label)) && area_overlap[2] !=  (as.integer(g[[6]]$label) + as.integer(g[[7]]$label))){
  # change inverted to TRUE
  g <- draw.pairwise.venn(area1 = area_overlap[1],
                          area2 = area_overlap[2],
                          cross.area = area_overlap[3],
                          category = c("AD", "WM"),
                          ind = FALSE,
                          inverted = TRUE,
                          scaled = TRUE,
                          ext.text = TRUE,
                          lwd = 1, 
                          ext.line.lwd = 1,
                          ext.dist = -0.15,
                          ext.length = 0.9,
                          ext.pos = -4,
                          fill = c("cornflowerblue", "darkorchid1"),
                          cex = 6,
                          cat.cex = 6,
                          cat.col = c("black", "black"),
                          cat.dist = c(-0.16, -0.09),
                          cat.pos = c(0,10),
                          rotation.degree = 35)

  # switch labels
  tmp_var      <- g[[6]]$label
  g[[6]]$label <- g[[5]]$label
  g[[5]]$label <- tmp_var
  rm(tmp_var)
}

jpeg("AD_vs_WM_Total_new.jpg", width = 1200, height = 1200)
plot.new()
title(main = "AD vs. WM", sub = "Total Populations", cex.main = 6, cex.sub = 5, line = -4, outer = TRUE)
grid.draw(g)
dev.off()



来源:https://stackoverflow.com/questions/23687803/manual-ordering-of-sets-in-r-venndiagram

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!