简体   繁体   English

如何在corrplot中更改颜色方案

[英]How to change color scheme in corrplot

I am using corrplot in R to visualise a correlation-coefficient matrix as follows. 我在R中使用corrplot来显示相关系数矩阵,如下所示。

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

在此输入图像描述

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. 我知道我需要使用colorRampPalette来指定我想要的颜色。 However, I could not figure out what colour codes to use. 但是,我无法弄清楚要使用的颜色代码。 Could anyone help me with this, please? 有人可以帮帮我吗?

Thank you! 谢谢!

If you want to use red, you can define your own colorRampPalette as you've alread mentioned. 如果你想使用红色,你可以像你提到的那样定义你自己的colorRampPalette。 Just note that the plot seems to set the range of colors from -1 to 1 (even if you adjust the cl.lim value). 请注意,该图似乎将颜色范围设置为-1到1(即使您调整cl.lim值)。 Thus you still need to define colors for the -1 to 0 range in your ramp. 因此,您仍需要在渐变中定义-1到0范围的颜色。 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 会产生

在此输入图像描述

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. 即使我们在调色板中定义了“蓝色”,它也不会显示,因为我们将颜色条限制为大于1的值。

This "unused" part of the color gradient cab be seen with the original version as well if you take out cl.lim 如果您取出cl.lim ,也可以看到原始版本的颜色渐变驾驶室的“未使用”部分

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

在此输入图像描述

The function colorRampPalette returns a function that takes a numeric argument: 函数colorRampPalette返回一个带有数字参数的函数:

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

在此输入图像描述

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM