Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corrplot top labels distance

Tags:

r

r-corrplot

I am using corrplot library to plot correlation matrices in this manner :

library(corrplot)
car_matrix <- cor(mtcars, use = "pairwise.complete.obs", method = "pearson")

png("Cars matrix.png", width = 1800, height = 1800, res = 300)

corrplot(car_matrix, method = "number", type = "upper",
         col = RColorBrewer::brewer.pal(n = 8, name = "Greys"), tl.col = "black", 
         number.cex = 0.75, tl.srt = 0)

dev.off()

I use tl.srt = 0 parameter so that all the labels face upwards. Although the orientation is then corrected, there is no longer any spacing between top labels and the matrix (see image).

I tried to modify the spacing using tl.offset = 1, which adds spacing to both top and left labels. But since left labels already have default spacing, tl.offset = 1 creates excessive spacing for them.

I can't find a parameter in corrplot that allows to choose the spacing for top/left labels independently. Is there any other way to do so? enter image description here

like image 307
arnaudm Avatar asked Jul 21 '26 05:07

arnaudm


1 Answers

You could add a break to each name of the matrix to add some space like this:

data("mtcars")
library(corrplot)
library(RColorBrewer)

car_matrix <- cor(mtcars, use = "pairwise.complete.obs", method = "pearson")
colnames(car_matrix) <- paste0(colnames(car_matrix), "\n")
png("Cars matrix.png", width = 1800, height = 1800, res = 300)

corrplot(car_matrix, method = "number", type = "upper",
         col = brewer.pal(n = 8, name = "Greys"), tl.col = "black",
         number.cex = 0.75, tl.srt = 0)

enter image description here

like image 181
Quinten Avatar answered Jul 22 '26 20:07

Quinten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!