rvenn-diagrameulerr

Scaled plotting of multiple pairwise Venn diagrams in R


I want to plot >50 Venn/Euler diagrams of two sets each to scale. It is necessary that not only the overlap of the two sets and the set size themselves should scale but also the size of the individual diagrams compared to each other.

Since I know of no R package that allows the plotting of >50 pairwise Venn diagrams at the same time, I was planning to plot them first individually (e.g., using eulerr) and then put all of them together using the gridExtra package or something similar.

However, in this way, the size of the individual pairwise diagrams is not comparable:

require(gridExtra)
require(eulerr)
fit1 <- euler(c(A=300, B=500, "A&B"=100))
fit2 <- euler(c(A=40, B=70, "A&B"=30))
grid.arrange(plot(fit1), plot(fit2), nrow=1)

Does anyone know of an R package or a combination of packages that would allow size-appropriate plotting of several pairwise Venn diagrams?


Solution

  • You could try using the widths argument of grid.arrange. You would have to determine the ratio of each of the venn diagrams' totals. In your example, the total size ratio is 800:110, which is 7.27, so if you do grid.arrange(plot(fit1), plot(fit2), ncol = 2, widths = c(7.27, 1)) then fit2 will be much smaller than fit1. The ggarrange() function from ggpubr should work also.

    fit1 <- euler(c(A=300, B=500, "A&B"=100))
    fit2 <- euler(c(A=40, B=70, "A&B"=30))
    
    tot1 <- 800
    tot2 <- 110
    ratio_v <- tot1/tot2
    
    grid.arrange(plot(fit1), plot(fit2), ncol = 2, widths = c(ratio_v, 1))
    ggpubr:ggarrange(plotlist = list(plot(fit1), plot(fit2)), ncol = 2, widths = c(ratio_v, 1))
    

    enter image description here

    Edit: Want the individual pairwise sets to have their own size ratios, instead of everything relative to a global maximum. This is a simple example, but you can write a function to do this automatically for each one. Basically set the maximum number of columns (I just chose 100), and then convert each of your ratios to be out of 100. Make a row for each venn diagram set, then rbind them all into a matrix and use the layout_matrix argument.

    ### Make fits
    fit1 <- euler(c(A=300, B=500, "A&B"=100))
    fit2 <- euler(c(A=40, B=70, "A&B"=30))
    fit3 <- euler(c(C=100, D=300, "C&D"=50))
    fit4 <- euler(c(C=50, D=80, "C&D"=30))
    
    ### Assign totals 
    tot1 <- 800
    tot2 <- 110
    tot3 <- 400
    tot4 <- 130
    
    ### Find ratios
    ratioAB_v <- round(tot1/tot2)
    ratioCD_v <- round(tot3/tot4)
    
    ### Convert ratios
    smallAB_v <- round(1/ratioAB_v*100)
    smallCD_v <- round(1/ratioCD_v*100)
    
    ### Make rows
    row1_v <- c(rep(1, (100-smallAB_v)), rep(2, smallAB_v))
    row2_v <- c(rep(3, (100-smallCD_v)), rep(4, smallCD_v))
    
    ### Make matrix
    mat <- rbind(row1_v, row2_v)
    
    ### Plot
    grid.arrange(plot(fit1), plot(fit2), plot(fit3), plot(fit4), layout_matrix = mat)
    

    enter image description here