簡體   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