【问题标题】:How to xyplot multiple lines in R如何在R中对多行进行xyplot
【发布时间】:2019-05-27 08:34:29
【问题描述】:

我想在 R 中绘制具有多条线的图形,如下所示:

  • 2 行
  • x 轴是日期
  • y轴为对数返回

我有 3 个向量中的数据

print(class(TradeDate))
print(class(ArimaGarchCurve))
print(class(CompareCurve))
---------------------------------------------
[1] "factor"
[1] "numeric"
[1] "numeric"

我搜索了一下,发现xyplot可能有用,但是不知道怎么用。我试过了。

pdf("Testing.pdf")
plotData <- data.frame(Date=TradeDate,
                       Arima=ArimaGarchCurve,
                       BuyHold=BuyHoldCurve)
print(xyplot(
    Arima ~ Date,
    data=plotData,
    superpose=T,
    col=c("darkred", "darkblue"),
    lwd=2,
    key=list(
        text=list(
            c("ARIMA+GARCH", "Buy & Hold")
        ),
        lines=list(
            lwd=2, col=c("darkred", "darkblue")
        )
    )
))
dev.off()

结果如下: Learn from here

非常感谢。

dput(head(plotData,20))
structure(list(Date = structure(1:20, .Label = c("2001-12-03", 
"2001-12-04", "2001-12-05", "2001-12-06", "2001-12-07", "2001-12-10", 
"2001-12-11", "2001-12-12", "2001-12-13", "2001-12-14", "2001-12-17", 
"2001-12-18", "2001-12-19", "2001-12-20", "2001-12-21", "2001-12-24", 
"2001-12-25", "2001-12-26", "2001-12-27", "2001-12-28", "2001-12-31", 
"2002-01-01", "2002-01-02", "2002-01-03", "2002-01-04", "2002-01-07",
"2019-05-22", "2019-05-23"), class = "factor"), Arima = c(-0.0134052258713131, 
-0.00542641764174324, 0.0128513670753771, 0.0282761455973665, 
0.0179931884968989, 0.0281714817318116, 0.0435962602538011, 0.0462004298658309, 
0.0194592964361352, 0.0248069155406948, 0.032807001046888, 0.0381120657516546, 
0.0381120657516546, 0.030090589527961, -0.0146168717909267, -0.00630652663076437, 
-0.00630652663076437, -0.00630652663076437, 0.0100429785563596, 
0.0100429785563596), BuyHold = c(-0.0134052258713131, -0.00542641764174324, 
0.0128513670753771, 0.0282761455973665, 0.0384544388322794, 0.0281714817318116, 
0.0125050470584384, 0.0151092166704679, -0.0116319167592278, 
-0.0170082867113405, -0.0090082012051471, -0.00370313650038065, 
-0.00370313650038065, -0.0117246127240743, -0.056432074042962, 
-0.0481217288827996, -0.0481217288827996, -0.0481217288827996, 
-0.0317722236956757, -0.0317722236956757)), row.names = c(NA, 
20L), class = "data.frame")

【问题讨论】:

  • 您能否dput(plotData) 并发布结果以共享您的数据或创建一些虚假数据以使您的代码可重现?
  • drive.google.com/open?id=1JBnd5zhwWYC6R1cEA8MfQOedXPiUu3su,这是dput的输出,非常感谢
  • 不幸的是,它不是与外部链接共享数据的最佳选择,我的建议是使用 dput(plotData)dput(head(plotData,20)) 分别拥有所有输出或前 20 行,然后发布它们编辑您的问题。请不要认为这是一个迂腐的要求,但数据的外部链接不是正确的方式。
  • 您在融化数据后尝试过ggplot2 吗?类似下面的东西应该可以工作。 ggplot(data.table::melt(plotData, 'Date')) + geom_line(aes(x = Date, y = value, col = variable)
  • 你从 davide 和 @s_t 那里得到了很好的建议。作为 lattice 的粉丝,我正在添加一个带有一些您可能会觉得有用的扩展的答案。

标签: r lattice


【解决方案1】:

我认为这会有所帮助:

library(lattice)
xyplot(
  Arima + BuyHold ~ Date,                                   # here you can add log() to the two ts
  data=plotData,
  superpose=T,
  col=c("#cc0000", "#0073e6"),                              # similar colors
  lwd=2,
  key=list(
     text  = list(c("ARIMA+GARCH log", "Buy & Hold log")),
     lines = list( lwd=2, col=c("#cc0000", "#0073e6"))      # similar colors
  ), type=c("l","g")                                        # lines and grid
)

如果您想减少 x 轴上的刻度数,您可以创建标签,并以这种方式添加它们(在这种情况下,一年,您将计算完整的时间序列参数):

x.tick.number <- 1
at <- seq(1, nrow(d), length.out=x.tick.number)
labels <- round(seq(2001, 2001, length.out=x.tick.number))

剧情中:

xyplot(
  Arima + BuyHold ~ Date,                                   # here you can add log() to the two ts
  data=d,
  superpose=T,
  col=c("#cc0000", "#0073e6"),                              
  lwd=2,
  key=list(
    text  = list(c("ARIMA+GARCH log", "Buy & Hold log")),
    lines = list( lwd=2, col=c("#cc0000", "#0073e6"))      
  ), type=c("l","g"),
  scales = list(at=at, labels=labels, rot=90))

【讨论】:

    【解决方案2】:

    lattice 和 ggplot 都提供解决方案。无论如何,正如@davide 所建议的那样,“融化”您的数据或将其从“宽”格式转换为“长”格式是一种非常好的做法。感兴趣的值被放置在单个变量中,并创建一个平行因子来识别与每个值关联的组。

    这可以通过几种方法在基础 R 中完成。此处显示了stack() 的使用。此外,通过将日期的因子或字符表示转换为Date 对象,latticeggplot2 中的绘图例程将更好地为您管理轴标签。

    df <- data.frame(Date = as.Date(plotData$Date), stack(plotData[2:3]))
    (names(df)) # stack names the data 'values and the grouping factor 'ind'
    levels(df$ind) <- c("ARIMA+GARCH", "Buy & Hold") # simplifies legends
    

    这是一个有点简单的图,添加了一些网格线和图例(键):

    xyplot(values ~ Date, data = df, groups = ind, type = c("g", "l"), auto.key = TRUE)
    

    可以使用latticepanel 函数和auto.key 中的元素自定义绘图。尽管在函数的顶层使用 col = c("darkred", "darkblue") 会为绘图中的线条着色,但将其传递给可选的 par.settings 参数使其可用于图例函数。

    xyplot(values ~ Date, data = df, groups = ind,
      panel = function(...) {
        panel.grid(h = -1, v = -1)
        panel.refline(h = 0, lwd = 3)
        panel.xyplot(..., type = "l")},
      auto.key = list(points = FALSE, lines = TRUE, columns = 2),
      par.settings = list(superpose.line = list(col = c("darkred", "darkblue"))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 2023-03-21
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多