【发布时间】:2012-08-25 04:11:03
【问题描述】:
我想知道如何在矩阵相关热图中添加另一层重要且需要的复杂性,例如除了 R2 值(-1 到 1)之外的显着性水平星的方式之后的 p 值?
在这个问题中,不打算将显着性水平星号或 p 值作为文本放在矩阵的每个正方形上,而是在矩阵的每个正方形上以显着性水平的开箱即用图形表示形式显示这一点。我认为只有那些享受创新思维祝福的人才能赢得掌声来解开这种解决方案,以便有最好的方式来表示我们的“半真半真矩阵相关热图”中增加的复杂性组件。我用谷歌搜索了很多,但从未见过合适的,或者我会说一种“眼睛友好”的方式来表示显着性水平加上反映 R 系数的标准色调。
可重现的数据集在这里找到:
http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/
R代码请在下面找到:
library(ggplot2)
library(plyr) # might be not needed here anyway it is a must-have package I think in R
library(reshape2) # to "melt" your dataset
library (scales) # it has a "rescale" function which is needed in heatmaps
library(RColorBrewer) # for convenience of heatmap colors, it reflects your mood sometimes
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba <- as.data.frame(cor(nba[2:ncol(nba)])) # convert the matrix correlations to a dataframe
nba.m <- data.frame(row=rownames(nba),nba) # create a column called "row"
rownames(nba) <- NULL #get rid of row names
nba <- melt(nba)
nba.m$value<-cut(nba.m$value,breaks=c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1),include.lowest=TRUE,label=c("(-0.75,-1)","(-0.5,-0.75)","(-0.25,-0.5)","(0,-0.25)","(0,0.25)","(0.25,0.5)","(0.5,0.75)","(0.75,1)")) # this can be customized to put the correlations in categories using the "cut" function with appropriate labels to show them in the legend, this column now would be discrete and not continuous
nba.m$row <- factor(nba.m$row, levels=rev(unique(as.character(nba.m$variable)))) # reorder the "row" column which would be used as the x axis in the plot after converting it to a factor and ordered now
#now plotting
ggplot(nba.m, aes(row, variable)) +
geom_tile(aes(fill=value),colour="black") +
scale_fill_brewer(palette = "RdYlGn",name="Correlation") # here comes the RColorBrewer package, now if you ask me why did you choose this palette colour I would say look at your battery charge indicator of your mobile for example your shaver, won't be red when gets low? and back to green when charged? This was the inspiration to choose this colour set.
矩阵相关热图应如下所示:
增强解决方案的提示和想法:
- 此代码可能有助于了解从该网站获取的显着性水平星:
http://ohiodata.blogspot.de/2012/06/correlation-tables-in-r-flagged-with.html
R代码:
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " "))) # so 4 categories
- 可以像 alpha 美学一样将显着性级别作为颜色强度添加到每个正方形,但我认为这不容易解释和捕捉
- 另一个想法是有 4 个不同大小的正方形对应于星星,当然给最小的给不重要的,如果最高的星星增加到全尺寸的正方形
- 在这些重要的正方形内包含一个圆圈的另一个想法,圆圈的线的粗细对应于一种颜色的重要性级别(剩下的 3 个类别)
- 与上述相同,但固定线条粗细,同时为剩余的 3 个重要级别提供 3 种颜色
- 可能你想出了更好的主意,谁知道呢?
【问题讨论】:
-
你的代码启发我用ggplot2重写
arm::corrplot函数:rpubs.com/briatte/ggcorr -
效果很好!您能否扩展此功能以使那些不显着的相关性(例如
-
感谢您的反馈。我对这里(和其他地方)使用 $p$-values 持怀疑态度,但我会尝试找出一些方法来标记无关紧要的系数。
-
上面引用的函数现在是
GGally包的一部分,包的维护者进行了更正和添加。 -
(-1, -0,75) 颜色在哪里??使用 c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1),我们应该有 8 个区间和 8 种颜色,而不是 7...
标签: r ggplot2 correlation heatmap significance