Plot correlation matrix with R in specific data range

1.4k views Asked by At

I have used corrplot package to plot my data-pairs. But all the relationships in my data are positive.

Mydata<-read.csv("./xxxx.csv")
M <-cor(Mydata)
corrplot(M,,col=rev(brewer.pal(n=8, name="RdYlBu")))    

Using ggcorr, I also can't find any solution to deal with the issue.

enter image description here

How to generate a user-defined colormap with the corresponding range from 0 to 1?

1

There are 1 answers

0
dww On

If you are trying to map the entire range of the colormap to only the positive correlations, you could use col = rep(rev(brewer.pal(n=8, name="RdYlBu")), 2). This repeats the color sequence, and then cl.lim = c(0,1) forces corrplot to use only the 2nd half of the sequence, mapped to the range 0 to 1.

par(xpd=T)
corrplot(M,,'upper', 
  col = rep(rev(brewer.pal(n=8, name="RdYlBu")), 2), 
  cl.lim = c(0,1),
  mar = c(1, 0, 1, 0))

enter image description here

Some reproducible data

set.seed(12)
x = (1:100)/100
Mydata = data.frame(a=x^runif(1, 0, 50), 
                    b=x^runif(1, 0, 50),
                    c=x^runif(1, 0, 50), 
                    d=x^runif(1, 0, 50),
                    e=x^runif(1, 0, 50),
                    f=x^runif(1, 0, 50),
                    g=x^runif(1, 0, 50),
                    h=x^runif(1, 0, 50),
                    i=x^runif(1, 0, 50))

M = cor(Mydata)