2017-08-10 82 views
0

我將我的數據集分成了一些數據框,我想創建一個包含所有相關性圖的pdf文件,但每個圖都應該以數據框的名稱作爲標題。這裏是我的代碼:如何添加數據框名稱作爲corrplot中的標題?

# 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() 

回答

0

您可以match.call提取名稱:

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

在第一次使用的correlation,這應該設置title"bv1nona"

1

或者你也可以提取與

title <- deparse(substitute(obj)) 
名稱
相關問題