I can draw rectangles in a normal correlation matrix using corrplot, e.g.:
library(corrplot)
corrplot(cor(mtcars)) %>%
corrRect(name = c("mpg", "qsec", "carb"))
But when using this mixed corrplot:
corrplot.mixed(cor(mtcars)) %>%
corrRect(name = c("mpg", "qsec", "carb"))
I get this error message:
Error in if (type == "upper") { : argument is of length zero
How to make it work in a mixed corrplot?
The source for
corrRectcontains the lineif (type == "upper").typeis defined in the function astype = corrRes$arg$type, wherecorrResis the correlation plot you pass in the first argument.The first plot you created has the
typeproperty (runcorrplot(cor(mtcars))$arg$typeand the output will be"full").However, plots created with
corrplot.mixed()do not have a value for$arg$type. So we need to create one.As your correlation plot is not an
upper(orlower) triangular plot, we can assign the value"full"totype. This will allow you to get past theif()statement which causes an error and does not relate to your plot, and to ultimately create the rectangles in the same place.Note: make sure to set
tl.pos = "lt"to show the text labels. You can changediag = "l"if you want to show the correlation of 1.00 of every column with itself, rather than having a blank square.Output