我想知道如何在矩阵相关热图中添加另一层重要且必要的复杂性,例如在R2值(-1至1)之外以显著水平星号方式加入p值。请注意,在本问题中并不打算将显著性水平星号或p值作为文本放置在矩阵的每个方格中,而是通过图形化的方法来显示矩阵每个方格中的显著性水平,这需要具备创新思维才能找到最佳解决方案。我搜索了很多,但从未见过一种适当或者说“眼睛友好”的方法来表示显著性水平和反映R系数的标准色调。可重现的数据集在此处找到:http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/。以下是R代码:
矩阵相关性热图应该长这样:
提示和想法以增强解决方案:
- 此代码可能有用,可从此网站获取关于显著性水平星号的想法:
http://ohiodata.blogspot.de/2012/06/correlation-tables-in-r-flagged-with.html
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
- 可以将显著性水平添加为每个正方形的颜色强度,就像透明度美学一样,但我认为这可能不容易解释和捕捉
- 另一个想法是有4种不同大小的正方形对应于星星,当然给非显著性最小的,并增加到最高星星的全尺寸正方形
- 另一个想法是在那些显著的正方形内部包含一个圆圈,圆圈的线条粗细与显著性水平相对应(剩下的3个类别),都是同一种颜色
- 与上述相同,但固定线条厚度,同时为剩余的3个显著级别提供3种颜色
- 也许你会想出更好的主意,谁知道呢?
arm::corrplot
е‡Ѕж•°пјљhttp://rpubs.com/briatte/ggcorr - Fr.GGally
包的一部分,由包的维护者进行了修正和补充。 - Fr.