如何在 R 中绘制多条线

How to xyplot multiple lines in R

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

我有 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")

我认为这会有所帮助:

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))

lattice 和ggplot 都提供了解决方案。无论如何,正如@davide 建议的那样,"melting" 您的数据或将其从 "wide" 格式转换为 "long" 是一个很好的做法。感兴趣的值放在单个变量中,并创建一个平行因子来标识与每个值关联的组。

这可以通过多种方法在 base 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"))))