-3

I have a xts object containing a number of timeseries. The data looks like:

head(data)

   date           v1          v2          v3         v4         v5            v6  
2014-07-31         NA         721        696         NA         487          469
2014-08-02        735         752        696        559         505          469
2014-08-04       1502         737        696        757         510          469
2014-08-06        799         722        697        559         487          469
    ...

"date" is a date variable and the other variables contain price developments. I would like to automatically plot all of the series (so v1, v2, v3), without manually inserting their names. This could be done using xtsExtra, but this package is no longer available for R3.1.0.

Is there a way to plot these time-series in one window using ggplot2? (incl. labeling and different colours)

Many thanks!

florinho_33
  • 1
  • 1
  • 3
  • [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – shekeine Oct 20 '14 at 17:10
  • May be the answer provided on this link would help http://stackoverflow.com/questions/18801556/multiple-time-series-in-one-plot – Meso Oct 20 '14 at 17:16

1 Answers1

2

You can plot multiple line series using the group argument to ggplot. From your original dataframe, you may need to reformat things using melt from the reshape2 package.

library(ggplot2)
library(reshape2)
df<-data.frame(date=as.Date(c('2014-06-25','2014-06-26','2014-06-27')),v1=rnorm(3),v2=rnorm(3))
newdf<-melt(df,'date')
ggplot(newdf,aes(x=date,y=value,group=variable,color=variable) ) + geom_line() +scale_x_date()
keegan
  • 2,892
  • 1
  • 17
  • 20