【问题标题】:Highlight plot area in time series突出显示时间序列中的绘图区域
【发布时间】:2018-09-18 12:49:34
【问题描述】:

我已经绘制了我的 xts 时间序列如下:

library(xts)
data(sample_matrix)
prices <- as.xts(sample_matrix)[,"Close"]
pw_returns <- diff(log(prices))
plot(pw_returns, main="", col="darkblue", lwd=1)

我想强调波动性集群,如下图所示:

有人知道如何在 R 中做到这一点吗?

【问题讨论】:

  • @RobertoMoratore;不确定这里的这张图是不是ggplot2
  • 另外,@toyo10,请提供一个最小的可重现示例。我们没有pw_returns 对象。
  • @Axeman,问题是关于在 R 中做的,而不是在基本情节中。我认为上面的链接仍然适用。

标签: r plot xts


【解决方案1】:

您可以使用addPolygon() 执行此操作。它将有助于创建一个包含阴影区域的上限和下限的中间对象。

nr <- nrow(pw_returns)
shade <- cbind(upper = rep(1, nr), lower = rep(-1, nr))
shade <- xts(shade, index(pw_returns))

现在我们可以为我们想要的任何时期绘制和添加阴影区域。确保设置on = -1 以在主图后面绘制阴影区域。

# main plot
plot(pw_returns, main = "", col = "darkblue", lwd = 1)
# add shaded region to February, 2007
addPolygon(shade["2007-02"], col = "lightpink", on = -1)

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多