Re: [R] multiple line plots

From: sosman <>
Date: Thu 06 Oct 2005 - 18:39:02 EST

Marc Schwartz wrote:
> On Wed, 2005-10-05 at 22:19 +1000, sosman wrote:
>>I have some data in a CSV file:
>>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.

The matplot example looks like the go.

The example data didn't really show the grouping and even though I mentioned time series, simply plotting the t values as an ordered sequence is fine for this application (sorry about the red herring).

The dataset below is what I should have shown:

     pos t

1 M1_01 4511
2 M1_02 4514
3 M1_03 4503
4 M1_01 4500
5 M1_02 4496
6 M1_03 4516
7 M1_01 4504
8 M1_02 4516
9 M1_03 4498

So what I ended up with was:

# Make a wide data set
tw = unstack(t, t ~ pos)
# Results in a list since not all series the same length # Find the shortest dataset
len = min(sapply(tw, length))

setlen = function(l, newlen) { length(l) = newlen } # Not sure why this did not work
#sapply(tw, setlen, len)

for (n in names(tw)) {

     length(tw[[n]]) = len
matplot(data.frame(tw), type='l')

Apart from flying a bit blind, I obtained the plot I was after.

thanks mailing list PLEASE do read the posting guide! Received on Thu Oct 06 19:05:47 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:36 EST