【发布时间】:2021-11-28 13:28:40
【问题描述】:
以下创建一个两列 XTS 对象作为示例数据。
quantmod::getSymbols(c('SPY','IWM'),
src = 'yahoo',
from = '2010-01-01',
to = '2021-02-01',
auto.assign = TRUE,
warnings = FALSE)
close <- cbind(SPY$SPY.Close, IWM$IWM.Close)
下面将两列作为时间的函数进行了很好的绘制,但没有出现图例。 legend 命令看起来像它在执行时没有返回错误,但在绘图上没有创建图例。
qualityTools::plot(close, col=1:ncol(close))
legend('topleft', legend=colnames(close),
col=1:ncol(close), lty=1, cex=0.5)
Plot of XTS object without legend
我还尝试了以下方法。它绘制了两列的图,我得到了一个图例。但是,as.ts() 函数将 XTS 对象的时间索引转换为从 1 到 ncol(close) 的增量计数器,而不是实际日期。因此,图中的时间轴也是一个简单的增量计数器,而不是实际日期。
closets <- stats::as.ts(close)
qualityTools::plot(closets, plot.type='single', col = 1:ncol(closets))
legend('topleft', legend=colnames(closets),
col=1:ncol(closets), lty=1, cex=0.5)
Plot of TS object with legend but a period counter rather than real time axis
关于如何在 XTS 对象的第一次情节尝试中获得图例的任何想法?如果没有,我会在 TS 对象的第二次绘图尝试中选择更好的时间轴。
【问题讨论】: