今天学习colorRemap函数。
Color Interpolation(颜色插值)
These functions return functions that interpolate a set of given colors to create new color palettes (like topo.colors) and color ramps, functions that map the interval to colors (like grey).
这些函数返回对一组给定颜色进行插值以创建新的调色板(如topo.colors)和色带的函数,这些函数将间隔映射到颜色(如grey)。
也就是说colorRemap函数对一组颜色颜色进行插值,它的返回值也是函数,返回的函数将间隔映射到具体的颜色。
语法:
colorRamp(colors, bias = 1, space = c("rgb", "Lab"),
interpolate = c("linear", "spline"), alpha = FALSE)
colorRampPalette(colors, …)
参数详解:
colors:colors to interpolate; must be a valid argument to col2rgb()
中文小注:colors是要“插值”的颜色,必须是col2rgb函数的有效参数。col2rgb是R color到RGB(red/green/blue)转换的函数,以后再开个文章学。
bias:a positive number. Higher values give more widely spaced colors at the high end.
中文小注:bias是一个正数。较高的值会在高端提供较宽间隔的颜色。
space:a character string; interpolation in RGB or CIE Lab color spaces.
中文小注:space是一个字符串,rgb或Lab,分别表示在RGB或CIE Lab色彩空间上进行插值。默认是rgb。
interpolate:use spline or linear interpolation.
中文小注:使用spline或者linear插值, c("linear", "spline")。
alpha:logical: should alpha channel (opacity) values be returned? It is an error to give a true value if space is specified.
中文小注:alpha是一个逻辑值,表明是否返回表示不透明度的值。当指定space后,alpha=TRUE会引发错误。
...:arguments to pass to colorRamp.
中文小注:其他等等的参数是传递给colorRamp的参数。
The CIE Lab color space is approximately perceptually uniform, and so gives smoother and more uniform color ramps. On the other hand, palettes that vary from one hue to another via white may have a more symmetrical appearance in RGB space.
CIE Lab色彩空间在感知上近似均匀,因此可以提供更平滑和更均匀的色彩渐变。 另一方面,通过白色从一个色调变化到另一个色调的调色板在RGB空间中可能具有更对称的外观。
The conversion formulas in this function do not appear to be completely accurate and the color ramp may not reach the extreme values in Lab space. Future changes in the R color model may change the colors produced with space = "Lab".
此函数中的转换公式似乎不完全准确,并且色带可能未达到Lab空间中的极限值。 R颜色模型的将来更改可能会更改space =“ Lab”产生的颜色。
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. colorRampPalette returns a function that takes an integer argument (the required number of colors) and returns a character vector of colors (see rgb) interpolating the given sequence (similar to heat.colors or terrain.colors).
colorRamp返回一个函数,其参数为0到1之间的值的向量,该向量映射到RGB颜色值的数字矩阵,每种颜色一行,三或四列。 colorRampPalette返回一个函数,该函数接受一个整数参数(所需的颜色数),并返回一个插值给定序列的颜色字符向量(请参见rgb)(类似于heat.colors或terrain.colors)。
这一段话总结下来就是,这里有两个函数,一个是colorRamp,一个是colorRampPalette,这两个函数都会返回一个函数,返回的函数的功能是将“间隔”映射到颜色。既然是函数,那么就有对应的参数。colorRamp返回的函数的参数是一个[0,1]向量;colorRampPalette返回的函数的参数是一个整数值,表示所需的颜色数。
Good starting points for interpolation are the “sequential” and “diverging” ColorBrewer palettes in the RColorBrewer package.
见学习RColorBrewer包。
用法示例:
示例1:colorRamp(c("red", "green"))((0:4)/4) ## (x) , x in [0,1]
> (0:4)/4
[1] 0.00 0.25 0.50 0.75 1.00
生成一个0-1之间的向量,该向量有五个值。
> colorRamp(c("red", "green"))( (0:4)/4 )
[,1] [,2] [,3]
[1,] 255.00 0.00 0
[2,] 191.25 63.75 0
[3,] 127.50 127.50 0
[4,] 63.75 191.25 0
[5,] 0.00 255.00 0
colorRamp最后生成的结果是一行一个颜色,每行有3或4的矩阵。
示例2:colorRampPalette(c("blue", "red"))(4) ## (n)
> colorRampPalette(c("blue", "red"))(4)
[1] "#0000FF" "#5500AA" "#AA0055" "#FF0000"
生成的结果是4个颜色值。
#a ramp in opacity of blue values
示例3:colorRampPalette(c(rgb(0,0,1,1), rgb(0,0,1,0)), alpha = TRUE)(8)
> rgb(0,0,1,1)
[1] "#0000FFFF"
> rgb(0,0,1,0)
[1] "#0000FF00"
> colorRampPalette(c(rgb(0,0,1,1), rgb(0,0,1,0)), alpha = TRUE)(8)
[1] "#0000FFFF" "#0000FFDA" "#0000FFB6" "#0000FF91" "#0000FF6D" "#0000FF48" "#0000FF24"
[8] "#0000FF00"
require(graphics)
Here space="rgb" gives palettes that vary only in saturation,as intended.
With space="Lab" the steps are more uniform, but the hues are slightly purple.
在这里,space =“ rgb”提供了仅在饱和度方面有所变化的调色板。
使用space =“ Lab”,步骤更加统一,但色泽略带紫色。
filled.contour(volcano, color.palette =colorRampPalette(c("red", "white", "blue")),asp = 1)
filled.contour(volcano,color.palette=colorRampPalette(c("red", "white", "blue"), space = "Lab"),asp = 1)
filled.contour函数生成轮廓图,轮廓之间的区域用纯色填充(Cleveland称为水平图)。 图的右侧显示了一个键,该键显示颜色如何映射到z值。
volcano是graphics中的一个矩阵
color.palette参数接收一个函数
asp是y/x的长宽比
#插值“sequential” ColorBrewer调色板
YlOrBr <- c("#FFFFD4", "#FED98E", "#FE9929", "#D95F0E", "#993404")
filled.contour(volcano,color.palette = colorRampPalette(YlOrBr, space = "Lab"),asp = 1)
filled.contour(volcano,color.palette = colorRampPalette(YlOrBr, space = "Lab", bias = 0.5),asp = 1)
## 'jet.colors' is "as in Matlab" 'jet.colors'是“在Matlab中”
## (and hurting the eyes by over-saturation)过饱和会伤害眼睛
jet.colors <-colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
filled.contour(volcano, color = jet.colors, asp = 1)
## space="Lab" helps when colors don't form a natural sequence
## 当颜色不形成自然序列时,space =“ Lab”可提供帮助
m <- outer(1:20,1:20,function(x,y) sin(sqrt(x*y)/3))
rgb.palette <- colorRampPalette(c("red", "orange", "blue"),space = "rgb")
Lab.palette <- colorRampPalette(c("red", "orange", "blue"),space = "Lab")
filled.contour(m, col = rgb.palette(20))
filled.contour(m, col = Lab.palette(20))
耐着性子看完帮助,然鹅还是一脸懵。等我缓一缓......