简体   繁体   English

对应于R中数据的热图颜色

[英]Heat map colors corresponding to data in R

I have a one dimensional vector of data in R and I want to find heat map colors that correspond to this data. 我在R中有一个一维数据向量,我想找到与该数据相对应的热图颜色。 For example: 例如:

data = c(12,32,33,41,5)

I then want to find a vector of HEX colors that correspond to that vector - something like higher values have darker colors and lower values have lighter colors or something of that sort. 然后,我想找到一个与该向量相对应的十六进制颜色的向量-诸如较高的值具有较暗的颜色,较低的值具有较浅的颜色或类似的东西。

Are there any packages/functions out there that will do this? 是否有任何软件包/功能可以做到这一点?

Thanks! 谢谢!

By a bit of googling, I found the following function on this link : 通过一点搜索,我在此链接上发现了以下功能:

val2col<-function(z, zlim, col = heat.colors(12), breaks){
 if(!missing(breaks)){
  if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
 }
 if(missing(breaks) & !missing(zlim)){
  breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) 
 }
 if(missing(breaks) & missing(zlim)){
  zlim <- range(z, na.rm=TRUE)
  zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
  zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3)
  breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) 
 }
 colorlevels <- col[((as.vector(z)-breaks[1])/(range(breaks)[2]-range(breaks)[1]))*(length(breaks)-1)+1] # assign colors to heights for each point
 colorlevels
}

It seems that is exactly what you are looking for. 看来这正是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM