Skip to content

Adjust axis label placement #56

@spencer-tassone

Description

@spencer-tassone

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions