简体   繁体   中英

Spearman correlation plot in corrplot

[.[enter image description here][1]][1]I have a data set with Yes No values for Cancer and underlying risk factors (egagefirstchild). Below an example of the data set

set.seed(42)
cancer <- sample(c("yes", "no"), 200, replace=TRUE) 
agegroup <- sample(c("35-39", "40-44", "45-49"), 200, replace=TRUE)  
agefirstchild <- sample(c("Age < 30", "Age 30 or greater", "nullipareous"), 200, replace=TRUE) 
dat <- data.frame(cancer, agegroup, agefirstchild)

I would like to run a Spearman correlation plot that looks like this[![enter image description here][2]][2]

This plot comes from the corrplot package. But when I apply this code for my data set it gives me an error. Error in matrix(if (is.null(value)) logical() else value, nrow = nr, dimnames = list(rn, : length of 'dimnames' [3] not equal to array extent Images are available at links under code, and below the description:

[![Corrplot][2]][2]

[![Correlation plot from example below][3]][3]

And where in the code can I add the method, I need Spearman? It doesn't necessarily need to be exactly the same as below but similar format and with values in the plot

corrplot(dat, method = "color", col = col(200),
       type = "upper", order = "hclust", number.cex = .7,
       addCoef.col = "black", 
       tl.col = "black", tl.srt = 90, 
       p.mat = p.mat, sig.level = 0.01, insig = "blank", 
       diag = FALSE) ```


[1]: https://i.stack.imgur.com/xkKLY.png
[2]: https://i.stack.imgur.com/jrghy.png
[3]: https://i.stack.imgur.com/DHUEe.png

You have to:

1)make your variables numeric factors first and then
2)create the spearman correlation matrix and then
3)create the plot according to the created matrix

    set.seed(42)
cancer <- sample(c("yes", "no"), 200, replace=TRUE) 
agegroup <- sample(c("35-39", "40-44", "45-49"), 200, replace=TRUE)  
agefirstchild <- sample(c("Age < 30", "Age 30 or greater", "nullipareous"), 200, replace=TRUE) 
dat <- data.frame(cancer, agegroup, agefirstchild) 

#make numeric factors out of the variables
dat$agefirstchild <- as.numeric(as.factor(dat$agefirstchild))
dat$cancer <- as.numeric(as.factor(dat$cancer)) 
dat$agegroup <- as.numeric(as.factor(dat$agegroup))

corr_mat=cor(dat,method="s") #create Spearman correlation matrix

library("corrplot")
corrplot(corr_mat, method = "color",
     type = "upper", order = "hclust", 
     addCoef.col = "black",
     tl.col = "black")  

在此处输入图像描述

Since your variables are all categorical, perhaps a mosaicplot would be a better graphic.

mosaicplot(~cancer+agegroup+agefirstchild, data=dat, shade=TRUE)

在此处输入图像描述 Nothing looks significant.

Or using the vcd package (for improved labelling):

library(vcd)
mosaic(~cancer+agefirstchild+agegroup, data=dat, shade=TRUE)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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