4

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.

stragu
  • 1,051
  • 9
  • 15
zhiwei li
  • 1,635
  • 8
  • 26
  • 1
    Read the documentation of `?corrplot`. Under value it says that `corrplot()` `(Invisibly) returns a reordered correlation matrix.` To get what you want, you need to grab the grob that it is producing. https://stackoverflow.com/questions/53734543/converting-corrplot-output-to-grob – Ben Norris Nov 27 '20 at 02:34

1 Answers1

3

The documentation for corrplot() says the following:

Value

(Invisibly) returns a reordered correlation matrix.

So what you want to get out of your function is the side effect of corrplot() (the plot) rather than the matrix.

One way to do that would be to use recordPlot() to "save" the latest plot:

library(corrplot)
#> corrplot 0.84 loaded
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)
  recordPlot() # record the latest plot
}
ff <- corr.test(mtcars, method = 'spearman')

myfun(ff) # shows the plot
p <- myfun(ff) # still shows the plot but also saves it

p # show the recorded plot
class(p)
#> [1] "recordedplot"

Created on 2020-11-27 by the reprex package (v0.3.0)

stragu
  • 1,051
  • 9
  • 15