2015-03-02 159 views
1

我有一个2乘2的矩阵,我想根据它们的值为数字着色(比如我有0-20的数字,我想要着色0-2 =蓝色; 2-4 =天蓝色...... 12-14 =黄色,18-20 =红色等)。在Excel中,我只能使用Conditional Formatting选项使用3种颜色(请参见图)。任何人都知道我是否可以在另一个程序中使用更多颜色(最好是R)。谢谢! PS:请注意,我并不需要散热图或等高线图,因为我对数字的确切值感兴趣。矩阵中的颜色数字根据它们的值

snapshot of my excel spreadsheet with the 3 colors based on the values of the numbers

+0

谢谢,我纠正了。 – user3290846 2015-03-02 18:56:02

+0

xlsx软件包可能会有用。它似乎可以使用该设置单个单元格的颜色ar字体参数 – OganM 2015-03-02 19:04:00

+0

适用于Mac的Excel 2008,版本12.3.6 – user3290846 2015-03-02 20:00:08

回答

2

这里是一个解决方案,我希望它有助于

# 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])) 
    } 
} 

Numbers coloured by value

+0

哎呀抱歉,您想要某些范围的颜色框?你可以这样做的一种方法是修改(也许是重命名)上面的'findNearestColour()'函数。无论如何,我希望它给你一个出发点,随时给我留言解释。 – roman 2015-03-03 16:19:59

+0

如果你想要相同大小的色块(但更少),你可以减少'nColors'的尺度粒度。 – roman 2015-03-03 16:26:25

+0

请张贴这个的屏幕截图。 – 2015-05-31 02:55:46