简体   繁体   中英

Add Axis Labels to 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:

在此处输入图像描述

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

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, eg

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

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')
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM