[英]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). [.[在此处输入图像描述][1]][1]我有一个数据集,其中癌症和潜在风险因素 (egagefirstchild) 的值为 Yes No。 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]我想运行看起来像这样的 Spearman 相关 plot [![在此处输入图像描述][2]][2]
This plot comes from the corrplot package.这个 plot 来自 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:矩阵中的错误(if (is.null(value))logical() else value, nrow = nr, dimnames = list(rn, : 'dimnames' [3] 的长度不等于数组范围图像可在代码下的链接中获得,并在描述下方:
[![Corrplot][2]][2] [![Corrplot][2]][2]
[![Correlation plot from example below][3]][3] [![以下示例中的相关 plot][3]][3]
And where in the code can I add the method, I need Spearman?我可以在代码的哪个位置添加方法,我需要 Spearman? It doesn't necessarily need to be exactly the same as below but similar format and with values in the plot它不一定需要与以下完全相同,但格式相似,并且具有 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 1)先让你的变量数值因子
2)create the spearman correlation matrix and then 2)创建斯皮尔曼相关矩阵,然后
3)create the plot according to the created matrix 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")
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):或使用vcd package(用于改进标签):
library(vcd)
mosaic(~cancer+agefirstchild+agegroup, data=dat, shade=TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.