如何在corrplot中添加数据框名称作为标题?

时间:2017-08-10 14:34:49

标签: r dataframe title r-corrplot

我将我的数据集压缩到一些数据框中,我想创建一个包含所有相关图的pdf文件,但每个图都应该以数据框的名称作为标题。这是我的代码:

# Function 1 for correlation matrix
cor.mtest <- function(mat, conf.level = 0.95){
  mat <- as.matrix(mat)
  n <- ncol(mat)
  p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n)
  diag(p.mat) <- 0
  diag(lowCI.mat) <- diag(uppCI.mat) <- 1
  for(i in 1:(n-1)){
    for(j in (i+1):n){
      tmp <- cor.test(mat[,i], mat[,j], conf.level = conf.level)
      p.mat[i,j] <- p.mat[j,i] <- tmp$p.value
      lowCI.mat[i,j] <- lowCI.mat[j,i] <- tmp$conf.int[1]
      uppCI.mat[i,j] <- uppCI.mat[j,i] <- tmp$conf.int[2]
    }
  }
  return(list(p.mat, lowCI.mat, uppCI.mat))
}

# Function 2 for correlation plot
correlation <- function(obj){
  dat <- select_if(obj, is.numeric)
  pval.mat <- cor.mtest(dat,0.95)
  title <- #NAME OF THE DATA FRAME#
  dat1 <- corrplot(cor(dat,method="pearson"),method="number",type="upper",sig.level=0.05,p.mat = pval.mat[[1]], tl.pos="upper")
  dat2 <- corrplot(cor(dat,method="pearson"),add=T,type="lower",method="square", tl.pos="n", title=title, mar=c(0,0,1,0))
  return(list(dat1, dat2))
}

# Saving all correlation in a PDF file
par(ask=F)
pdf('correlations.pdf')
corrbv1nona <- correlation(bv1nona)
corrbv2nona <- correlation(bv2nona)
corrbv3nona <- correlation(bv3nona)
corrbv4 <- correlation(bv4)
corrfull1nona <- correlation(full1nona)
corrfull2nona <- correlation(full2nona)
corrfull3nona <- correlation(full3nona)
corrfull4 <- correlation(full4)
corrsl1nona <- correlation(sl1nona)
corrsl2nona <- correlation(sl2nona)
corrsl3nona <- correlation(sl3nona)
corrsl4 <- correlation(sl4)
dev.off()

1 个答案:

答案 0 :(得分:0)

您可以使用match.call提取名称:

title <- as.character(as.list(match.call())$obj)

首次使用correlation时,应将title设置为"bv1nona"