R语言生成颜色向量

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值