Home > Enterprise >  How do I make certain regions of of my venn diagram colored and have the rest blank?
How do I make certain regions of of my venn diagram colored and have the rest blank?

Time:06-19

For example, make A ∩ B red and everything else white. I've attached an image of my current code and Venn diagram.

install.packages("ggplot2")
install.packages("ggVennDiagram")
library(ggplot2)
library(ggVennDiagram)

## Creating a Venn Diagram

# use data frame as input

test = list(A = 1:1,B = 1:1)

# create a Venn diagram and display all sets

ggVennDiagram(test,label = c("none"))   
scale_fill_gradient(low = "blue",high = "black")   
theme(legend.position = "none",plot.background = element_rect(fill = "steelblue"),
panel.border = element_rect(fill = NA),plot.margin = margin(10, 10, 10, 10))

enter image description here

CodePudding user response:

ggVennDiagram isn't designed explicitly to draw diagrams of set membership, but it can be made to do so. If you use the following set-up:

library(ggVennDiagram)
library(ggplot2)

test = list(A = 1:1, B = 1:1)

p <- ggVennDiagram(test,label = c("none"))  
     scale_color_manual(values = c("black", "black"))  
     theme(legend.position = "none",
        panel.border = element_rect(fill = NA, size = 2),
        plot.margin = margin(10, 10, 10, 10))

p$layers[[1]]$mapping <- aes(fill = name)

Then you can easily specify the membership / non-membership areas like this: first, specify the colors that you want to indicate whether an area is within the set or not:

yes <- "lightblue"
no  <- "white"

Now you can draw the sets as follows:

# A
p   scale_fill_manual(values = c(A = yes, B = no, A..B = yes))

# B
p   scale_fill_manual(values = c(A = no, B = yes, A..B = yes))

# A U B
p   scale_fill_manual(values = c(A = yes, B = yes, A..B = yes))

# A n B
p   scale_fill_manual(values = c(A = no, B = no, A..B = yes))

# A' U B'
p   scale_fill_manual(values = c(A = no, B = no, A..B = no))  
  theme(panel.background = element_rect(fill = yes))

# B'
p   scale_fill_manual(values = c(A = yes, B = no, A..B = no))  
  theme(panel.background = element_rect(fill = yes))

# A'
p   scale_fill_manual(values = c(A = no, B = yes, A..B = no))  
  theme(panel.background = element_rect(fill = yes))

Hopefully you have enough here to color any element you want appropriately.

Created on 2022-06-17 by the reprex package (v2.0.1)

  • Related