【问题标题】:How to plot different and independent color gradient legend in heatmap.2 (R)如何在 heatmap.2 (R) 中绘制不同且独立的颜色渐变图例
【发布时间】:2021-10-04 17:34:33
【问题描述】:

我从计算中得到一个矩阵,其值在 0 到 4 之间。我使用函数 heatmap.2 进行绘图。

这个想法是在单元格值的函数中具有一个颜色代码和不同的渐变:

从 0 到 0.99:红色渐变

从 1 到 1.99:黄色渐变

从 2 到 2.99:绿色渐变

从 3 到 4:灰度渐变

如您所见,渐变不是独立的,颜色从一个范围平滑地变化到另一个范围(例如,我将橙色从红色变为黄色)。如何获得 4 个不同且独立的梯度?意思是,从0到0.99只有红色渐变,从1到1.99只有黄色渐变等

这是我使用的代码(我为示例创建了一个随机矩阵):

library(gplots)

# create a matrix of random values from [0, 4]
random.matrix  <- matrix(runif(100, min = 0, max = 4), nrow = 10)

quantile.range <- quantile(random.matrix, probs = seq(0, 1))
palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
color.palette  <- colorRampPalette(c("red", "yellow", "green", "grey"))(length(palette.breaks)-1)
heatmap.2(
  random.matrix,
  dendrogram = "none",
  scale      = "none",
  trace      = "none",
  symbreaks=FALSE,
  symkey=FALSE, 
  key        = TRUE,
  key.title = 'class information',
  key.xlab = 'class number',
  keysize = 2,
  #( "bottom.margin", "left.margin", "top.margin", "right.margin" )
  key.par=list(mar=c(3,3.5,2,0)),
  labRow     = NA,
  labCol     = NA,
  col    = color.palette,
  breaks = palette.breaks,
)
legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))

剧情如下:

【问题讨论】:

    标签: r matrix plot heatmap gplots


    【解决方案1】:

    我是这样找到的:

    # gplots contains the heatmap.2 function
    library(gplots)
    
    # create a matrix of random values from [0, 4]
    random.matrix  <- matrix(runif(100, min = 0, max = 4), nrow = 10)
    
    quantile.range <- quantile(random.matrix, probs = seq(0, 1))
    palette.breaks <- seq(quantile.range["0%"], quantile.range["100%"], 0.25)
    
    colfunc1 <- colorRampPalette(c("white", "red"))
    colfunc2 <- colorRampPalette(c("white", "yellow"))
    colfunc3 <- colorRampPalette(c("white", "green"))
    colfunc4 <- colorRampPalette(c("grey", "black"))
    
    color.palette  <- colorRampPalette(c(colfunc1(4), colfunc2(4), colfunc3(4), colfunc4(3)))(length(palette.breaks)-1)
    
    heatmap.2(
      random.matrix,
      dendrogram = "none",
      scale      = "none",
      trace      = "none",
      symbreaks=FALSE,
      symkey=FALSE, 
      key        = TRUE,
      key.title = 'class information',
      key.xlab = 'class number',
      keysize = 2,
      #( "bottom.margin", "left.margin", "top.margin", "right.margin" )
      key.par=list(mar=c(3,3.5,2,0)),
      labRow     = NA,
      labCol     = NA,
      col    = color.palette,
      breaks = palette.breaks,
    )
    legend(x="topright", legend=c("class 1: city", "class 2: crops", "class 3: forest", "class 4: road"),fill=c("red", "yellow", "green", "grey", ""))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      相关资源
      最近更新 更多