2015-02-11 72 views
0

我有一个相关矩阵,并希望获得从冷(负相关)到红(正相关)的热图,其中白色为0相关。R中的热图(使用heatmap()函数)

现在,即使我说'scale = none',这意味着平均相关性被描绘为白色(在我的情况下,它是0.2,这意味着所有的0相关性都是轻微的蓝色)。

你能帮我解决这个问题吗?谢谢

library(stats) 
library(gplots) 
library(RColorBrewer) 
heatmap(graph.g,Colv = NA, Rowv=NA, revC=T, scale='none', 
    xlab= "IDS-C30 symptoms", main = "Heatmap of glasso associations", 
    col = rev(brewer.pal(11,"RdBu"))) 

enter image description here

回答

2

对于相关矩阵,你还可以使用那些特别为此设计corrplotcorrgram库。他们开箱即用,还具有额外的绘图功能。在R Graphics Cookbook中,您可以找到examples如何使用ggplot2使用geom_tile()geom_raster()函数绘制这种绘图。

library(corrplot) 
library(corrgram) 
library(ggplot2) 
library(reshape2) 

corrplot(cor(mtcars)) 
corrplot(cor(mtcars), method="color") 
corrgram(cor(mtcars)) 
corrgram(cor(mtcars), lower.panel=panel.shade, 
     upper.panel=panel.pie) 

p <- ggplot(melt(cor(mtcars)), aes(x=Var1, y=Var2, fill=value)) 
p + geom_tile() + scale_fill_gradient2(midpoint=0, limits=c(-1, 1)) 

enter image description here enter image description here enter image description here enter image description here enter image description here

1

这不是一个完美的解决方案,但它似乎把工作做好。要点是要将光谱限制到相关矩阵所取的值,并使其更平滑,调色板从brewer.pal提供的11值最大值(使用奇数重复以使中值保持整数)拉伸。

vec <- rep(rev(brewer.pal(11,"RdBu")), each = 101) # stretched palette 
med <- (length(vec) + 1)/2 # middle of palette 
rad <- length(vec) - med # radius of palette 
min.g <- med + min(graph.g) * rad # lowest value taken 
max.g <- med + max(graph.g) * rad # highest value taken 
heatmap(graph.g,Colv = NA, Rowv=NA, revC=T, scale='none', 
    xlab= "IDS-C30 symptoms", main = "Heatmap of glasso associations", 
    col = vec[min.g:max.g]) # palette restricted to realized values 
+0

惊人的,谢谢! – Torvon 2015-02-11 17:36:29