Search code examples
rcolorsplotcolor-blindness

Polygon color transparency using plot and colorblind pallet


I am using vegan to plot species accumulation curves.
* I want to overlay them on the same plot * I want to use colorblind friendly colors * I want to use transparency with the plotted polygons

I'm able to do the first two of the above (see example code below), but when I use colorblind-friendly RGB colors (to allow use of alpha, the parameter specifying the transparency) instead of hex colors (which do not allow transparency), I get an error message (example: 'Error in rgb(0, 158, 115, 0.5) : color intensity 115, not in [0,1]')

I know my code is less than elegant - just a beginner thrashing about!

How can I do this with overlying polygons in colorblind-friendly colors with transparency control?

    habitat1 = data.frame(species1=c(0,0,3,4,0,5,9),
                  species2=c(1,0,3,0,5,0,0),
                  species3=c(0,1,0,2,4,0,0),
                  species4=c(8,0,2,0,5,1,0))
    habitat2 = data.frame(species1=c(3,23,13,99,1,0,0),
                  species2=c(1,0,3,4,0,26,0),
                  species3=c(7,1,8,38,4,47,7),
                  species4=c(7,7,2,3,5,0,8))

    require(ggplot2)
    require(vegan)

    speca_hab1 <- specaccum(comm=habitat1, method="random", permutations=1000)
    speca_hab2 <- specaccum(comm=habitat2, method="random", permutations=1000)

    par(mfrow=c(1,1), mai=c(2, 2, 1, 1))

    plot.new() # telling R we are starting a new plot

    plot(speca_hab1, main="Speciea Accumulation by Habitat Type", 
         xlab ="# of samples", ylab ="# of species", ci.type="polygon", 
         ci.col="#CC79A7", ci.lty=0, col = "yellow", xlim = c(1, 6), 
         ylim = c(0, 8)) # habitat 1 - Savanna
    plot(speca_hab2, xlab ="# of samples", ylab ="# of species", 
         ci.type="polygon", ci.col="#D55E00", ci.lty=0, col="yellow", 
         add=TRUE) # habitat 2 - Prairie

    # colorblind friendly colors
    # 0,0,0       #000000 # Black
    # 230,159,0   #E69F00 # Orange
    # 86,180,233  #56B4E9 # Sky Blue
    # 0,158,115   #009E73 # bluish Green
    # 240,228,66  #F0E442 # Yellow
    # 0,114,178   #0072B2 # Blue
    # 213,94,0    #D55E00 # Vermillion
    # 204,121,167 #CC79A7 # reddish Purple

    #-------------------------------------------------
    #The following does not work:

    plot.new() # telling R we are starting a new plot

    plot(speca_hab1, main="Speciea Accumulation by Habitat Type", 
         xlab ="# of samples", ylab ="# of species", ci.type="polygon", 
         ci.col=rgb(0,158,115,0.5), ci.lty=0, col = "yellow", xlim = c(1, 6), 
         ylim = c(0, 8)) # habitat 1 - Savanna
    plot(speca_hab2, xlab ="# of samples", ylab ="# of species", 
         ci.type="polygon", ci.col=rgb(213,94,0,0.5), ci.lty=0, col="yellow", 
         add=TRUE) # habitat 2 - Prairie

    # color=rgb(0,0,0,alpha=0.3)  gives black with a tranparency of 30%
    # or,  rgb(0,158,115,0.5) is 50% transparency for bluish Green

Solution

  • Your code was not working because rgb requires proportions. Just divide all the values by 255.

    plot.new() # telling R we are starting a new plot
    
    plot(speca_hab1, 
         main = "Speciea Accumulation by Habitat Type", 
         xlab = "# of samples", 
         ylab = "# of species",
         ci.type = "polygon", 
         ci.col = rgb(0, 158/255, 115/255, 0.5), 
         ci.lty = 0, 
         col = "yellow", 
         xlim = c(1, 6), 
         ylim = c(0, 8)) # habitat 1 - Savanna
    plot(speca_hab2, 
         xlab = "# of samples", 
         ylab ="# of species", 
         ci.type = "polygon", 
         ci.col = rgb(213/255, 94/255, 0, 0.5),
         ci.lty = 0, 
         col = "yellow", 
         add = TRUE) # habitat 2 - Prairie
    

    enter image description here

    Here is a convenient wrapper to do all the work for you!

    col2alpha <- function(col, alpha) {
      col_rgb <- col2rgb(col)/255
      rgb(col_rgb[1], col_rgb[2], col_rgb[3], alpha = alpha)
    }
    
    plot(speca_hab1, 
         main = "Speciea Accumulation by Habitat Type", 
         xlab = "# of samples", 
         ylab = "# of species",
         ci.type = "polygon", 
         ci.col = col2alpha("#0072B2", 0.5), 
         ci.lty = 0, 
         col = "yellow", 
         xlim = c(1, 6), 
         ylim = c(0, 8))