【问题标题】:Spearman correlation plot in corrplotcorrplot 中的 Spearman 相关图
【发布时间】:2020-10-02 23:38:56
【问题描述】:

[![在此处输入图像描述][1]][1]我有一个数据集,其中癌症和潜在风险因素(例如,agefirstchild)的值为 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 相关图,如下所示[![在此处输入图像描述][2]][2]

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

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

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

我可以在代码的什么地方添加方法,我需要 Spearman?它不一定需要与下面完全相同,但格式和图中的值相似

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

【问题讨论】:

    标签: r


    【解决方案1】:

    你必须:

    1) 先将变量设为数值因子,然后
    2)创建斯皮尔曼相关矩阵,然后
    3)根据创建的矩阵创建绘图

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

    【讨论】:

    • 您好,谢谢! @siimpoldre 您示例中的图表看起来不错。当我运行完全相同时,我得到一个所有指标都被压缩的图表。在我的完整数据集中,有更多的指标。图片见链接:i.stack.imgur.com/DHUEe.png
    • 尝试将 corrplot() 函数中的“tl.cex”参数改小一些;或调整(放大)显示绘图的窗口的大小;或者可能使变量名称更短。
    • 您好,谢谢,我在哪里可以在 corrplot 函数中添加 tl.cex 参数?如何整合?
    • 你可以将它写在 corrplot() 的括号内,例如: corrplot(corr_mat, method = "color", type = "upper", order = "hclust", addCoef.col = "black ", tl.col = "黑色", tl.cex = 0.7)。默认值为 tl.cex=1,因此任何低于 1 的内容都会使他的文本越来越小。玩弄它,看看有什么用
    • 嘿,谢谢它的工作我现在有一个可见的情节。认为最好是更改数字的字体大小,因为仍有一些重叠
    【解决方案2】:

    由于您的变量都是分类的,也许马赛克图会是更好的图形。

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

    看起来没什么大不了的。

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

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

    【讨论】:

    • 嗨,爱德华,谢谢!情节看起来不错,但希望有一个像下面这样的情节
    • 我建议您阅读 Spearmans 等级相关性。您的数据不是连续的,而且除了年龄组之外,不是有序的。因此,为您的变量呈现相关性是不合适的。
    • 嗨,谢谢,我去读了。我也去阅读这些情节。是否也可以在这些正方形中有值?这样您就可以更好地了解它们所代表的含义?
    • 你想要什么样的价值观?当然可以将文本放在矩形中,但这可能太难了,不值得努力。这些值可以从颜色中看出,这在右侧的栏中进行了解释。增加蓝色阴影表示频率高于您的预期,而红色阴影表示频率低于预期。
    • 啊,现在我看到白框代表一个值 :) 我以为我缺少颜色,但一切都在白框中。这很清楚。所以它实际上显示了哪些子变量患乳腺癌的机会增加,能够像这样可视化它是很有见地的。我和我的统计老师谈过,他确实说过。标称变量不能用于Spearman,序数可以。我也接受了你的答案,但也许你只能接受 1 个答案
    猜你喜欢
    • 1970-01-01
    • 2018-05-09
    • 2013-10-01
    • 1970-01-01
    • 2016-03-16
    • 2018-02-01
    • 1970-01-01
    • 2013-03-02
    • 2018-01-01
    相关资源
    最近更新 更多