Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove margin in ggvenn plotted with ggplot?

I am trying to create a venn diagram with the ggvenn package utilizing ggplot2. However, when I save the image with ggsave there is a lot of white space around the image. I tried to set the margins using par(mar=c(0,0,0,0)) as well as in the plot code itself.

Reproducible example

library(ggvenn)
library(ggplot2)
d <- tibble(value   = c(1,     2,     3,     5,     6,     7,     8,     9),
            `Set 1` = c(TRUE,  FALSE, TRUE,  TRUE,  FALSE, TRUE,  FALSE, TRUE),
            `Set 2` = c(TRUE,  FALSE, FALSE, TRUE,  FALSE, FALSE, FALSE, TRUE),
            `Set 3` = c(TRUE,  TRUE,  FALSE, FALSE, FALSE, FALSE, TRUE,  TRUE),
            `Set 4` = c(FALSE, FALSE, FALSE, FALSE, TRUE,  TRUE,  FALSE, FALSE))

p2 = ggplot(d, aes(A = `Set 1`, B = `Set 2`)) +
  geom_venn(fill_color = c("white", "white"), show_percentage = FALSE) +
  theme(axis.line=element_blank(),
      panel.background = element_rect(fill = "white",colour = "white"),
      axis.text.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks=element_blank(),
      axis.title.x=element_blank(),
      axis.title.y=element_blank(),
      legend.position="none",
      panel.border=element_blank(),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      plot.background=element_blank()) +
      theme(plot.margin=grid::unit(c(0,0,0,0), "mm"))

#ggsave(plot = p2, "/plot2.tiff"

the result is an image with large white margins

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://imgur.com/a/N28KbZi

How can I reduce the margins?

I tried reducing the margins using par(mar=c(0,0,0,0) and theme (plot.margin=grid::unit(c(0,0,0,0),"mm"))

>Solution :

To get actual circles in your Venn diagram, you probably want to use coord_fixed(). Turn off axis expansion using expand = FALSE. We can add theme_void() which removes all axes and margins etc.

ggplot(d, aes(A = `Set 1`, B = `Set 2`)) +
  geom_venn(fill_color = c("white", "white"), show_percentage = FALSE) +
  coord_fixed(expand = FALSE) +
  theme_void()

This should give a plot with reasonable margins.

enter image description here

If you want to reduce them further, give more stringent axis limits to coord_fixed(), e.g.:

coord_fixed(expand = FALSE, xlim = c(-1.7, 1.7), ylim = c(-1.4, 1.4))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading