简体   繁体   English

R corrplot colorlegend更改范围

[英]R corrplot colorlegend change range

I am trying to plot in R a correlation matrix using the corrplot package. 我试图使用corrplot包在R中绘制相关矩阵。
My problem is that the range of min and max correlation coefficients of the entire matrix is (-0.2,0.2). 我的问题是整个矩阵的最小和最大相关系数的范围是(-0.2,0.2)。 I plot the matrix with corrplot and I use a custom colorRampPalette , say 我用corrplot绘制矩阵,然后使用自定义colorRampPalette

col1<-colorRampPalette(c('red','yellow','green','blue'))

for the colormap of the legend, so I set col=col1(10) , and I set cl.lim=c(-0.2,0.2) . 对于图例的cl.lim=c(-0.2,0.2)映射,所以我设置了col=col1(10) ,并设置了cl.lim=c(-0.2,0.2)

When I see the plot however the colorlegend appears from -0.2 to 0.2 but with just 2 colors, instead what I would like is a colorlegend with the entire spectrum of colors in 10 bins of the custom palette but in range (-0.2,0.2) so instead of having just 2 colors I will have 10 colors. 当我看到情节时,colorlegend出现在-0.2到0.2但只有2种颜色,而我想要的是一个colorlegend,其中包含10个自定义调色板中的整个色域,但在范围内(-0.2,0.2)因此,我只有2种颜色而不是10种颜色。

The solution for this was duplicate the color range, so, the get the second half... 对此的解决方案是重复颜色范围,因此,获得下半部...

mypal = jet.colors(1000) # jet.colors from library(matlab)

color = c(mypal,mypal)

corrplot(M, col=color)

I encountered a similar problem but had mostly very high correlations. 我遇到了类似的问题,但主要是非常高的相关性。 That made it hard to distinguish between different points without defining a lot of unused colors in my palette. 这使得很难区分不同的点而不在我的调色板中定义大量未使用的颜色。

My solution was to rescale my correlations to the range (-1, 1) (which is the range assumed by corrplot ) prior to plotting: 我的解决方案是在绘图之前将我的相关性重新调整到范围(-1, 1) (这是corrplot假定的范围):

corrplot2 <- function(corr, col) {
    a = 2 / (max(corr) - min(corr))
    b = 1 - (2 / (1 - (min(corr) / max(corr))))
    y = a * corr + b
    corrplot(y, method="circle", bg="grey92", col=col, 
             order="hclust", addrect=4, cl.lim=c(-1, 1))
}

This way the entire distribution of values can again be nicely distinguished using my colors of choice col . 通过这种方式,可以使用我选择col颜色再次很好地区分整个值的分布。

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

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