简体   繁体   English

将 corrplot 的结果分配给变量

[英]Assigning the result of corrplot to a variable

I am using the function corrplot from corrplot package to generate a plot of a correlation matrix created with cor.test (psych package).我正在使用 corrplot 包中的函数 corrplot 来生成用 cor.test (psych 包)创建的相关矩阵图。 When I try to save the result into a variable, the variable is NULL.当我尝试将结果保存到变量中时,该变量为 NULL。

Anyone could advice, please?任何人都可以建议,好吗?

library(corrplot)
library(psych)
library(ggpubr)

data(iris)

res_pearson.c_setosa<-iris%>%
  filter(Species=="setosa")%>%
  select(Sepal.Length:Petal.Width)%>%
  corr.test(., y = NULL, use = "complete",method="pearson",adjust="bonferroni", alpha=.05,ci=TRUE,minlength=5)

corr.a<-corrplot(res_pearson.c_setosa$r[,1:3],
         type="lower", 
         order="original", 
         p.mat = res_pearson.c_setosa$p[,1:3], 
         sig.level = 0.05, 
         insig = "blank", 
         col=col4(10), 
         tl.pos = "ld",
         tl.cex = .8, 
         tl.srt=45, 
         tl.col = "black",
         cl.cex = .8)+
  my.theme #this is a theme() piece, but if I take this away, the result is a list rather than a plot

You can create your own function where you put recordPlot at the end to save the plot.您可以创建自己的函数,将recordPlot放在末尾以保存绘图。 After that you can save the output of the function in a variable.之后,您可以将函数的输出保存在变量中。 Here is a reproducible example:这是一个可重现的示例:

library(corrplot)
library(psych)
library(ggpubr)
library(dplyr)

data(iris)

res_pearson.c_setosa<-iris%>%
  filter(Species=="setosa")%>%
  select(Sepal.Length:Petal.Width)%>%
  corr.test(., y = NULL, use = "complete",method="pearson",adjust="bonferroni", alpha=.05,ci=TRUE,minlength=5)

your_function <- function(ff){
corr.a<-corrplot(ff$r[,1:3],
                 type="lower", 
                 order="original", 
                 p.mat = ff$p[,1:3], 
                 sig.level = 0.05, 
                 insig = "blank", 
                 #col=col4(10), 
                 tl.pos = "ld",
                 tl.cex = .8, 
                 tl.srt=45, 
                 tl.col = "black",
                 cl.cex = .8)
  #my.theme #this is a theme() piece, but if I take this away, the result is a list rather than a plot
recordPlot() # save the latest plot
}
your_function(res_pearson.c_setosa)

p <- your_function(res_pearson.c_setosa)

p

Created on 2022-07-13 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2022-07-13 创建

As you can see, the variable p outputs the plot.如您所见,变量 p 输出绘图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM