问题描述
在R中,我可以轻松生成一个色带,例如colorRampPalette。下面产生了从蓝色到红色的五种颜色的序列:
In R, I can easily generate a color ramp, for example colorRampPalette. The following produces a sequence of five colors, from blue to red:
> mypal <- colorRampPalette( c( "blue", "red" ) )( 5 )
> mypal
[1] "#0000FF" "#3F00BF" "#7F007F" "#BF003F" "#FF0000"
如何轻松在此调色板上映射数字向量?假设我有一个介于0到10之间的数字向量,如下所示:
How can I easily map a vector of numbers on this palette? Say, I have a vector of numbers between 0 and 10, like this:
x <- c( 1, 9, 8.5, 3, 3.4, 6.2 )
我想要一个函数,例如 map2color
,这样当我运行 map2color(mypal,x)
时,我得到
I would like a function, say map2color
, such that when I run map2color( mypal, x )
, I get
#0000FF, #FF0000, #FF0000, #3F00BF, #3F00BF, #BF003F
我通常会执行以下操作
mypalette[ findInterval( x, seq( 0, 10, length.out= 6 ), rightmost.closed= T ) ]
但是也许有更好的解决方法,会自动为数字矢量生成一个色阶。
but maybe there is a better solution OOB, automatically generating a color scale for a numeric vector.
推荐答案
我在R-Forge上的colourscheme软件包可以做到:
My colourscheme package on R-Forge does that:
其主要目的是创建映射数值va的函数引导(而不只是整数颜色数字)颜色。安装小插图后,它是最好的文档。
its primary purpose is to create functions that map numeric values (not just integer colour numbers) to colours. The vignette is the best documentation once you've got it installed.
这篇关于如何在R中生成数字到颜色的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!