How to have R corrplot title position correct?
Asked Answered
L

1

23

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

library('corrplot')

#options(error=recover) # https://mcmap.net/q/257740/-subscript-out-of-bounds-general-definition-and-solution
#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

Laith answered 9/11, 2016 at 14:22 Comment(0)
L
33

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) # https://mcmap.net/q/535678/-corrplot-parameters-in-r
         )

Output

enter image description here

Laith answered 9/11, 2016 at 14:22 Comment(4)
Can you explain what is the meaning of mar=c(0,0,1,0)?Ambros
plot margins (for sides), inherited from par: stat.ethz.ch/R-manual/R-devel/library/graphics/html/par.htmlFertilization
Quote from the link: "A numerical vector of the form c(bottom, left, top, right) which gives the number of lines of margin to be specified on the four sides of the plot. The default is c(5, 4, 4, 2) + 0.1."Estebanesteem
See github request: github.com/taiyun/corrplot/issues/72Pelson

© 2022 - 2024 — McMap. All rights reserved.