简体   繁体   English

R中的热图(使用heatmap()函数)

[英]Heatmap in R (using the heatmap() function)

I have a correlation matrix and want to obtain a heat map from cold (negative correlations) to red (positive correlations), with white being 0 correlations. 我有一个相关矩阵,想要获得从冷(负相关)到红色(正相关)的热图,白色为0相关。

Now, the heatmap command seems to somehow average, even if I say 'scale=none', meaning that the average correlation is portrayed as white (which, in my case, is 0.2, meaning that all 0 correlations are slightly blue). 现在,即使我说“ scale = none”,heatmap命令似乎也以某种方式是平均的,这意味着平均相关性被描绘成白色(在我的情况下是0.2,意味着所有0相关性都略带蓝色)。

Can you help me fix this? 你能帮我解决这个问题吗? Thank you 谢谢

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")))

在此处输入图片说明

This is not an elegant solution, but it seems to get the job done. 这不是一个很好的解决方案,但似乎可以完成工作。 The gist is to restrict the spectrum to the values taken by the correlation matrix, and to make this smoother the palette is stretched from the 11-value maximum provided by brewer.pal (using an odd number of repeats so that the median remains an integer). 要点是将频谱限制在相关矩阵所取值的范围内,并使其更平滑,以从brewer.pal提供的11个值的最大值brewer.pal (使用奇数次重复,以使中位数保持为整数) )。

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

For correlation matrix you can also use corrplot or corrgram libraries that are designed especially for this purpose. 对于相关矩阵,您还可以使用专门为此设计的corrplotcorrgram库。 They work out of the box and also have additional plotting features. 它们开箱即用,还具有其他绘图功能。 In R Graphics Cookbook you can find examples of how to draw this kind of plot with ggplot2 using geom_tile() or geom_raster() functions. R图形食谱 ,你可以找到例子的如何绘制这样的情节用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))

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM