Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding percents to Venn Diagrams in R

I am trying to add percentages to each section of my Venn Diagram. I have tried using paste to concatenate the circle titles with the percentages. However, unfortunately, this does not completely work, for it only provides the percentages for each of the independent groups and does not provide percentages for each of the intersections, since the intersections do not have titles. Ideally, I would like the percentage to be inside of the circle. For example, I would like triple intersection in the middle (work, study, play) to denote "83, 20.54%" instead of what it currently denotes, "83."

The following is a basic example of the code than I am working with:

g = draw.triple.venn(
  area1 = 396,
  area2 = 273,
  area3 = 147,
  n12 = 266,
  n23 = 86,
  n13 = 143,
  n123 = 83,
  category = c("Study", "Work", "Play"),
  fill = c("blue", "red", "green"),
  euler.d=TRUE,
  scaled=TRUE, ind = FALSE,
)

require(gridExtra)
grid.arrange(gTree(children=g), main="Title", sub="subtitle")
like image 469
user3209543 Avatar asked Dec 04 '22 07:12

user3209543


2 Answers

At the moment VennDiagram::draw.triple.venn has the cell labels hard coded as the numbers. There are no switches to throw to change that default. It's pretty easy to hack it after you identify the place where the labels are defined. Change:

cell.labels <- areas

To:

draw.triple.venn2 <- function( ....
      .....
cell.labels <- paste0(areas," : ", round( 100*areas/sum(areas), 1), "%")
       .....
  }

png(); 
 print( grid.arrange(gTree(children=g), main="Title", sub="subtitle")); 
dev.off()

enter image description here

I defined a draw.triple.venn2 function and inserted a "2" in your code and got what you see above.

like image 60
IRTFM Avatar answered Dec 06 '22 20:12

IRTFM


As of now, the VennDiagram package now supports the print.mode argument, which can be changed to "percent" to display percentages inside the Venn diagram. For example:

example.list = list(A=1:10, B=6:15, C=c(10, 16:20))
venn.grid = venn.diagram(example.list, filename=NULL, print.mode="percent")
grid.draw(venn.grid)

enter image description here

like image 38
Eric Fournier Avatar answered Dec 06 '22 20:12

Eric Fournier