我想知道如何在矩阵相关热图中添加另一层重要且需要的复杂性,例如除了 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 种颜色
- 也许你会想出更好的主意,谁知道?