Search code examples
rggplot2colorsdata-visualizationvenn-diagram

Color Fill with R VennDiagram: "Unexpected parameter length for 'fill'"


I use a Venn Diagram to visualize the overlap between values_one and values_two.

The default outcome is black-and-white, but I want to fill it with colors. Based on this tutorial, I use RColorBrewer; however, I get the following error message:

Error in VennDiagram::draw.pairwise.venn(area1 = length(x[1]), area2 = length(x[[2]]), : Unexpected parameter length for 'fill'

What did I do wrong, how can I fix it?

Thank you for your help!

# ------------------
# reproducible values
# ------------------
values_one <- c("Physical Review B", "IFAC Papersonline", "Journal Of High Energy Physics", 
"Physical Review Letters", "Nuclear Fusion", "Physics Letters B")

values_two <- c("Lecture Notes", "Optics Infobase Conference Papers", "Physical Review B", "IFAC Papersonline", "Journal Of High Energy Physics", "Physical Review Letters")

# ------------------
# create VennDiagram
# ------------------
library("VennDiagram")
library("RColorBrewer")

myCol <- brewer.pal(3, "Pastel2")

venn.diagram(x = list(values_one, values_two)
             , category.names = c("WoS", "Scopus")
             , filename = paste0(filePath, "venn2019.png")
             , output=T
             , fill=myCol)

Solution

  • Try limiting myCol to two hex codes instead of three.

    Edit:

    Because RColorBrewer::brewer.pal has a lower limit on number of colors you can choose directly (n > 2), in case you want only one or two colors, you first need to specify the palette you want to use and then subset the specific hex codes. For example like so:

    myCol <- brewer.pal(8, "Pastel2")[c(1, 2)]
    myCol <- brewer.pal(8, "Pastel2")[c(1, 8)] # Or any other nice-looking combination