图表的色彩搭配直接决定图表给读者的感受和印象,漂亮的图表自然会给文章增色不少。因此,学会自定义图表的配色非常重要。
3种色彩搭配(获取一组配色颜色值)的方法。
配色的本质就是得到一组搭配十分“和谐”的颜色。R中的常见颜色的表示方式有16进制颜色码,英文代码,RGB颜色值三种方式。常见的R色彩搭配包如RColorBrewer、ggsci、ggtech等提供现成的色彩搭配方案,可以直接拿来用。下面以RColorBrewer为例:
library(RColorBrewer)
display.brewer.all(type = "seq")
display.brewer.all(type = "qual")
这里仅输出了两种类型的配色,如下图
colors1<-brewer.pal(9,"RdPu")
colors1
"#FFF7F3" "#FDE0DD" "#FCC5C0" "#FA9FB5""#F768A1" "#DD3497" "#AE017E" "#7A0177""#49006A"
colors1<-brewer.pal(9,"Dark2")
"#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E" "#E6AB02" "#A6761D"
"#666666"
另一个配色神器:colortools
https://rpubs.com/gaston/colortools
使用方法:
安装 install.packages("colortools")
library(colortools)
#色轮的生成
wheel("darkblue", num = 12)
barplot(1:12,names.arg=my1,col=my1,las=2)
#单色搭配(Monochromatic )色系的生成
sequential("red")
#类比色搭配(Analogous )与补色/对比色搭配(Complement )色轮可以方便地生成相邻、对比等色系
analogous("darkblue")
#Complementary color scheme
complementary("steelblue")
# define some colors
some_colors = setColors("#3D6DCC", 15)
# pizza plot
pizza(some_colors)
# analagous scheme for color "#3D6DCC"
analogous("#3D6DCC")
# complementary scheme for color "#3D6DCC"
complementary("#3D6DCC")
# split complementary scheme for color "#3D6DCC"
splitComp("#3D6DCC")
# triadic scheme for color "#3D6DCC"
triadic("#3D6DCC")
# tetradic scheme for color "#3D6DCC"
tetradic("#3D6DCC")
# square scheme for color "#3D6DCC"
square("#3D6DCC")
# sequential colors for "#3D6DCC"
sequential("#3D6DCC")
pals()
pals("cheer")
# color names of palette 'cheer'pizza(pals("cheer")
pizza(terrain.colors(12), bg="white")
p<-setColors(3, num=18)
hist(rnorm(100),breaks=10,col=p)
barplot(1:5,col=pals("cheer"))
#箱图的绘制
boxplot(len~dose*supp,data=ToothGrowth,col=pals("cheer"),xlab="group",ylab="len")
绘制聚类图
install.packages("pheatmap")
install.packages("cluster")
install.packages("ape")
library(pheatmap)
library(cluster)
library(ape)
my2<-setColors("blue",num=12)
my3<-pals("drift")
pheatmap(scale(mtcars),col=my2)
#配色可以让我们生成热图中的数据更加清晰、直观
pheatmap(scale(mtcars),col=sequential("blue",10))
pheatmap(scale(mtcars),col=my3,cutree_rows = 3)
plot(as.phylo(hclust(dist(scale(mtcars)))),tip.col=my1)
set.seed(100)
m.c<-kmeans(scale(mtcars),centers=3)
pairs(mtcars[,1:5],pch=21,bg=pals("mystery")[m.c$cluster])
#散点图与k-mean聚类联合分析中,不同互补色系可以更加和谐的表征不同组别的点的分布
综上,通过改颜色工具包,可以便捷的得到我们想要的配色乃至独特的色系。
此外,还可以用windows的取色器,mspaint
在线取色网站