Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

Create colour range with corrplot in R

I have searched google and stack overflow for an answer to my question in R but can't find a suitable one. I have a matrix, which I import into R, I then use corrplot to plot. My question is that I have three correlation groupings:

1: 0-<0.5

2: >0.5-0.7

3: >0.7

I want one colour for each grouping, so red for group 1, blue for group 2, and green for group 3

Is there a way to do that?

here is some dummy data:

library(corrplot)

data(mtcars)
M <- cor(mtcars)
set.seed(0)
corrplot(M,type='upper',method = "square",addCoef.col = "white")

Many thanks in advance!

question from:https://stackoverflow.com/questions/65937190/create-colour-range-with-corrplot-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

maybe try:

data(mtcars)
M <- abs(cor(mtcars)) # absolute values 
M[M<=0.5]=-1          # your conditions
M[M>=0.7]=1
M[M != -1 & M != 1]=0
my.col <- colorRampPalette(c("red", "blue", "green"))  # your colors 
set.seed(0)
corrplot(M,type='upper',method = "color", col = my.col(3), cl.pos = "n")

see https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...