如何通过特定标签增加热图的区别

时间:2018-02-16 16:11:57

标签: r heatmap

我有这样的数据

df<- structure(c(1804L, 147L, 2982L, 263L, 1420L, 2225L, 905L, 1654L, 
2018L, 0L, 1L, 1649L, 53L, 1388L, 830L, 0L, 217L, 85L, 831L, 
15499L, 37394L, 568L, 216L, 498L, 78L, 696L, 1831L, 1098L, 3283L, 
2683L, 1L, 2L, 25896L, 114L, 2597L, 2476L, 0L, 83L, 65L, 2983L, 
62277L, 43394L, 3836L, 181L, 4565L, 460L, 4780L, 3961L, 1017L, 
691L, 2348L, 0L, 0L, 1987L, 20L, 831L, 4269L, 0L, 7L, 58L, 64L, 
1670L, 8250L, 1074L, 92L, 4804L, 141L, 1893L, 2444L, 1045L, 1314L, 
1848L, 0L, 0L, 3352L, 50L, 1396L, 11083L, 0L, 130L, 65L, 471L, 
16847L, 24535L, 2057L, 363L, 2739L, 244L, 4473L, 1581L, 1161L, 
4366L, 7743L, 0L, 0L, 4529L, 96L, 2815L, 5407L, 0L, 2451L, 273L, 
14016L, 209852L, 192321L, 791L, 140L, 434L, 143L, 1036L, 1012L, 
264L, 1279L, 987L, 0L, 0L, 4834L, 60L, 1952L, 489L, 0L, 39L, 
29L, 502L, 10482L, 17520L, 2259L, 280L, 1817L, 398L, 3318L, 2375L, 
729L, 2214L, 1601L, 0L, 0L, 5894L, 58L, 4210L, 5245L, 0L, 111L, 
206L, 1683L, 34731L, 57623L, 3868L, 309L, 5879L, 398L, 6534L, 
3191L, 2218L, 2076L, 4670L, 0L, 0L, 4089L, 49L, 1891L, 1506L, 
0L, 96L, 106L, 227L, 5869L, 5844L, 6126L, 527L, 1768L, 298L, 
7844L, 1574L, 922L, 2179L, 6217L, 0L, 0L, 2588L, 42L, 1296L, 
2163L, 0L, 2L, 48L, 129L, 3031L, 18798L, 3354L, 256L, 4796L, 
366L, 4875L, 1910L, 2200L, 3631L, 10243L, 0L, 1L, 2899L, 175L, 
1555L, 6639L, 0L, 1200L, 109L, 4801L, 67661L, 78865L, 849L, 89L, 
639L, 60L, 2623L, 368L, 444L, 823L, 994L, 0L, 0L, 3063L, 25L, 
421L, 838L, 0L, 154L, 49L, 517L, 11004L, 7515L, 2244L, 594L, 
2712L, 327L, 3178L, 5659L, 1828L, 2427L, 10729L, 0L, 0L, 5371L, 
272L, 8191L, 4524L, 0L, 207L, 146L, 4249L, 66773L, 81601L), .Dim = c(21L, 
12L), .Dimnames = list(c("A149564", "A108622", "B178878", "E163072", 
"S184113", "N159216", "G0112149", "EN183160", "EM168329", "EN232793", 
"ENS277545", "E0108691", "E0115008", "E37462", "EN0775", "E0100146", 
"E7430", "ENU653", "E00168314", "T197971", "ENSG0"), c("Sample1", 
"Sample2", "Sample3", "Sample4", "Sample5", "Sample6", "Sample7", 
"Sample8", "Sample9", "Sample10", "Sample11", "Sample12")))

我尝试制作热图并尝试绘制

heatmap(df, col = topo.colors(16), 
        scale="none", cexRow=0.5,
        xlab = "samples", ylab =  "names",
        main = "heatmap",margins = c(5,10)
        )

问题是它没有显示热图,而它显示的是恒定颜色。

我想用颜色差异绘制它,并使其如下所示

enter image description here

请看上面的热图。我想将所有5个样本一起绘制(顶部是heamap中的红色),然后是7个样本(顶部为紫色),其余部分为顶部绿色

我还希望保持名称的顺序与数据中的顺序相同。

我也发现了这篇文章,但我无法弄清楚如何修改我的代码 http://harrycaufield.net/severalog/2016/3/1/heatmaps-in-r-two-ways

2 个答案:

答案 0 :(得分:1)

要使列不再重新排序Colv=NA(在?heatmap中有详细记录)。请注意,如果设置Colv=NA

,则不会有树形图

为列集ColSideColors添加颜色栏(在?heatmap中再次详细记录)

因为您的数据是右倾斜的,所以日志转换可以很好地修复单个单元格的颜色。并且因为您的数据全部&gt; 0我认为最好使用顺序调色板而不是发散调色板。

heatmap(log(df+1), ColSideColors = rep(c("red4","green4"), c(5,7)), Colv = NA,
        col = rev(colorspace::terrain_hcl(100)), scale="none")

enter image description here

答案 1 :(得分:0)

我认为您的问题主要是矩阵中数字之间的巨大差异

例如,如果您记录值(+1以避免零),您的热图会更好看:

df0=log(df+1)
heatmap(df0, col = topo.colors(16), 
        scale="none", cexRow=0.5,
        xlab = "samples", ylab =  "names",
        main = "heatmap",margins = c(5,10)
)

enter image description here