简介和使用

02. 详说RColorBrewer - 图1

我们需要下载一下这个包:

  1. install.packages('RColorBrewer')

使用包中的函数brewer.pal() 可以创建一个颜色值的向量,n 指定从颜色盘中取得的数目,name 指定颜色盘:

  1. > library(RColorBrewer)
  2. > mycolor = brewer.pal(7, 'Set1')
  3. > mycolor
  4. [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628"

可以看看这些颜色:

  1. > barplot(rep(1,7), col = mycolor)

02. 详说RColorBrewer - 图2

我们还可以看看包中的所有颜色:

  1. library(RColorBrewer)
  2. display.brewer.all()

02. 详说RColorBrewer - 图3

按照官方的介绍,RColorBrewer 上面这三类调色板各有应用:

  1. Sequential palettes (first list of colors): 适合单一方向变化的连续性变量,names are : Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu YlOrBr, YlOrRd
  2. Qualitative palettes (second list of colors): 适合分类变量,names are : Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3.
  3. Diverging palettes (third list of colors): 由中间向两侧变化的连续性变量,names are : BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral



重要函数

  1. function arguments:
  2. n: Number of different colors in the palette, minimum 3, maximum depending on palette.
  3. name: A palette name from the lists above. For example name = RdBu.
  4. type: The type of palette to display. Allowed values are one of: div”, qual”, seq”, or all”.
  5. select: A list of palette names to display.
  6. colorblindFriendly: if TRUE, display only colorblind friendly palettes.

获得对应调色板对应数目颜色的十六进制数值:

  1. brewer.pal(n, name)
  2. brewer.pal(3, "OrRd")
  3. [1] "#FEE8C8" "#FDBB84" "#E34A33"

展示某个调色板的对应数目颜色(其实也可以自己画柱状图实现类似):

  1. display.brewer.pal(3, "OrRd")

02. 详说RColorBrewer - 图4

展示部分或全部颜色, 这点吹爆啊,还有一个参数colorblindFriendly 可以选择是否只出现对色盲友好的颜色:

  1. display.brewer.all(n = NULL, type = "all", select = NULL,
  2. colorblindFriendly = T)

02. 详说RColorBrewer - 图5

总结

02. 详说RColorBrewer - 图6

  1. Sequential palettes (first list of colors): 适合单一方向变化的连续性变量,names are : Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu YlOrBr, YlOrRd
  2. Qualitative palettes (second list of colors): 适合分类变量,names are : Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3.
  3. Diverging palettes (third list of colors): 由中间向两侧变化的连续性变量,names are : BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral