Search code examples
rggplot2patchwork

Removing plot background to make it png


I have a VennDiagram that I wish to plot on top of a chart. I already know I can do this with patchwork::inset_element(). The problem, though, is that the VennDiagram has a white blackground which is default to R Studio plots. I've been looking into magick and trying to remove the Diagram's background, or make it transparent, so it'd essentially become a png image and wouldn't overlap with the chart data, but I can't figure it out how to do this. Could anyone land me a hand???

pacman:: p_load (ggplot2, VennDiagram, patchwork)

Venn Diagram Example:

myvd <- draw.quad.venn (area1 = 300, area3 = 70, area4 = 2, area2 = 1,
                        n12 = 0, n13 = 0, n14 = 0,
                        n23 = 0, n24 = 1, n34 = 1,
                        n123 = 0, n124 = 0, n134 = 0, n234 = 0, n1234 = 0,
                        category = c("A", "B", "C", "D"),
                        fill = c ("orange", "yellow", "pink", "lightblue"), lty = "blank")

Chart Example:

data <- data.frame(name=c("A","B","C","D","E"), value=c(3,12,5,18,45))
chart <- ggplot(data, aes(x=name, y=value)) + geom_bar(stat = "identity", width=0.2)

What it looks like:

enter image description here

But...!

I really needed to have this Venn Diagram's background removed, so it wouldn't overlap with the chart data... and I can't figure out how to do this =/


Solution

  • You can do:

    chart + inset_element(vd + theme_void(), 0, 0.4, 0.9, 0.9)
    

    enter image description here

    Or, if you are using VennDiagram instead of ggVennDiagram, try:

    chart +
      lapply(vd, annotation_custom, xmin = 1, xmax = 4.5, ymax = 45, ymin = 15)
    

    enter image description here