ggcorrplot icon indicating copy to clipboard operation
ggcorrplot copied to clipboard

Adjust axis label placement

Open spencer-tassone opened this issue 8 months ago • 0 comments

Sometimes it is helpful to have the axis labels next to the squares (or circles) of the correlation plot. Below is an example of the problem and the solution which comes from stack overflow. It would be helpful if this could be incorporated into ggcorrplot.

library(tidyverse)
library(ggcorrplot)

# Example data
M <- cor(mtcars)

# Has the y-axis labels inline ontop of one another
ggcorrplot(M,
           type = "lower",
           outline.color = 'black',
           lab = TRUE,
           tl.srt = 0,
           ggtheme = ggplot2::theme_void)


# Here is how to offeset the y-axis labels to be next to the square rather than stacked inline.
labs <- tibble(label = rev(head(row.names(M), -1)), 
               x = rev(seq(label)) - 0.9,
               y = rev(seq(label)))

ggcorrplot(M,
           type = "lower",
           outline.color = 'black',
           lab = TRUE,
           tl.srt = 0,
           ggtheme = ggplot2::theme_void) +
  scale_y_discrete(labels = NULL) +
  geom_text(aes(label = label, x = x, y = y), data = labs, 
            inherit.aes = FALSE, hjust = 1) +
  coord_equal(clip = "off")
#> Coordinate system already present. Adding new coordinate system, which will
#> replace the existing one.

Created on 2025-05-12 with reprex v2.1.1

spencer-tassone avatar May 12 '25 14:05 spencer-tassone