R corrplot colorlegend change range
Asked Answered
T

3

5

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.

Tripping answered 23/1, 2015 at 12:11 Comment(1)
did you find the solution?Margoriemargot
M
3

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)
Margoriemargot answered 10/8, 2016 at 14:46 Comment(0)
L
0

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.

Lettie answered 2/3, 2018 at 14:55 Comment(0)
A
0

I had the same issue. It can be solved by changing the parameter is.corr to FALSE.

col <- colorRampPalette(brewer.pal(10, "Spectral"))(100)
corrplot(cor_matrix,
         method="color",
         col=col,
         is.corr = FALSE, #this is the parameter you were looking for
         col.lim = c(0,1)) #sets limits to the color legend
Artemas answered 17/5, 2023 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.