简体   繁体   English

相关性Corrplot配置

[英]Correlation Corrplot Configuration

I am newbie in R scripts :-) 我是R脚本的新手:-)

I need build a correlation matrix and I´am trying to configurate some parameters to adapt the graph. 我需要建立一个相关矩阵,我试图配置一些参数来适应图形。 I am using the corrplot package. 我正在使用corrplot包。

I Built a corrplot matrix this way: 我用这种方式构建了一个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))

I need show the values of correlation in the lower matrix inside the color matrix that I built. 我需要在我构建的颜色矩阵内的下部矩阵中显示相关值。 How i can do that? 我怎么能这样做?

Is it possible exclude the main diagonal from the lower matrix? 是否可以从下矩阵中排除主对角线? In this diagonl always we have the perfect correlation. 在这个对角线中,我们始终具有完美的相关性。

The other doubt - I want to show the significant values for the correlation using stars instead of squares. 另一个疑问 - 我想用星星代替正方形显示相关的重要值。 like (*, , *). 喜欢 (*, , *)。 Is it possible? 可能吗?

Can you help me guys? 你能帮帮我们吗?

With a bit of hackery you can do this in a very similar R package, corrgram . 有点hackery你可以用一个非常相似的R包, corrgram来做到这corrgram This one allows you to easily define your own panel functions, and helpfully makes theirs easy to view as templates. 这个允许您轻松定义自己的面板功能,并有助于使它们作为模板轻松查看。 Here's the some code and figure produced: 这是生成的一些代码和数字:

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)

在此输入图像描述

The code isn't very clean, as it's mostly patched together functions from the package, but it should give you a good start to get the plot you want. 代码不是很干净,因为它主要是从包中修补功能,但它应该为你提供一个良好的开端来获得你想要的情节。 Possibly you can take a similar approach with the corrplot package too. 可能你也可以采用与corrplot包类似的方法。

update: Here's a version with stars and cor on the same triangle: 更新:这是一个在同一个三角形上有星星和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)

在此输入图像描述

Also commented out is another way of showing significance, it'll bold those below a threshold rather than using stars. 另外注释出另一种显示重要性的方法,它会将那些低于门槛而不是使用星星的那些加粗。 Might be clearer that way, depending on what you want to show. 可能会更清楚,取决于你想要展示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM