在Pheatmap中绘制热图的特定值

时间:2019-05-29 17:55:36

标签: r plot heatmap pheatmap

我有一个像这样的数据框:

gene    s1  s2  s3
1   -3.83   -8.17   -8.59
2   0.33    -4.51   -7.27
3   0.15    -5.26   -6.2
4   -0.08   -6.13   -5.95
5   -1.15   -4.82   -5.75
6   -0.99   -4.11   -4.85
7   0.42    -4.18   -4.54
8   -0.32   -3.43   -4.4
9   -0.72   -3.37   -4.39

我想使用pheatmap制作一个热图,如果它低于-4,则应为绿色,而超过+4的则应为红色,而介于两者之间的则应为红色/绿色。我也不想扩展我的数据并且没有聚类。到目前为止,我在R中有此代码:

d <- read.table("test.txt", header = TRUE, sep = "\t", row.names = 1, quote = "")

pheatmap(as.matrix(d),  # matrix
         scale = "none",         # z score scaling applied to rows 
         cluster_cols=FALSE,    # do not cluster columns
         cluster_rows = FALSE,
         treeheight_row=0,      # do not show row dendrogram
         show_rownames=FALSE,   # do not show row names i.e gene names
         main = "test.txt",
         color = colorRampPalette(c("#0016DB","#FFFFFF","#FFFF00"))(50),
         )

如何使用上面提到的配色方案绘制此图。

谢谢

1 个答案:

答案 0 :(得分:1)

d <-read.table(text="gene      s1      s2      s3
                        1   -3.83   -8.17   -8.59
                        2    0.33   -4.51   -7.27
                        3    0.15   -5.26   -6.20  
                        4   -0.08   -6.13   -5.95
                        5   -1.15   -4.82   -5.75
                        6   -0.99   -4.11   -4.85
                        7   0.42    -4.18   -4.54
                        8   -0.32   -3.43   -4.40
                        9   -0.72   -3.37   -4.39", header=T)

library(pheatmap)

my_colors <- c(min(d),seq(-4,4,by=0.01),max(d))

my_palette <- c("green",colorRampPalette(colors = c("green", "red"))
                (n = length(my_colors)-2), "red")

pheatmap(as.matrix(d),  
         scale = "none",         
         cluster_cols=FALSE,    
         cluster_rows = FALSE,
         treeheight_row=0,      
         show_rownames=FALSE,   
         main = "test.txt",
         color = my_palette,
         breaks = my_colors)

reprex package(v0.3.0)于2019-05-29创建