Changing legend labels for ggcorrplot

Hello,

In the correlation plot below, the upper diagonal (red) represents the correlation between responses to questions 1-5 among males and the lower diagonal (black) represents the correlation between responses to questions 1-5 among females. In order to stratify the plot by gender, I took the absolute value of the correlation data for males and -1 times the absolute value of the correlation data for females. As a result, negative values represent female responses rather than a negative correlation. The figure, as it stands, is nearly perfect. I just want to remove the negative signs from the legend such that the legend reads 1.0, 0.5, 0.0, 0.5, 1.0 from top to bottom. Any ideas on how to achieve this? Here is the figure:

And here is a reproducible example:

lapply(c("ggcorrplot", "psych"),library, character.only=T)

Q01<-data.frame(Q01=rep(NA, 100))
Q01<-sample(6, size=nrow(Q01), replace=TRUE)
Q01<-as.data.frame(Q01)

Q02<-data.frame(Q02=rep(NA, 100))
Q02<-sample(6, size=nrow(Q02), replace=TRUE)
Q02<-as.data.frame(Q02)

Q03<-data.frame(Q03=rep(NA, 100))
Q03<-sample(6, size=nrow(Q03), replace=TRUE)
Q03<-as.data.frame(Q03)

Q04<-data.frame(Q04=rep(NA, 100))
Q04<-sample(6, size=nrow(Q04), replace=TRUE)
Q04<-as.data.frame(Q04)

Q05<-data.frame(Q05=rep(NA, 100))
Q05<-sample(6, size=nrow(Q05), replace=TRUE)
Q05<-as.data.frame(Q05)

data<-do.call("cbind", list(Q01, Q02, Q03, Q04, Q05))

Gender<-data.frame(Gender=rep(NA, 100))
Gender<-sample(2, size=nrow(Gender), replace=TRUE)
Gender<-as.data.frame(Gender)

data<-cbind(data, Gender)

data$Gender<-as.factor(car::recode(data$Gender, "1='Male'; 2='Female'"))

corrplot_data_male<-cor(data[,1:5], use="pairwise", method="pearson")

corrplot_data_female<-cor(data[,1:5], use="pairwise", method="pearson")

corrplot_data_complete<-lowerUpper(lower=-1*abs(corrplot_data_female),
                                              upper=abs(corrplot_data_male))

figure<-ggcorrplot(corrplot_data_complete,
                   colors=c("black", "white", "red"),
                   method="square") +
  theme(panel.grid.major=element_blank())

figure

hello,

it's possible to overwrite the scale with the same spec? something like

colors  <- c("black", "white", "red")
figure + scale_fill_gradient2(low = colors[1], high = colors[3], mid = colors[2], midpoint = 0, 
limit = c(-1, 1), space = "Lab", name = "Corr", label = c(1,.5,0,.5,1))

maybe there is a better way!
cheers

1 Like

Nice! Works like a charm, cheers!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.