1. 内置函数生成
## colorRampPalette函数
colors <- colorRampPalette(c("blue", "red"))(5)
colorRampPalette(c("red", "orange", "blue"),space = "rgb")(5)
##colorRamp函数
col_fun <- colorRamp(c("red", "green", "blue"))
# col_fun 把 0 and 1之间的向量映射到RGB颜色值
portions <- c(0.1,0.2,0.8)
pie(portions, col = col_fun(portions))
# colorRamp returns a function with argument a vector of values between 0 and 1
# that are mapped to a numeric matrix of RGB color values
# with one row per color and 3 or 4 columns.
##rainbow函数
col1 <- rainbow(10)
col2<- rainbow(10, s = 1, v = 1, start = 0.2, end = 0.8,
alpha = 0.5, rev = FALSE)
par(mfrow = c(2, 2))
pie(rep(1, 10), col = col1)
pie(rep(1, 10), col = col2)
2. RColorBrewer包
library(RColorBrewer)
display.brewer.all()
#display.brewer.pal(6, "OrRd")
brewer.pal.info # 调色板信息
brewer.pal(n,"Dark2")
n<-6
colors <- brewer.pal(n,"Set3")
brewer.pal(n,"Dark2")
brewer.pal(n,"Dark2")
## n 不超过每个调色板的颜色数
# Accent 8
# Dark2 8
# Paired 12
# Pastel1 9
# Pastel2 8
# Set1 9
# Set2 8
# Set3 12
newpalette<-colorRampPalette(brewer.pal(9,"Blues"))(10)
3.circlize包
col_fun = colorRamp2(c(-4, 0, 5), c("green", "white", "red"))
# colorRamp2:返回一个函数,该函数接受数值向量并返回插值颜色。
col_fun(-1) # green
col_fun(0) # white
col_fun(1) #red
col <- c(-2,-0.5,0.2,0.3,3,4)
pie(1:6, col = col_fun(col))