我是R剧本的新手: - )
我需要建立一个相关矩阵,我试图配置一些参数来调整图形。我正在使用corrplot
包。
我用这种方式建立了一个corrplot矩阵:
corrplot(cor(d1[,2:14], d1[,2:14]), method=c("color"),
bg = "white", addgrid.col = "gray50",
tl.cex=1, type="lower", tl.col = "black",
col = colorRampPalette(c("red","white","blue"))(100))
我需要在我构建的颜色矩阵内的下部矩阵中显示相关值。我怎么能这样做?
是否可以从下方矩阵中排除主对角线?在这个对角线中,我们总是有完美的相关性。
另一个疑问 - 我想用星星代替正方形来显示相关性的重要值。喜欢 (*, , *)。有可能吗?
你能帮帮我们吗?
答案 0 :(得分:10)
有一点hackery你可以在一个非常相似的R包中corrgram
执行此操作。这个允许您轻松定义自己的面板功能,并有助于使它们作为模板轻松查看。这是生成的一些代码和数字:
set.seed(42)
library(corrgram)
# This panel adds significance starts, or NS for not significant
panel.signif <- function (x, y, corr = NULL, col.regions, digits = 2, cex.cor,
...) {
usr <- par("usr")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
results <- cor.test(x, y, alternative = "two.sided")
est <- results$p.value
stars <- ifelse(est < 5e-4, "***",
ifelse(est < 5e-3, "**",
ifelse(est < 5e-2, "*", "NS")))
cex.cor <- 0.4/strwidth(stars)
text(0.5, 0.5, stars, cex = cex.cor)
}
# This panel combines edits the "shade" panel from the package
# to overlay the correlation value as requested
panel.shadeNtext <- function (x, y, corr = NULL, col.regions, ...)
{
if (is.null(corr))
corr <- cor(x, y, use = "pair")
ncol <- 14
pal <- col.regions(ncol)
col.ind <- as.numeric(cut(corr, breaks = seq(from = -1, to = 1,
length = ncol + 1), include.lowest = TRUE))
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col = pal[col.ind],
border = NA)
box(col = "lightgray")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- formatC(corr, digits = 2, format = "f")
cex.cor <- .8/strwidth("-X.xx")
text(0.5, 0.5, r, cex = cex.cor)
}
# Generate some sample data
sample.data <- matrix(rnorm(100), ncol=10)
# Call the corrgram function with the new panel functions
# NB: call on the data, not the correlation matrix
corrgram(sample.data, type="data", lower.panel=panel.shadeNtext,
upper.panel=panel.signif)
代码不是很干净,因为它主要是从包中修补功能,但它应该为你提供一个良好的开端来获得你想要的情节。可能您也可以采用corrplot
包的类似方法。
更新:以下是同一个三角形上有星星和角落的版本:
panel.shadeNtext <- function (x, y, corr = NULL, col.regions, ...)
{
corr <- cor(x, y, use = "pair")
results <- cor.test(x, y, alternative = "two.sided")
est <- results$p.value
stars <- ifelse(est < 5e-4, "***",
ifelse(est < 5e-3, "**",
ifelse(est < 5e-2, "*", "")))
ncol <- 14
pal <- col.regions(ncol)
col.ind <- as.numeric(cut(corr, breaks = seq(from = -1, to = 1,
length = ncol + 1), include.lowest = TRUE))
usr <- par("usr")
rect(usr[1], usr[3], usr[2], usr[4], col = pal[col.ind],
border = NA)
box(col = "lightgray")
on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
r <- formatC(corr, digits = 2, format = "f")
cex.cor <- .8/strwidth("-X.xx")
fonts <- ifelse(stars != "", 2,1)
# option 1: stars:
text(0.5, 0.4, paste0(r,"\n", stars), cex = cex.cor)
# option 2: bolding:
#text(0.5, 0.5, r, cex = cex.cor, font=fonts)
}
# Generate some sample data
sample.data <- matrix(rnorm(100), ncol=10)
# Call the corrgram function with the new panel functions
# NB: call on the data, not the correlation matrix
corrgram(sample.data, type="data", lower.panel=panel.shadeNtext,
upper.panel=NULL)
另外注释是显示重要性的另一种方式,它会将那些低于门槛而不是使用星星加粗。可能会更清楚,取决于你想要展示的内容。