如何在R中xyplot多行

时间:2019-05-27 08:34:29

标签: r lattice

我想像这样在R中绘制多行图形: enter image description here

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

我有3个向量的数据

java.sql.Date

我搜索并发现print(class(TradeDate)) print(class(ArimaGarchCurve)) print(class(CompareCurve)) --------------------------------------------- [1] "factor" [1] "numeric" [1] "numeric" 可能有用,但是我不知道如何使用它。我已经尝试过了。

xyplot

结果如下: enter image description here Learn from here

非常感谢您。

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

2 个答案:

答案 0 :(得分: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
)

enter image description here

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

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

enter image description here

答案 1 :(得分:0)

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

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

可以通过lattice中的panel函数和元素使用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"))))

two lines in lattice image

相关问题