2017-06-06 148 views
1

我想使用顏色爲綠色,黑色和紅色的pheatmap製作R中的熱圖,並使用範圍從-2到2的圖例,這裏是碼,我用:對於低於和高於閾值的值,使用自定義色標的熱圖貼圖

library(pheatmap) 
my_palette <- colorRampPalette(c("green", "black", "red"))(n = 201) 
colors = c(seq(as.numeric(-2),-0.01,length=100), 0, 
seq(0.01,as.numeric(2),length=100)) 

pheatmap(mFilt_annot_sort_matrix, color = my_palette, breaks = colors, scale = 
"none", cluster_rows = F, cluster_cols = F, margin = c(5,5)) 

的問題是,我想的值小於綠色-2紅色比2的更大,而我的解決方案,這些價值觀是白色的,你能幫幫我嗎?

+0

如果您使用'dput(df)'提供樣本數據集,我可以爲此繪製圖表。看看[如何在R中創建一個很好的重現示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – Masoud

+0

如果有人遇到了稍微不同的需求,可以參考下面的問題:[Pheatmap Color for Specific Value](https://stackoverflow.com/questions/41783245/pheatmap-color-for-specific-value) 。 – Masoud

回答

1

您需要將斷點設置爲-2和+2並在兩者之間建立梯度。您還需要將顏色設置爲小於最小值和大於最大所需值。往下看;

library(pheatmap) 

colors <- c(min(mFilt_annot_sort_matrix),seq(-2,2,by=0.01),max(mFilt_annot_sort_matrix)) 

my_palette <- c("green",colorRampPalette(colors = c("green", "black", "red")) 
                (n = length(colors)-3), "red") 


pheatmap(mFilt_annot_sort_matrix, color = my_palette, breaks = colors, scale = 
"none", cluster_rows = F, cluster_cols = F, margin = c(5,5)) 

使用正常數據的示例;

使用rnorm.within功能我提出以下數據集:

#V1 is random between -4 and 4 
#V2 is less than -2 
#V3 is greater than 2 

df <- data.frame(cbind(rnorm.within(1000, -4, 4) 
,rnorm.within(1000,-4,-3), rnorm.within(1000,3,4))) 

和應用以上(同斷裂和調色板)的程序製作熱圖將得到:

pheatmap(df, color = my_palette, breaks = colors, scale = 
        "none", cluster_rows = F, cluster_cols = F, margin = c(5,5))