使用绘图和色盲调色板的多边形颜色透明度

时间:2014-08-09 12:32:13

标签: r colors plot color-blindness

我正在使用纯素来绘制物种积累曲线   *我想将它们叠加在同一个图上   *我想使用色盲友好的颜色   *我想对绘制的多边形使用透明度

我能够完成上面的前两个(参见下面的示例代码),但是当我使用colorblind-friendly RGB颜色(允许使用alpha,指定透明度的参数)而不是十六进制颜色(不允许透明度),我收到一条错误信息(例如:'rgb中的错误(0,158,115,0.5):颜色强度115,而不是[0,1]')

我知道我的代码不够优雅 - 只是一个初学者喋喋不休!

如何通过透明度控制以彩色友好颜色覆盖多边形来实现此目的?

    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

1 个答案:

答案 0 :(得分:1)

您的代码无效,因为rgb需要比例。只需将所有值除以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

这是一个方便的包装器,可以为您完成所有工作!

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))