在一张图表上绘制 xts object 的所有列(数字未预定义)

Plot all columns (number is not predefined) of xts object on one chart

我如何编写 R 代码来获取单个 xts object 中的所有列并将它们绘制在单个图表上?

我的 xts object 看起来像下面的屏幕截图,我在编写代码时不知道那里的列数是多少:screenshot of xts object

我希望 R 获取每一列并将它们绘制为单个图表上的其中一条线。理想情况下,它应该是一些花哨的 quantmod 图表(因为每一列都是特定投资工具的价格),但即使是简单的图表也可以。

补充评论。

这是我使用的数据文件 (csv):One Drive sharepoint link to zip file

这是我目前的代码:

etfs_history_input <- read.zoo(file = "etfs_history.csv", sep = ",", index.column = 1, header = TRUE, FUN=as.POSIXct, tz = "Europe/Moscow", format = "%Y-%m-%d")
etfs_h <- as.xts(coredata(etfs_history_input), order.by = index(etfs_history_input))

为了达到我的目标输出,我最接近的事情是这样写的:

apply(etfs_h, 2, plot)

但结果:

你可以尝试像熔化然后用 ggplot 绘图

library(data.table)
library(ggplot2)
d <- melt(object, measures.var = patterns("^FX"))
ggplot(d, aes(date,value, col=variable)) + 
  geom_point() 

但没有例子来测试那只是盲目的尝试

如果您想使用 xts 绘图,请阅读 ?plot.xts。 示例:

library(quantmod)
syms <- c("AAPL","MSFT", "NQ", "SSRM")
getSymbols(syms)

x <- lapply(syms, function(x) Cl(get(x)))
m <- do.call(cbind, x)

plot(m, main = "stocks")
addLegend("topleft", on=1, 
          legend.names = colnames(m), lty = 1)