sflee
sflee

Reputation: 1719

How to xyplot multiple lines in R

I would like to plot graphs with multiple lines in R like this: enter image description here

I have data in 3 vectors

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

I search and found that xyplot may be useful, but I don't know how to use it. I have tried.

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

Here is the result: enter image description here Learn from here

Thank you very much.

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

Upvotes: 1

Views: 3012

Answers (2)

David O
David O

Reputation: 803

Both lattice and ggplot offer solutions. Regardless, as @davide suggests, "melting" your data or converting it from a "wide" format to a "long" is a very good practice. Values of interest are placed in a single variable and a parallel factor is created to identify the group associated with each value.

This can be done in base R by several methods. The use of stack() is shown here. In addition, by converting the factor or character representation of the date into a Date object, the plotting routines in lattice and ggplot2 will do a better job managing axes labels for you.

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

Here's a somewhat simple plot with few additions for grid lines and legend (key):

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

The plots can be customized with lattice through panel functions and elements in auto.key. Although using col = c("darkred", "darkblue") at the top level of the function would color the lines in the plot, passing it through the optional par.settings argument makes it available for the legend function.

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

Upvotes: 0

s__
s__

Reputation: 9485

I think that this could help:

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

If you want to reduce the number of ticks on the x axis, you'd create your labels, and add them in this way (in this case, one year, you'd calculate your full time series parameters):

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

In the plot:

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

Upvotes: 3

Related Questions