繁体   English   中英

corrplot中的Spearman相关plot

[英]Spearman correlation plot in corrplot

[.[在此处输入图像描述][1]][1]我有一个数据集,其中癌症和潜在风险因素 (egagefirstchild) 的值为 Yes No。 下面是一个数据集的例子

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)

我想运行看起来像这样的 Spearman 相关 plot [![在此处输入图像描述][2]][2]

这个 plot 来自 corrplot package。 但是当我将此代码应用于我的数据集时,它给了我一个错误。 矩阵中的错误(if (is.null(value))logical() else value, nrow = nr, dimnames = list(rn, : 'dimnames' [3] 的长度不等于数组范围图像可在代码下的链接中获得,并在描述下方:

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

[![以下示例中的相关 plot][3]][3]

我可以在代码的哪个位置添加方法,我需要 Spearman? 它不一定需要与以下完全相同,但格式相似,并且具有 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

你必须:

1)先让你的变量数值因子
2)创建斯皮尔曼相关矩阵,然后
3)根据创建的矩阵创建plot

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

在此处输入图像描述

由于您的变量都是分类的,因此马赛克图可能是更好的图形。

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

在此处输入图像描述 没有什么看起来很重要。

或使用vcd package(用于改进标签):

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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