简体   繁体   English

ggcorplot():更改 X 轴和 Y 轴上的变量标签

[英]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.我目前正在研究 plot 与涉及社会因素和成绩等结果的数据集的梯度相关性。

The variable names are not that accessible, and I was wondering how to change, for example, "famrel" to "Family Relationship" on the axis.变量名不是那么容易访问,我想知道如何将轴上的“famrel”更改为“家庭关系”。

I am using ggcorrplot() as well as ggplotly to add interactivity.我正在使用 ggcorrplot() 以及 ggplotly 来添加交互性。

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 .除了@Kat 直接编辑 dataframe 的选项外,您还可以直接在corr中重命名列名和行名。 Also be aware that select_if has been superseded .另请注意select_if已被取代

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:如果您只想更改轴上的单个值(例如, famrel ),则可以像这样编辑单个行名和单个列名:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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