How to change color scheme in corrplot
Asked Answered
H

2

8

I am using corrplot in R to visualise a correlation-coefficient matrix as follows.

library(corrplot) 
library(datasets)
corrplot(abs(cor(mtcars)), method="color", tl.pos="n", cl.lim = c(0,1))

enter image description here

The default colour scheme is blue-based. However, I would like to change it to red-based. I know I need to use colorRampPalette to specify colours I want. However, I could not figure out what colour codes to use. Could anyone help me with this, please?

Thank you!

Hogwash answered 9/6, 2015 at 22:33 Comment(4)
Have you tried a Color Picker?Patman
When asking for plotting help, it's best to include a reproducible example with sample input data. Also, since corrplot does not appear to be a function in base R, clearly indicate which packages you are using,Pehlevi
I think that at the middle (section: Using Different Color Spectrum) of this page there is what you are looking for: cran.r-project.org/web/packages/corrplot/vignettes/…Puerile
@Pehlevi I made some changes. I think you can reproduce this yourself.Hogwash
P
19

If you want to use red, you can define your own colorRampPalette as you've alread mentioned. Just note that the plot seems to set the range of colors from -1 to 1 (even if you adjust the cl.lim value). Thus you still need to define colors for the -1 to 0 range in your ramp. For example

corrplot(abs(cor(mtcars)), method="color", tl.pos="n", 
    cl.lim=c(0,1), col=colorRampPalette(c("blue","white","red"))(200))

will produce

enter image description here

and even though we defined "blue" in the color palette, it doesn't show up because we limited the color bar to values greater than 1.

This "unused" part of the color gradient cab be seen with the original version as well if you take out cl.lim

corrplot(abs(cor(mtcars)), method="color", tl.pos="n")

enter image description here

Pehlevi answered 9/6, 2015 at 22:58 Comment(0)
E
6

The function colorRampPalette returns a function that takes a numeric argument:

corrplot(abs(cor(mtcars)), method="color",
         col= colorRampPalette(c("white","pink", "red"))(10) ,
         tl.pos="n", cl.lim = c(0,1))

enter image description here

The default is defined with this color spectrum:

col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7",
        "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))  

This would give you a wider range of reds:

colnew <- colorRampPalette(c("#670000", "#B20000", "#D60000", "#F40000", "#FD0000", "#FFFFFF"))

And I thought adding a "brown" at the end extended the range for better visual separation:

col= colorRampPalette(c("white","lightpink", "red","brown"))(10)
Evergreen answered 9/6, 2015 at 22:59 Comment(3)
Thanks. I guess another way is to simply reverse the order of colour codes in default col2.Hogwash
By the way, is it possible to remove the white grid line, please? I tried 'addgrid.col=NULL'. It did not work.Hogwash
addgrid.col="transparent"Evergreen

© 2022 - 2024 — McMap. All rights reserved.