【问题标题】:Correlation Corrplot Configuration相关 Corrplot 配置
【发布时间】:2013-10-01 11:51:46
【问题描述】:

我是 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))

我需要在我构建的颜色矩阵内的下部矩阵中显示相关值。我该怎么做?

是否可以从下矩阵中排除主对角线?在这个对角线中,我们总是有完美的相关性。

另一个疑问 - 我想使用星号而不是正方形来显示相关性的显着值。喜欢 (*, , *)。有可能吗?

你们能帮帮我吗?

【问题讨论】:

  • lower.tri 函数有用吗?看看?lower.tri

标签: r correlation r-corrplot


【解决方案1】:

通过一些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 包采取类似的方法。

更新:这是一个在同一个三角形上带有星星和cor的版本:

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)

还注释掉了另一种显示重要性的方式,它将加粗低于阈值的那些,而不是使用星号。这样可能会更清楚,具体取决于您要显示的内容。

【讨论】:

  • 嗨.. @blmoore 是否可以将星星放在一起作为正方形中的相关值并仅使用半矩阵?
  • 是的,我已经更新了我的答案,并举例说明了如何做到这一点,希望对您有所帮助
  • 很容易为颜色添加图例吗?就像我的第一个例子。所以谢谢!!!你帮了我很多! @blmoore :-)
  • corrgram 包中没有实现,但是你可以自己做一个,以上函数的源码展示了颜色是如何生成的。调查发生了什么,如果你无法弄清楚,那么可能会问一个新问题。虽然颜色和相关性显示的是相同的数据,所以图例并不是那么必要
猜你喜欢
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
相关资源
最近更新 更多