使用colorRampPalette进行lapply

时间:2015-04-23 07:51:41

标签: r heatmap

我有以下矢量:

colorRampPalette

我想要做的是使用library(RColorBrewer) mycolorfunction <- colorRampPalette(brewer.pal(9,"Set1")) unlist(lapply(as.list(x),mycolorfunction)) 为每个数字指定颜色。 但是为什么这个命令提供的输出比我预期的要多:

 [1] "#E41A1C" "#E41A1C" "#E41A1C" "#E41A1C" "#999999" "#E41A1C" "#999999" "#E41A1C" "#999999" "#E41A1C" "#999999"
[12] "#E41A1C" "#999999" "#E41A1C" "#999999" "#E41A1C" "#999999" "#E41A1C" "#FF7F00" "#999999" "#E41A1C" "#FF7F00"
[23] "#999999" "#E41A1C" "#FF7F00" "#999999" "#E41A1C" "#7E6E85" "#E1C62F" "#999999" "#E41A1C" "#4DAF4A" "#FF7F00"
[34] "#A65628" "#999999" "#E41A1C" "#4DAF4A" "#FF7F00" "#A65628" "#999999" "#E41A1C" "#4DAF4A" "#FF7F00" "#A65628"
[45] "#999999" "#E41A1C" "#4DAF4A" "#FF7F00" "#A65628" "#999999" "#E41A1C" "#4DAF4A" "#FF7F00" "#A65628" "#999999"
[56] "#E41A1C" "#4DAF4A" "#FF7F00" "#A65628" "#999999"

它给出了这个:

RowSideColors

我希望它能产生大小为20的矢量,只有5色Hex。 什么是正确的方法?

在一天结束时,我想在heatmap.2中将最终向量用作{{1}}参数。

1 个答案:

答案 0 :(得分:2)

直接使用函数mycolorfunctionx作为索引的向量:

x <- c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5)
library(RColorBrewer)
mycolorfunction <- colorRampPalette(brewer.pal(9,"Set1"))

mycolorfunction(max(x))[x]
#  [1] "#E41A1C" "#E41A1C" "#E41A1C" "#4DAF4A" "#4DAF4A" "#4DAF4A" 
#  [7] "#4DAF4A" "#4DAF4A" "#4DAF4A" "#4DAF4A" "#FF7F00" "#FF7F00"
# [13] "#FF7F00" "#A65628" "#999999" "#999999" "#999999" "#999999"
# [19] "#999999" "#999999"