简体   繁体   中英

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

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.

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)

在此处输入图像描述

If you only want to change a single value on the axes (eg, 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"

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