Home > OS >  set the color of categories in venn diagram in r
set the color of categories in venn diagram in r

Time:12-05

I am using ggVennDiagram to creat a venn diagram. I would like to set the color of categories manually. Here is what I am trying, however the color of border line of the circles is not changed.

x <- list(A=1:5,B=2:7,C=3:6,D=4:9)
ggVennDiagram(x, label = "count", label_alpha = 0, 
          color =  c("A" = "yellow","B" ="steelblue",'C' = 'red', 'D' = 'black') ,
          set_color = c("A" = "yellow","B" ="steelblue", 'C' = 'red', 'D' = 'black'))  
 scale_fill_gradient(low = "#F4FAFE", high = "#4981BF")

Any Idea how I can match the colors of circle lines with the name of categories?

Thanks

CodePudding user response:

From ggVennDiagram documentation it looks as if you have to build up the venn diagram rather than use the ggVennDiagram function. Maybe this adaptation from the documentation example gives you enough to work on...

Updated to include OP's comment for percentage count.

library(ggplot2)
library(ggVennDiagram)

x <- list(A=1:5,B=2:7,C=3:6,D=4:9)

venn <- Venn(x)
data <- process_data(venn)
ggplot()  
  # 1. region count layer
  geom_sf(aes(fill = count), data = venn_region(data))  
  # 2. set edge layer
  geom_sf(aes(color = name), data = venn_setedge(data), show.legend = TRUE, size = 2)  
  # 3. set label layer
  geom_sf_text(aes(label = name), data = venn_setlabel(data))  
  # 4. region label layer
  geom_sf_label(aes(label = paste0(count, " (", scales::percent(count/sum(count), accuracy = 2), ")")), 
                data = venn_region(data),
                size = 3)  
  scale_fill_gradient(low = "#F4FAFE", high = "#4981BF") 
  scale_color_manual(values = c("A" = "yellow","B" ="steelblue",'C' = 'red', 'D' = 'black'),
                     labels = c('D' = 'D = bdiv_human')) 
  theme_void()

Created on 2021-12-04 by the reprex package (v2.0.1)

  • Related