简介和使用
我们需要下载一下这个包:
install.packages('RColorBrewer')
使用包中的函数brewer.pal() 可以创建一个颜色值的向量,n 指定从颜色盘中取得的数目,name 指定颜色盘:
> library(RColorBrewer)
> mycolor = brewer.pal(7, 'Set1')
> mycolor
[1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628"
可以看看这些颜色:
> barplot(rep(1,7), col = mycolor)
我们还可以看看包中的所有颜色:
library(RColorBrewer)
display.brewer.all()
按照官方的介绍,RColorBrewer 上面这三类调色板各有应用:
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
Qualitative palettes (second list of colors): 适合分类变量,names are : Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3.
Diverging palettes (third list of colors): 由中间向两侧变化的连续性变量,names are : BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
重要函数
function arguments:
n: Number of different colors in the palette, minimum 3, maximum depending on palette.
name: A palette name from the lists above. For example name = RdBu.
type: The type of palette to display. Allowed values are one of: “div”, “qual”, “seq”, or “all”.
select: A list of palette names to display.
colorblindFriendly: if TRUE, display only colorblind friendly palettes.
获得对应调色板对应数目颜色的十六进制数值:
brewer.pal(n, name)
brewer.pal(3, "OrRd")
[1] "#FEE8C8" "#FDBB84" "#E34A33"
展示某个调色板的对应数目颜色(其实也可以自己画柱状图实现类似):
display.brewer.pal(3, "OrRd")
展示部分或全部颜色, 这点吹爆啊,还有一个参数colorblindFriendly
可以选择是否只出现对色盲友好的颜色:
display.brewer.all(n = NULL, type = "all", select = NULL,
colorblindFriendly = T)
总结
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
Qualitative palettes (second list of colors): 适合分类变量,names are : Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3.
Diverging palettes (third list of colors): 由中间向两侧变化的连续性变量,names are : BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral