rggplot2ggcorrplot

Viridis color scale for ggcorrplot


I am making a correlation matrix using the package ggcorrplot. Within the package you can set a vector of three colors for the color scale bar using the colors = c(...) function however, I was wondering if there is a way to set the scale bar to any of the Viridis color palettes?

library(ggplot2)
library(ggcorrplot)

corr <- round(cor(mtcars),1)

ggcorrplot(corr)

ggcorrplot(corr,
           colors = c('purple','seagreen3','yellow'))

Created on 2024-05-15 with reprex v2.1.0


Solution

  • The question was answered based on @Gregor Thomas comment

    library(ggplot2)
    library(ggcorrplot)
    library(viridis)
    #> Loading required package: viridisLite
    
    corr <- round(cor(mtcars),1)
    
    ggcorrplot(corr) +
      scale_fill_gradientn(colors = viridis(256, option = 'D'))
    #> Scale for fill is already present.
    #> Adding another scale for fill, which will replace the existing scale.
    

    ggcorrplot(corr) +
      scale_fill_gradientn(colors = viridis(256, option = 'H'))
    #> Scale for fill is already present.
    #> Adding another scale for fill, which will replace the existing scale.
    

    Created on 2024-05-15 with reprex v2.1.0