I have some data in a CSV file: time,pos,t,tl 15:23:44:350,M1_01,4511,1127 15:23:44:350,M1_02,4514,1128 15:23:44:350,M1_03,4503,1125 ... 15:23:44:491,M2_01,4500,1125 15:23:44:491,M2_02,4496,1124 15:23:44:491,M2_03,4516,1129 ... 15:23:44:710,M3_01,4504,1126 15:23:44:710,M3_02,4516,1129 15:23:44:710,M3_03,4498,1124 ... Each pos (eg M1_01) is an independent time series. I would like to plot each time series as lines on a single plot and I wondered if there was something more straight forward than I was attempting. I got as far as: fname = 't100.csv' t = read.csv(fname) tpos = split(t, t$pos) plot(tpos[["M1_01"]]$t, type='l') for (p in names(tpos)) { lines(tpos[[p]]$t) } which seems to work but then I got stuck on how to make each line a different colour and figured that there might a be a one liner R command to do what I want. Any tips would be appreciated.
On Wed, 2005-10-05 at 22:19 +1000, sosman wrote:> I have some data in a CSV file: > > time,pos,t,tl > 15:23:44:350,M1_01,4511,1127 > 15:23:44:350,M1_02,4514,1128 > 15:23:44:350,M1_03,4503,1125 > ... > 15:23:44:491,M2_01,4500,1125 > 15:23:44:491,M2_02,4496,1124 > 15:23:44:491,M2_03,4516,1129 > ... > 15:23:44:710,M3_01,4504,1126 > 15:23:44:710,M3_02,4516,1129 > 15:23:44:710,M3_03,4498,1124 > ... > > Each pos (eg M1_01) is an independent time series. I would like to plot > each time series as lines on a single plot and I wondered if there was > something more straight forward than I was attempting. > > I got as far as: > > fname = 't100.csv' > t = read.csv(fname) > tpos = split(t, t$pos) > plot(tpos[["M1_01"]]$t, type='l') > for (p in names(tpos)) { > lines(tpos[[p]]$t) > } > > which seems to work but then I got stuck on how to make each line a > different colour and figured that there might a be a one liner R command > to do what I want. > > Any tips would be appreciated.See the examples in ?plot.ts for some approaches. You will need to review ?ts to create time series objects from your data to be used in plot.ts(). Another approach, which is not specific to time series, is ?matplot. HTH, Marc Schwartz
sosman a ??crit : lines() has a color argument from the online help : ?lines lines(x, y = NULL, type = "l", col = par("col"),...
Assuming, as in your example, that you want to plot your series against 1, 2, 3, ... first form a ts series, my.series, and then plot it using the col= argument: my.series <- do.call("cbind", lapply(tpos, ts)) m <- ncol(my.series) ts.plot(my.series, col = 1:m) Another possibility is to use col = rainbow(m) in the last line. On 10/5/05, sosman <sourceforge at metrak.com> wrote:> I have some data in a CSV file: > > time,pos,t,tl > 15:23:44:350,M1_01,4511,1127 > 15:23:44:350,M1_02,4514,1128 > 15:23:44:350,M1_03,4503,1125 > ... > 15:23:44:491,M2_01,4500,1125 > 15:23:44:491,M2_02,4496,1124 > 15:23:44:491,M2_03,4516,1129 > ... > 15:23:44:710,M3_01,4504,1126 > 15:23:44:710,M3_02,4516,1129 > 15:23:44:710,M3_03,4498,1124 > ... > > Each pos (eg M1_01) is an independent time series. I would like to plot > each time series as lines on a single plot and I wondered if there was > something more straight forward than I was attempting. > > I got as far as: > > fname = 't100.csv' > t = read.csv(fname) > tpos = split(t, t$pos) > plot(tpos[["M1_01"]]$t, type='l') > for (p in names(tpos)) { > lines(tpos[[p]]$t) > } > > which seems to work but then I got stuck on how to make each line a > different colour and figured that there might a be a one liner R command > to do what I want. > > Any tips would be appreciated. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >