rggplot2ggcorrplot

Adding theme parameters in ggplot


I have below ggplot

library(ggplot)
  library(ggcorrplot)
  data(mtcars)
corr <- round(cor(mtcars), 1)
head(corr[, 1:6])
ggcorrplot(corr, hc.order = TRUE, type = "upper", outline.col = "white") +
theme(legend.key.height=unit(2, "cm"), legend.position = "left") +
guides(color=guide_legend(title="New Legend Title"))

I above case, I was trying to change the legend name to New Legend Title, but unfortunately that did not happen. I also want to add specific background colour to the entire plot and also change the colour scale for correlation from 'red' to 'blue'

Is there any way to achieve them?


Solution

  • The issue is that the legend is a guide for the fill aesthetic. Hence, use fill instead of color. Additionally the guide is a colorbar so you have to use guide_colorbar:

    library(ggplot2)
    library(ggcorrplot)
    
    corr <- round(cor(mtcars), 1)
    
    ggcorrplot(corr, hc.order = TRUE, type = "upper", outline.col = "white") +
      theme(legend.key.height = unit(2, "cm"), legend.position = "left") +
      guides(fill = guide_colorbar(title = "New Legend Title"))