Home > Blockchain >  How to increase color contrast for correlation plot in R
How to increase color contrast for correlation plot in R

Time:10-25

I have the following data:

dat <- structure(list(`2022.09.16` = c(1, 5, 1, 8, NA, 1, NA, 7, 4, 
NA), `2022.10.11` = c(2, 6, 4, 8, 1, 2, 2, 8, 7, 11), `2022.10.18` = c(4, 
8, 5, 8, 2, NA, 2, 7, 9, 9)), row.names = c("p1", "p2", "p3", 
"p4", "p5", "p6", "p7", "p8", "p9", 
"p10"), class = "data.frame")

That looks like this:

    2022.09.16 2022.10.11 2022.10.18
p1           1          2          4
p2           5          6          8
p3           1          4          5
p4           8          8          8
p5          NA          1          2
p6           1          2         NA
p7          NA          2          2
p8           7          8          7
p9           4          7          9
p10         NA         11          9

What I want to do is to make correlation plot based on column. I tried this:

GGally::ggcorr(dat,  label = TRUE )

I get this plot:

enter image description here

As you can see the contrast between low and high value is not very clear. What I'd like to get is 0.7 to be bluish. How can I achieve that?

CodePudding user response:

Use scale_fill_gradientn.

library(GGally)
ggcorr(dat,  label = TRUE )  
  scale_fill_gradientn(colors = c("#1b9e77","#d95f02","#7570b3"),
                       values = scales::rescale(c(-1, 0.7, 1)),
                       limits = c(-1, 1))

resulting plot

Adjust colors as desired.

  • Related