I want to get labels on diagonal like in Fig. 3 but with corrplot
like in Fig. 1-2.
I am studying corrplot
manual here for the numeric diagonal labels.
I do not know any method which would allow to put numeric labels on corrplot
diagonal because I have managed to falsify all potential choices.
Falsified things
numeric diagonal labels cannot be set by the following line in the function cor.mtest
colnames(p.mat) <- rownames(p.mat) <- colnames(mat) <- diag.labels
colorlegend
is apparently not the right choice here
corrplot(...)
colorlegend(colbar = grey(1:100 / 100), labels=ids, addlabels = TRUE)
Partial things nice to know more but not limiting us
diag=FALSE, tl.pos="d"
is for a single cell. How can you make tl.pos="d"
for N amount of cells? - - The tl.pos=c("d")
will lead to an error. - - Is tl.pos
necessary for the numerical diagonal labels?Code but also here for different examples where included K.J.J.K's first answer's proposal as a test case but which is shown to be false for the task
library("corrplot")
# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
cor.mtest <- function(mat, diag.labels, ...) {
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) <- diag.labels
p.mat
}
ids <- c(seq(1,11))
M<-cor(mtcars)
p.mat <- cor.mtest(mtcars, diag.labels=ids)
corrplot(M, type="upper", order="hclust", diag=FALSE, # TODO tl.pos=c("d"),
p.mat = p.mat, sig.level = 0.05)
Fig. 1 Output where no expected labels on the diagonal,
Fig. 2 Falsifying K.J.J.K's proposal where no effect on the diagonal labels,
Fig. 3 Example of labels on diagonal with corrgram
found here
Expected output: numeric labels on the diagonal like in Fig. 3 but decoration otherwise wanted like in Fig. (1-2)
Get the code here and you get the output where no change on the diagonal labels in Fig. 2.
OS: Debian 8.5
R: 3.3.1
Ticket in Developer's Github: #71
library("corrplot")
# http://rstudio-pubs-static.s3.amazonaws.com/6382_886fbab74fd5499ba455f11360f78de7.html
# plotcorr(R, col = colorRampPalette(c("#E08214", "white", "#8073AC"))(10), type = "lower")
# http://www.sthda.com/english/wiki/visualize-correlation-matrix-using-correlogram
# corrplot(M, type="upper", order="hclust", tl.col="black", tl.srt=45)
## Compute p-value of correlations
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
M<-cor(mtcars)
# 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
}
# matrix of the p-value of the correlation
p.mat <- cor.mtest(mtcars)
head(p.mat[, 1:5])
corrplot(M, type="upper", order="hclust",
p.mat = p.mat, sig.level = 0.05)
# Leave blank on no significant coefficient
corrplot(M, type="upper", order="hclust",
p.mat = p.mat, sig.level = 0.01, insig = "blank")
col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method="color", col=col(200),
type="upper", order="hclust",
addCoef.col = "black", # Add coefficient of correlation
tl.col="black", tl.srt=45, #Text label color and rotation
# Combine with significance
p.mat = p.mat, sig.level = 0.01, insig = "blank",
# hide correlation coefficient on the principal diagonal
diag=FALSE
)
ids <- c(seq(1,11))
M<-cor(mtcars)
colnames(M)<-ids
rownames(M)<-c("I","told","you","row","names","controls","the","diag","labels","kj","jk")
corrplot(M, type="upper",p.mat = p.mat, sig.level = 0.05)