-2

I create a correlation plot with this code:

require(ggpubr)
require(tidyverse)
require(Hmisc)

M = mydata % > %
    select(var1, var2, var3, var4)
    corrplot(cor(select_if(M, is.numeric)), method = "ellipse", type = "upper", col = c("black", "white"),
    bg = "lightblue", tl.col = "black")

I would like to add a significance test as described on https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html:

res1 < -cor.mtest(mydata, conf.level = .95)

M = mydata % > %
    select(var1, var2, var3, var4)
    corrplot(cor(select_if(M, is.numeric)), method = "ellipse", type = "upper", col = c("black", "white"),
    bg = "lightblue", tl.col = "black", p.mat = res1$p, sig.level = .05)

But this doesn't work, I get this error message:

ERROR in ind[, 2] : incorrect number of dimensions

What's happening here?

Madamadam
  • 842
  • 2
  • 12
  • 24

1 Answers1

1

Without a reproducible example, it is difficult to be sure what is your problem here.

However, my intuition is that your res1 output have made the correlation test on the entire dataframe mydata whereas you are trying to get the correlation plot of few variables (var1 to var4).

So, that could explain the error message because you are calling res1$p that have probable more dimensions than the portion of mydata you are trying to plot.

Does mydata has only 4 variables ? or more ?

Instead try:

res1 < -cor.mtest(mydata[,c("var1", "var2", "var3", "var4")], conf.level = .95)

M = mydata % > %
    select(var1, var2, var3, var4)
    corrplot(cor(select_if(M, is.numeric)), method = "ellipse", type = "upper", col = c("black", "white"),
    bg = "lightblue", tl.col = "black", p.mat = res1$p, sig.level = .05)

If this is not working, please consider providing a reproducible example of mydata (see this tutorial to know how to provide a reproducible example: How to make a great R reproducible example)

dc37
  • 15,840
  • 4
  • 15
  • 32