3

I have a question concerting R: I was trying to plot a set of daily time series commodity data in R with the package ggplot2 and ggfortify into a matrix. I am trying to have standardized values on each y axis and the dates 1/1/2007, 1/1/2008... on the x axis. The visual concept should look like this:

enter image description here

Does anyone know how that works ?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Lili Matic
  • 59
  • 4
  • 1
    Welcome to SO! You need to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) with adequate sample data to reproduce the situation. – alistaire Apr 14 '16 at 21:59
  • Check out `autoplot.zoo` in the `zoo` package for some examples. (Also the replication code for the JSS paper you mention is available along with the nanuscript.) – Achim Zeileis Apr 14 '16 at 22:13

1 Answers1

4

The zoo package provides support for time series with flexible time indexes, e.g., including Date. The package also brings a fortify() method that can be leveraged for ggplot2 graphics. A convenience method for autoplot() is also provided, see ?autoplot.zoo for a selection of worked examples with different layouts.

For an example with 18 time series with Date index in a 3 x 6 layout, I use a subset of the data set FXRatesCHF from package fxregime. This provides exchange rates for different currencies with respect to the Swiss Franc (CHF).

library("zoo")
data("FXRatesCHF", package = "fxregime")
FX <- window(FXRatesCHF[, c(1:4, 6:19)], start = as.Date("2000-01-01"))

Then ggplot() can be applied to the output of the fortify() method:

library("ggplot2")
ggplot(aes(x = Index, y = Value), data = fortify(FX, melt = TRUE)) +
  geom_line(color = "darkred") +
  xlab("Time") + ylab("FX") +
  theme_bw() +
  facet_wrap(~ Series, scales = "free_y", ncol = 6)

FX ggplot2

The same kind of layout can also be easily created with base graphics. Only the panel titles are on the y-axis rather than in a gray-shaded main title:

plot(FX, col = "darkred", xlab = "Time", nc = 6,
  panel = function(...) { grid(col = "lightgray"); lines(...) })

FX base graphics

Finally, a lattice version can be created by

library("lattice")
trellis.par.set(theme = standard.theme(color = FALSE))
xyplot(FX, col = "darkred", xlab = "Time", layout = c(6, 3),
  panel = function(...) { panel.grid(col = "lightgray"); panel.lines(...) })

FX lattice

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • I have tried the following solution an dit worked very well: '# Standardize the data data_std <- data for (i in 2:26) {data_std[,i] <- (data_std[,i] - mean(data_std[,i])) / sd(data_std[,i])} # Create a variable that contains the date in a view that R can deal with: date <- as.Date(data$time, format="%m/%d/%Y") data_std$time <- date # Create the new dataframe "datalili" newdata_wheat <- data_std[,1:2] names(newdata_wheat)[2]<-paste("price") newdata_wheat["commodity"] <- "wheat" datalili <- newdata_wheat' – Lili Matic Apr 15 '16 at 10:33
  • I would still recommend to use a proper time series object (e.g., `zoo` as in my example) because there are dedicated plotting facilities for this. And the `for()` loop for scaling the data is also unnecessarily clumsily. If you want to scale the data in my example, simply include `FX <- scale(FX)` prior to plotting. – Achim Zeileis Apr 15 '16 at 19:08