HazelJay
HazelJay

Reputation: 33

Venn diagram (4 sets) with R: problem with percentages

I am trying to draw a Venn diagram with four logical variables. I have tried many different R packages but with each of them I have faced some problems. So far, the best result I have achieved by using the ggvenn package. However, the problem is, that it shows the percentages of the intersections based on the observations included in the diagram, instead of all observations in the data.

Below is an example Venn diagram and its code to illustrate the problem. So my question is: is there some way to display the percentages in relation to the total amount of observations in the data. For instance, in the diagram below the intersection of ABCD consists of 45 observation and thus the correct proportion would be 4.5% (i.e. 45/1000) instead of 4.7%.

I would really appreciate if someone could help me out with this.

library(ggvenn)

a <- sample(x = c(TRUE, TRUE, FALSE), size = 1000, replace = TRUE)
b <- sample(x = c(TRUE,FALSE, FALSE), size = 1000, replace = TRUE)
c <- sample(x = c(TRUE, FALSE, FALSE, FALSE), size = 1000, replace = TRUE)
d <- sample(x = c(TRUE, TRUE, TRUE, FALSE), size = 1000, replace = TRUE)

df <- tibble(values = c(1:1000), A=a, B=b, C=c, D=d)

ggvenn(df,
  fill_color = c("black", "grey70", "grey80", "grey90"),
  show_percentage = TRUE,
  digits = 1,
  text_size = 2.5)

An example Venn diagram

Upvotes: 3

Views: 1672

Answers (1)

tivd
tivd

Reputation: 750

A quick fix for this problem would be to use the latest Github version of {ggvenn}:

remove.packages("ggvenn")
devtools::install_github("yanlinlin82/ggvenn")

Then, by default, there will be also a percentage for the observations that are outside A/B/C/D. This gives you the percentages you're looking for:

Venn diagram

Upvotes: 3

Related Questions