0

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 =/

jay.sf
  • 60,139
  • 8
  • 53
  • 110
ginn
  • 87
  • 5
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a working code example and a snippet of your data or some fake data. – stefan Mar 05 '23 at 22:16

2 Answers2

4

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • It doesn't work =// sorry, I was editing my answer, but I'm actually using VennDiagram::draw.quad.venn() to plot my venn diagram – ginn Mar 05 '23 at 23:03
  • @not_ginn I was replicating your "desired plot". Anyway, see my update for how to achieve this with your other plot type. – Allan Cameron Mar 05 '23 at 23:24
0

A straightforward approach is to use the void theme of ggplot like this:

vd <- ggVennDiagram(x, theme = 'void')
ggplot(...) + ## your main plot code here
  inset_element(vd, .5, .5, 1, 1)
I_O
  • 4,983
  • 2
  • 2
  • 15
  • But I plotted my venn diagram with VennDiagram::draw.quad.venn() and it doesn't have that option. I opted for this package because I couldn't manually set the values with ggVennDiagram – ginn Mar 05 '23 at 22:43
  • Then, please, remove the Venn Diagramm example from your OP (you're using `ggVennDiagram` there). – I_O Mar 05 '23 at 22:46
  • Sorry...! Ok. Done – ginn Mar 05 '23 at 22:50
  • From this answer: https://stackoverflow.com/questions/16039437/modify-unplotted-glist-list I guess it might get hacky to directly set a transparent background. It might be easier to, e.g., open a `png` device, save the diagram with transparent background (https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/png.html) and use that for the inset. Sorry. – I_O Mar 05 '23 at 23:06