-2

I have a dataframedf

head(df)

     X0    X1    X2    X3    X4    X5    X6    X7    X8    X9 X10   X11   X12   X13   X14
1  99.6 106.2  70.2 137.4 212.4 601.2 109.8 157.8 229.2 126.6 237 129.6 131.4 196.8 417.6
2  99.6 106.2 208.8 137.4 159.0 601.2 109.8 134.4 229.2 126.6 237 107.4 131.4 237.6 417.6
3 180.0 103.2 111.6 274.8 144.6 601.2 109.8 138.0 229.2 126.6 237  69.0  95.4 248.4 417.6
4 135.6 103.2 106.2 274.8 144.6 601.2 139.8 139.8 229.2 126.6 237 125.4  93.6 142.2 417.6
5 105.6 103.2 108.6 162.6 149.4 601.2 152.4  67.8 229.2 105.0 237 133.8  93.6 240.0 417.6
6 106.8 103.2 143.4  66.0 132.6 601.2 133.2 105.6 229.2 136.8 237 133.8 231.6 240.0 417.6

Every row is a timeserie, how can I plot all the timeseries into one figure?

xirururu
  • 5,028
  • 9
  • 35
  • 64
  • 1
    Possible duplicate of [Plotting multiple time series on the same plot using ggplot()](http://stackoverflow.com/questions/19921842/plotting-multiple-time-series-on-the-same-plot-using-ggplot) – Shawn Mehan Oct 21 '15 at 17:34
  • 1
    Possible duplicate of [R - Plot multiple columns as years on x-axis, plot rows as different lines](http://stackoverflow.com/questions/32254821/r-plot-multiple-columns-as-years-on-x-axis-plot-rows-as-different-lines) – MrFlick Oct 21 '15 at 17:35
  • @ShawnMehan I have more than hundred timeseries, I can not create each line manually – xirururu Oct 21 '15 at 17:45
  • @MrFlick Sorry for the error tagging. I mean R and corrected it. – xirururu Oct 21 '15 at 17:46
  • @MrFlick Thanks for the answer. But is there a better solution? Because I want to at the end add an extra timeseries with another line style. Can I use ggplot to do it? – xirururu Oct 21 '15 at 17:48

1 Answers1

3

Usually time series are placed in columns rather than rows so transpose, convert to zoo and use autoplot to create a ggplot2 plot with legends:

library(zoo)
library(ggplot2)

p <- autoplot(as.zoo(t(df)), facet = NULL)
p

Omit facet=NULL if you want separate panels for each series.

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Hi G. Grothendieck, thanks very much for the answer. But I wish to add an extra time series in this figure with other line style at the end. Is it also possible with autoplot()? – xirururu Oct 21 '15 at 17:51
  • 3
    You can use all the facilities of ggplot2. See `?autoplot.zoo` for more. – G. Grothendieck Oct 21 '15 at 17:52
  • Hi G.Grothendieck, thanks very much for the tip. I tried with the ggplot, but I still have no idea, how to add an extra line in this Figure. For example, I want to add the first time series in it again, but with size=3. I try the following code `p <- autoplot(as.zoo(t(df)), facet = NULL); geom_line(df, y=df[1,], size=3)`, but I failed. Do you know how to do it? – xirururu Oct 21 '15 at 18:20
  • `p + geom_line(aes(Time, 20*demand), data = BOD, col = "red", lwd = 3)` – G. Grothendieck Oct 21 '15 at 18:25
  • Hi G. Grothendieck, I try to change the code, but it still has a problem, how can I set for the x axis??? `p + geom_line(data=df, aes(???, df[1,]), lwd=3)` – xirururu Oct 21 '15 at 18:35
  • I tried ggplot(df, aes(x=c(1:15), y=df[1,]))+geom_line(), but it is wrong. I have no idea, what's wrong with this code. The only way I can do is transposing the df, then add an column "id" for index. then use the "id" for the x-axis, then it works. (May be, it is because of some type error. But I have totally no idea... ) – xirururu Oct 21 '15 at 20:51
  • You can post another question. – G. Grothendieck Oct 21 '15 at 21:02