R corrplot colorlegend change range

2019-05-12 00:27发布

问题:

I am trying to plot in R a correlation matrix using the corrplot package.
My problem is that the range of min and max correlation coefficients of the entire matrix is (-0.2,0.2). I plot the matrix with corrplot and I use a custom colorRampPalette, say

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).

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.

回答1:

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)


回答2:

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:

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.