[英]How to save a plot created by corrplot function in r
I write a function that plots a correlation heatmap but I can not save the plot as an object.我写了一个 function 来绘制相关热图,但我无法将 plot 保存为 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
. myfun(ff)
的属性是matrix
而不是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.此外,当我在控制台中键入
p
时,我在绘图 window 中看不到 plot。 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?如何将
myfun()
创建的 plot 保存为 object (例如p
),以便我可以使用 object 做其他事情?
Any help will be highly appreciated.任何帮助将不胜感激。
The documentation for corrplot()
says the following: corrplot()
的文档说明如下:
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.所以你想从你的 function 中得到的是
corrplot()
(情节)而不是矩阵的副作用。
One way to do that would be to use recordPlot()
to "save" the latest plot:一种方法是使用
recordPlot()
来“保存”最新的 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)由代表 package (v0.3.0) 于 2020 年 11 月 27 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.