Search code examples
rr-corrplot

Force corrplot to show trailing zeros in correlation matrix plot


I am using the mtcars dataset in R to plot a correlation matrix using the corrplot package. My issue is that I would like the 0.9 value between wt and disp to show as 0.90. I have noticed this is possible when I change the method argument to "number" instead of "shade" but I want my plot to use the "shade" argument.

Is there a way I can transform my data so that corrplot shows the trailing zero?

My code is below:

library(tidyverse)
library(corrplot)

data("mtcars")

corr_df = mtcars %>%
  select(mpg, disp, hp, drat, wt, qsec)

mat = cor(corr_df, method = "spearman")

corrplot(mat, method = "shade",  tl.col = "black", 
                         addCoef.col = "black", type = "lower",  
                         order = "original" , diag = F,
                         addgrid.col = "black", tl.srt = 45, number.digits = 2)

here

I've tried using the number.digits argument but when it's set to 2 digits the output is still 0.9 and when it's set to 3 digits the output becomes 0.899.


Solution

  • This is not currently implemented in the package and the package has not received any updates in some time.

    An issue has been raised, and there is a pull request addressing the issue.

    Until the pull request is accepted, the best you could do would be to install the patched version with:

    devtools::install_github("the-mad-statter/corrplot@add_format")
    

    Here is the output with the patched version:

    library(tidyverse)
    library(corrplot)
    
    data("mtcars")
    
    corr_df = mtcars %>%
      select(mpg, disp, hp, drat, wt, qsec)
    
    mat = cor(corr_df, method = "spearman")
    
    corrplot(mat, method = "shade",  tl.col = "black", 
                             addCoef.col = "black", type = "lower",  
                             order = "original" , diag = F,
                             addgrid.col = "black", tl.srt = 45, number.digits = 2)
    

    Created on 2024-04-30 with reprex v2.1.0