Plot mts (multivariate time series) in ggplot2


ggplot2 does not support ts plot by default, but easily plot after brief data transformation.


# data: class(z) -> mts
z <- ts(matrix(rnorm(300), 100, 3), start=c(1961, 1), frequency=12)

# ggplot2 version. data transformation is critical
ggplot( melt(data.frame(time=as.numeric(time(z)), z), id.vars="time"), aes(time, value)) + 
  geom_line() +
  facet_grid(variable ~ .)

# basic graphics package supports easy mts plot

Here is an example of base graphics version:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s