如何使用定义的颜色作为R中的调色板

时间:2014-09-10 06:37:39

标签: r colors plot palette color-palette

我有以下颜色列表:

ddf = structure(list(r = c(0.374, 0.374, 0.051, 0.096, 0.876, 0.415, 
+ 0.596, 0.724, 0.847, 0.588, 0.481, 0.142, 0.819, 0.91, 0.969, 
+ 0.887, 0.432, 0.927, 0.381, 0.04), g = c(0.183, 0.905, 0.662, 
+ 0.706, 0.461, 0.845, 0.07, 0.101, 0.434, 0.885, 0.366, 0.075, 
+ 0.737, 0.722, 0.012, 0.536, 0.967, 0.125, 0.646, 0.898), b = c(0.528, 
+ 0.337, 0.028, 0.898, 0.628, 0.286, 0.523, 0.673, 0.937, 0.604, 
+ 0.337, 0.276, 0.658, 0.979, 0.451, 0.123, 0.446, 0.332, 0.656, 
+ 0.798)), .Names = c("r", "g", "b"), class = "data.frame", row.names = c(NA, 
+ -20L))
> 
> ddf
       r     g     b
1  0.374 0.183 0.528
2  0.374 0.905 0.337
3  0.051 0.662 0.028
4  0.096 0.706 0.898
5  0.876 0.461 0.628
6  0.415 0.845 0.286
7  0.596 0.070 0.523
8  0.724 0.101 0.673
9  0.847 0.434 0.937
10 0.588 0.885 0.604
11 0.481 0.366 0.337
12 0.142 0.075 0.276
13 0.819 0.737 0.658
14 0.910 0.722 0.979
15 0.969 0.012 0.451
16 0.887 0.536 0.123
17 0.432 0.967 0.446
18 0.927 0.125 0.332
19 0.381 0.646 0.656
20 0.040 0.898 0.798
> 

如何以与rainbow()调色板的以下命令相同的方式使用它:

barplot(rep(1,100), yaxt='n',col=rainbow(100))

以下作品:

barplot(rep(1,100), yaxt='n',col=with(ddf, rgb(r,g,b)))

然而,这里的颜色序列重复(5次),而不是调色板从一端拉伸到另一端。

1 个答案:

答案 0 :(得分:2)

在这种情况下,colorRampPalette()功能是您的朋友。首先,让我们来定义

mycols<-with(df, rgb(r,g,b))

现在我们开始,不重复

barplot(rep(1,20), yaxt='n',col=mycols)

enter image description here

现在,重复

barplot(rep(1,100), yaxt='n',col=mycols)

enter image description here

现在,使用colorRampPalette

进行插值
barplot(rep(1,100), yaxt='n',col=colorRampPalette(mycols)(100))

enter image description here