简体   繁体   English

如何将相关图表保存到 object?

[英]How do I save correlation chart to an object?

I'd like to make a correlation plot and save it to an object in a list.我想做一个关联 plot 并将其保存到列表中的 object。

I'm using a function and it appears to run the plot any time the function runs, so it's hard to save it to an object.我正在使用 function,它似乎在 function 运行时运行 plot,因此很难将其保存到 object。

I could use record output, but I don't want it to show up at all.我可以使用记录 output,但我根本不希望它出现。 This is under the hood in a function, so I want to assign it to an object without having it run.这是在 function 的引擎盖下,所以我想将它分配给 object 而无需运行它。

This is my code这是我的代码

library(fpp)
library(PerformanceAnalytics)
l <- list()  

l[["element"]] <-
  chart.Correlation(credit, 
                histogram = TRUE, 
                pch = 19)

l$element

I want to load the correlation plot when I run l$element, but not when I assign it to l[["element"]]我想在运行 l$element 时加载相关性 plot,但在将其分配给 l[["element"]] 时不加载

This is a little complex, because there is no "plot object".这有点复杂,因为没有“情节对象”。 The call to chart.Correlation simply calls a bunch of drawing routines.chart.Correlation的调用只是调用了一堆绘图例程。 It's not like a ggplot where the information to recreate the plot is stored as an object. In fact, the chart.Correlation function silently returns NULL .它不像ggplot ,其中重新创建 plot 的信息存储为 object。事实上,图表。 chart.Correlation静默返回NULL

If you want l$element to produce the plot, then you need to store the call to produce the plot inside l$element , and ensure that this call is evaluated when you type l$element into the console.如果您希望l$element生成 plot,则需要将生成 plot 的调用存储在l$element中,并确保在您将l$element键入控制台时评估此调用。 Probably the easiest way to do this is to create a little wrapper around chart.Correlation with your own S3 class, whose print method eval s the call:执行此操作的最简单方法可能是围绕chart.Correlation创建一个小包装器与您自己的 S3 class,调用其打印方法eval

library(fpp)
library(PerformanceAnalytics)
l <- list()  

make_corrChart <- function(...) {
  mc <- match.call()
  mc[[1]] <- quote(chart.Correlation)
  structure(list(call = mc), class = "CorrChart")
}

print.CorrChart <- function(x, ...) eval(x$call)

l[["element"]] <-  make_corrChart(credit, histogram = TRUE, pch = 19)

Note that no plot is created at this point, but all the information needed to create the plot is stored in l$element .请注意,此时没有创建 plot,但创建 plot 所需的所有信息都存储在l$element中。 So when I type:所以当我输入:

l$element

In the console, I get this:在控制台中,我得到这个:

在此处输入图像描述

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

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