Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Axis Labels to ggcorrplot?

Tags:

r

ggcorrplot

How do I add axis labels to a ggcorrplot? I have a plot of the correlation between two separate attempts at a questionnaire. X axis represents the first attempt, Y axis represents the second attempt. I want to label the axes to show that this is what is being represented here.

My code looks like this:

corrQData <- round(cor(Attempt1, Attempt2), digits = 1)

ggcorrplot(corrQData, 
           outline.color = "white",
           ggtheme = theme_bw(),
           colors = c("#F8696B", "#FFEB84", "#63BE7B"),
           legend.title = "Correlation",
           lab = TRUE,
           lab_size = 3,
           tl.cex = 8,
           tl.srt = 0,
           title = "Correlation Between Questionnaire Attempts") +
  theme(plot.title = element_text(hjust = 0.5, size=10), legend.title = element_text(size = 10))

My plot looks like this:

enter image description here

I tried adding + scale_x_discreet(name = "Attempt 1") to the end of my ggcorrplot code but it didn't do anything.

like image 568
Student228 Avatar asked Mar 13 '26 06:03

Student228


2 Answers

This can be achieved using {ggcorrplot2}.

While the the ggcorrplot developers may have good reasons for not including axis labels (eg they should not be necessary for a pairwise matrix), there are certain cases where it may make sense to desire specifying the axis labels and axis ticks. Eg. when comparing correlations across two different sets of variables, and excluding any comparisons made within sets.


ggcorrplot2::ggcorrplot() suppresses axis labels by internally using the following

theme(axis.title = element_blank())

If you want to over-rule this and provide axis labels, you must both specify the labels to be added and re-specify the axis.title behavior, e.g

corrdata <- round(cor(mtcars), 1)

ggcorrplot2::ggcorrplot(corrdata) + 
  ggplot2::labs(x = 'Variable Set 1', y = 'Variable Set 2') +
  ggplot2::theme(axis.title = element_text())

One can also manually specify the axis tick labels as well using

corrdata <- round(cor(mtcars), 1)

ggcorrplot2::ggcorrplot(corrdata) + 
  ggplot2::labs(x = 'Variable Set 1', y = 'Variable Set 2') +
  ggplot2::theme(axis.title = element_text())+
  ggplot2::scale_x_continuous(breaks = 1:dim(corrdata)[1], labels = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), 
      expand = c(0, 0))+
  ggplot2::scale_y_reverse(breaks = 1:dim(corrdata)[1], labels = c("l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v"), 
      expand = c(0, 0))

Plot with modified axis labeling

like image 108
Max Segnitz Avatar answered Mar 14 '26 19:03

Max Segnitz


You have to override the default behaviour of ggcorrplot which is not to display axis labels. Do that by adding labels with ggplot2::labs() (scale_x_discrete(name = ...) works fine also) and changing the plot's theme

library(ggcorrplot)
corrdata <- round(cor(mtcars), 1)

ggcorrplot(corrdata) + 
  ggplot2::labs(x = 'X label', y = 'Y label') +
  ggplot2::theme(
    axis.title.x = element_text(angle = 0, color = 'grey20'),
    axis.title.y = element_text(angle = 90, color = 'grey20')
  )
like image 45
Romain Avatar answered Mar 14 '26 19:03

Romain



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!