ggcorplot() : Changing Variable Labels on X and Y Axis

487 Views Asked by At

I'm currently working on a correlation plot with gradient for a dataset involving social factors and outcomes such as grades.

The variable names are not that accessible, and I was wondering how to change, for example, "famrel" to "Family Relationship" on the axis.

I am using ggcorrplot() as well as ggplotly to add interactivity.

Any help would be much appreciated! I've been googling for two hours but cannot for the life of me find an applicable solution that doesn't involve altering the original dataframe.

df_corr <- select_if(df, is.numeric)
df_corr

corr <- round(cor(df_corr), 1)
p.mat <- cor_pmat(df_corr)

corr.plot <- ggcorrplot(corr, 
  hc.order = TRUE,
  type = "lower", 
  )

ggplotly(corr.plot)

Above is my code; I have also attached a screenshot of the resulting chart.

I tried googling as well as searching stack overflow for the answer to my question, but was unsuccessful.

1

There are 1 best solutions below

0
On

In addition to @Kat's option of directly editing the dataframe, you can also rename the column names and rownames directly in corr. Also be aware that select_if has been superseded.

library(tidyverse)
library(ggcorrplot)
library(plotly)

data(mtcars)
df <- mtcars %>% select(mpg, gear, disp, drat)
df_corr <- df %>% select(where(is.numeric)) # changed here
corr <- round(cor(df_corr), 1)
colnames(corr) <- c("mpg_new", "gear_new", "disp_new", "drat_new")
rownames(corr) <- c("mpg_new", "gear_new", "disp_new", "drat_new")
p.mat <- cor_pmat(df_corr)
corr.plot <- ggcorrplot(corr, hc.order = TRUE, type = "lower")
ggplotly(corr.plot)

enter image description here

If you only want to change a single value on the axes (e.g., famrel), then you can edit a single rowname and a single column name like this:

colnames(corr)[colnames(corr) == "mpg"] <- "mpg_new"
rownames(corr)[rownames(corr) == "mpg"] <- "mpg_new"