rplotlayoutfigureeulerr

How to create a panel layout of plots from the eulerr [r] package


I am using the eulerr package to make venn diagrams/euler diagrams. However, I would like to add these plots to a panelled figure and I can't figure out how. Each time I make the euler plot, it creates a single new figure instead of adding to a panel.

Somewhere internally it must be calling a new figure window? In the documentation I see that there is the internal function plot.eulergram that has a newpage = TRUE default argument, but when I try to use the function plot.eulergram() instead of just plot() it says the function does not exist.

Here is a simple example, I tried using mfrow, layout, and fig:

library(eulerr)

x <- data.table("A" = rep(c(TRUE,FALSE), times = 20),
                "B" = rep(c(TRUE,FALSE), each = 10),
                "C" = rep(c(TRUE,TRUE,FALSE,TRUE), times = 5))

fit.ellipses <- eulerr::euler(combinations = x, shape = "ellipse")

# here is the basic plot, this part works
plot(fit.ellipses)

# try with mfrow, doesn't work
par(mfrow = c(1,2))
plot(fit.ellipses)
plot(fit.ellipses)

# try with layout, doesn't work
m <- matrix(1:2, ncol = 2)
layout(mat = m)
plot(fit.ellipses)
plot(fit.ellipses)

# try with fig, doesn't work
par(fig = c(0,.5,0,1))
plot(fit.ellipses)
par(fig = c(.5,1,0,1), new = TRUE)
plot(fit.ellipses)

# try that internal function, isn't there??
plot.eulergram(fit.eliipses, newpage = F)

# Error in plot.eulergram(fit.eliipses, newpage = F) : 
#  could not find function "plot.eulergram"

From the docs, it looks like maybe it uses the grid graphics system instead of base R, so maybe that's why these panelling functions aren't working, but I don't understand exactly what that means. Thank you for any advice!


Solution

  • You're looking for the gridExtra package. grid.arrange() is probably all you need:

    library(eulerr)
    library(data.table)
    library(gridExtra) # for arranging the plots
    
    x <- data.table("A" = rep(c(TRUE,FALSE), times = 20),
                    "B" = rep(c(TRUE,FALSE), each = 10),
                    "C" = rep(c(TRUE,TRUE,FALSE,TRUE), times = 5))
    
    fit.ellipses <- eulerr::euler(combinations = x, shape = "ellipse")
    
    # here is the basic plot, this part works
    plot(fit.ellipses)
    
    
    # Using grid.arrange() from gridExtra:
    grid.arrange(
      plot(fit.ellipses),
      plot(fit.ellipses)
    )
    
    # you can also use ncol/nrow to define the plot layout
    grid.arrange(
      plot(fit.ellipses),
      plot(fit.ellipses),
      plot(fit.ellipses),
      nrow=1
    )
    

    output of the second plot: enter image description here