簡體   English   中英

將 corrplot 的結果分配給變量

[英]Assigning the result of corrplot to a variable

我正在使用 corrplot 包中的函數 corrplot 來生成用 cor.test (psych 包)創建的相關矩陣圖。 當我嘗試將結果保存到變量中時,該變量為 NULL。

任何人都可以建議,好嗎?

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

您可以創建自己的函數,將recordPlot放在末尾以保存繪圖。 之后,您可以將函數的輸出保存在變量中。 這是一個可重現的示例:

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

reprex 包(v2.0.1) 於 2022-07-13 創建

如您所見,變量 p 輸出繪圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM