Reputation: 37
[![enter image description here][1]][1]I have a data set with Yes No values for Cancer and underlying risk factors (e.g.agefirstchild). 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.sstatic.net/xkKLY.png
[2]: https://i.sstatic.net/jrghy.png
[3]: https://i.sstatic.net/DHUEe.png
Upvotes: 1
Views: 6007
Reputation: 54
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")
Upvotes: 2
Reputation: 18663
Since your variables are all categorical, perhaps a mosaicplot would be a better graphic.
mosaicplot(~cancer+agegroup+agefirstchild, data=dat, shade=TRUE)
Or using the vcd package (for improved labelling):
library(vcd)
mosaic(~cancer+agefirstchild+agegroup, data=dat, shade=TRUE)
Upvotes: 0