Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have R corrplot title position correct?

Code and its output where title is wrongly positioned outside the window page:

library('corrplot')

#options(error=recover) # http://stackoverflow.com/a/15031603/54964
#debugger()
# load("last.dump.rda"); debugger(last.dump) # run if fail
options(error=function() dump.frames(to.file=TRUE))

# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat) 
  p.mat
}

M <- cor(mtcars)

p.mat <- cor.mtest(M)

title <- "ECG p-value significance"
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),  
     diag=FALSE, # tl.pos="d", 
         type="upper", order="hclust", 
     title=title, 
         addCoef.col = "black", # Add coefficient of correlation
         # Combine with significance
         p.mat = p.mat, sig.level = 0.05, insig = "blank" 
         # hide correlation coefficient on the principal diagonal
         )

Fig. 1 Output

enter image description here

R: 3.3.1
OS: Debian 8.5
Related ticket: #72

like image 460
Léo Léopold Hertz 준영 Avatar asked Nov 09 '16 14:11

Léo Léopold Hertz 준영


People also ask

What correlation does Corrplot use?

[ R , PValue ] = corrplot( Tbl ) plots the Pearson's correlation coefficients between all pairs of variables in the table or timetable Tbl , and also returns tables for the correlation matrix R and matrix of p-values PValue .

How do you make a Corrplot bigger in R?

The correlation coefficient value size in correlation matrix plot created by using corrplot function ranges from 0 to 1, 0 referring to the smallest and 1 referring to the largest, by default it is 1. To change this size, we need to use number. cex argument.

How do you visualize a correlation in R?

The easiest way to visualize a correlation matrix in R is to use the package corrplot. In our previous article we also provided a quick-start guide for visualizing a correlation matrix using ggplot2. Another solution is to use the function ggcorr() in ggally package.


1 Answers

Code where mar=c(0,0,1,0) fixes the thing

library('corrplot')

# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat) 
  p.mat
}

M <- cor(mtcars)

p.mat <- cor.mtest(M)

title <- "ECG p-value significance"
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),  
     diag=FALSE, # tl.pos="d", 
         type="upper", order="hclust", 
     title=title, 
         addCoef.col = "black", # Add coefficient of correlation
         # Combine with significance
         p.mat = p.mat, sig.level = 0.05, insig = "blank", 
         # hide correlation coefficient on the principal diagonal
     mar=c(0,0,1,0) # http://stackoverflow.com/a/14754408/54964
         )

Output

enter image description here

like image 128
Léo Léopold Hertz 준영 Avatar answered Sep 19 '22 15:09

Léo Léopold Hertz 준영