I write a function that plots a correlation heatmap but I can not save the plot as an object.
library(corrplot)
library(psych)
myfun = function(ff){
corrplot(ff$r,
p.mat = round(as.matrix(ff$p),3),
method = 'circle',
type = 'lower',
sig.level = c(.001, .01, .05),
tl.pos="lt",
tl.col="black", tl.cex=1.3,
tl.offset=0.2,
cl.pos="r",
insig = "label_sig",
pch.cex = 1.3,
pch.col="red",
cl.cex = 1.3)
corrplot(ff$r, type="upper", method="number",
col="coral4", tl.pos="n", cl.pos="n", number.cex = 1.2, add=T,diag=F)
}
It indeed plots a correlation heatmap.
ff = corr.test(mtcars, method = 'spearman')
myfun(ff)
The attributes of myfun(ff)
is a matrix
rather than a plot
.
p = myfun(ff)
class(p)
> class(p)
[1] "matrix"
Also, I can not see the plot in the Plots window when I type p
in the Console. It just gave me a correlation matrix.
> p
mpg cyl disp hp drat wt qsec
mpg 1.0000000 -0.9108013 -0.9088824 -0.8946646 0.65145546 -0.8864220 0.46693575
cyl -0.9108013 1.0000000 0.9276516 0.9017909 -0.67888119 0.8577282 -0.57235095
disp -0.9088824 0.9276516 1.0000000 0.8510426 -0.68359210 0.8977064 -0.45978176
hp -0.8946646 0.9017909 0.8510426 1.0000000 -0.52012499 0.7746767 -0.66660602
drat 0.6514555 -0.6788812 -0.6835921 -0.5201250 1.00000000 -0.7503904 0.09186863
wt -0.8864220 0.8577282 0.8977064 0.7746767 -0.75039041 1.0000000 -0.22540120
How can I save the plot created by myfun()
as an object (such as p
) so that I can use the object do some other things?
Any help will be highly appreciated.