首页 文章

R:heatmap颜色与数据不对应

提问于
浏览
1

我是R的新手,对热图有疑问 .

我在行和列中列出了不同条件下各自的丰度 . 这个数据被归一化,因此在每一行中有一个代表最大值,其余的值在1到0之间 . 我将1设置为红色,将0设置为白色 .

当我使用刻度绘制热图时,我在每一行中都没有红色正方形(= 1) .

这是我一直在使用的代码:

df <- read.table("my_data.txt", header = TRUE)
row.names(df) <- df$name
df <- df[, 2:21]
df_matrix <- data.matrix(df)
breaks = seq(0, max(df), length.out = 1000)
gradient1 = colorpanel( sum( breaks[-1]<=1 ), "white", "red" )
gradient2 = colorpanel( sum( breaks[-1]>1), "black", "red")
hm.colors = c(gradient1, gradient2)
df_heatmap <- heatmap(df_matrix, 
                  Rowv=NA, 
                  Colv=NA, 
                  col=hm.colors, 
                  scale = "column", 
                  margins = c(5,10))

我也尝试过其他选择,例如:

df_heatmap <- heatmap(df_matrix, 
                  Rowv=NA, 
                  Colv=NA, 
                  col=heat.colors(256),
                  scale = "column", 
                  margins = c(5,10))

如果您对如何调整它有任何建议,那就太棒了 . 感谢您的帮助!

1 回答

  • 0

    你想要缩小比例 . 来自 ?heatmapscale "centers and scales values in either the row or column direction."

    x <- matrix(rnorm(100), nrow=10)
    x <- apply(x, 1, function(z) abs(z)/(max(z)))
    
    breaks = seq(0, max(x), length.out = 1000)
    gradient1 = colorpanel( sum( breaks[-1]<=1 ), "white", "red" )
    gradient2 = colorpanel( sum( breaks[-1]>1), "black", "red")
    hm.colors = c(gradient1, gradient2)
    

    比例等于“列”

    df_heatmap <- heatmap(x, 
                          Rowv=NA, 
                          Colv=NA, 
                          col=hm.colors, 
                          scale = "column", 
                          margins = c(5,10))
    

    scale等于“none”

    df_heatmap <- heatmap(x, 
                      Rowv=NA, 
                      Colv=NA, 
                      col=hm.colors, 
                      scale = "none", 
                      margins = c(5,10))
    

相关问题