Setting R colors

Setting R colors

R color setting 分为2步:

  1. pick color
  2. 调色
# Rscript
# 选取color 
selected_color = c("white","blue")
# 将white 和 blue 之间平均分成100份
colors = colorRampPlatte(selected_color)(100)

选颜色

有以下几种选择

1. 手动输入名称

如“white”,“lightblue"
如RGB value 等方式

2. 使用R color 一些已经被设定好的 range

RColorBrewer
为我们设置好了一些可直接用的picked color:
分为三类:

  • sequential的,适用于0-1 这种类别的;
  • diverging 适用于(-1,1)
  • quantitive 可以直接用于散点图等,是区分度很大的一些颜色


    1.png

最多9个,可以设置2-9等分的picked color。
例如:

brewer.pal(n=5, "Reds")
display.brewer.pal(n=5, "Reds") 只用作展示
生成了平均五份的color


3.png

调色

连续样本值时,需要调色来反应程度。
colorRampPlatte(selected_color)(100)
得到100种相对连续的颜色。

示例

离散的颜色设定

不规定颜色

# 选颜色
mycols = brewer.pal(3, "Set1")

# 给150个样本根据他们各自所属的物种名称分配颜色名字。
# tricky!
sample_cols = with(iris, mycols[c(Species)])

plot(Sepal.Length ~ Sepal.Width,
col = sample_cols, # c(Species) 将factor 根据其level转换成 1,2,3
data=iris, pch=16)
legend("topleft", pch=16, col=rainbow_hcl(3),
legend=unique(iris$Species))

2.png

限定 物种-颜色

iris$color <- factor(iris$Species,
levels=c("virginica", "versicolor", "setosa"),
labels=rainbow_hcl(3))
plot(Sepal.Length ~ Sepal.Width,
col=as.character(color), pch=16, data=iris)

连续的颜色设定

Option 1

Break into categories and assign colors:

iris2 <- subset(iris, Species=="setosa")
color <- cut(iris2$Petal.Length,
breaks=c(0,1.3,1.5,2), labels=sequential_hcl(3))

Break by quantiles (be sure to include 0 & 1):

color <- cut(iris2$Petal.Length,
breaks=quantile(iris$Petal.Length, c(0, 0.25, 0.5,
0.75, 1)), labels=sequential_hcl(3))
plot(Sepal.Width ~ Sepal.Length, pch=16,
col=color, data=iris2)
Option 2

Fully continuous gradient:

data <- data.frame("a"=runif(10000),
"b"=runif(10000))
color=diverge_hcl(length(data$a))[rank(data$a)]
plot(a~b, col=color, pch=16, data=data)

推荐的教程

RColor CheatSheet
https://www.stat.ubc.ca/~jenny/STAT545A/block14_colors.html

你可能感兴趣的:(Setting R colors)