我有一个2 x 2的矩阵,我想根据它们的值给数字上色(假设我有0-20的数字,我想给0-2=蓝色;2-4=天蓝色...12-14=黄色,18-20=红色,依此类推。在Excel中,使用条件格式选项时,我只能使用3种颜色(见下图)。谁知道我是否可以在另一个程序中有更多的颜色(最好是R)。谢谢!
注:请注意,我不需要热图或等高线图本身,因为我对数字的精确值感兴趣。

发布于 2015-03-04 00:19:09
这里有一个解决方案,我希望它能有所帮助。
# you need this for the colour ramp
library(RColorBrewer)
# setup
rows <- 10
collumns <- 10
# data matrix
zVals <- round(rnorm(rows*collumns), 2)
z <- matrix(zVals, rows, collumns)
# pick the number of colours (granularity of colour scale)
nColors <- 100
# create the colour pallete
cols <-colorRampPalette(colors=c("blue", "grey", "red"))(nColors)
# get a zScale for the colours
zScale <- seq(min(z), max(z), length.out = nColors)
# function that returns the nearest colour given a value of z
findNearestColour <- function(x) {
colorIndex <- which(abs(zScale - x) == min(abs(zScale - x)))
return(cols[colorIndex])
}
# empty plot
plot(1, 1, type = "n", xlim = c(1, rows), ylim = c(1, collumns),
axes = F, xlab = "", ylab = "")
# populate it with the data
for(r in 1:rows){
for(c in 1:collumns){
text(c, r, z[c,r], col = findNearestColour(z[c,r]))
}
}

https://stackoverflow.com/questions/28816962
复制相似问题