0
votes

I have this statement in R:

plot(net.trips~step, data=subset1, main="Speed = 1.60",type="b", col=subset1$run)

It will produce five different graphs of time series data (one each for the value of subset1$run) on one plot in five different colors with points.

However, when I do this to get lines:

plot(net.trips~step, data=subset1, main="Speed = 1.60",type="l", col=subset1$run)

I get a plot with each data series black instead of five different colors.

Why is this happening, and how do I correct it?

Here is some sample data I cooked up:

RUN   STEP  NUM.TRIPS  
1     1     2   
1     2     4  
1     3     3  
2     1     5  
2     2     2  
2     3     7  

There should be two data series on the same plot:
Data series 1: (1,2),(2,4),(3,3)
Data series 2: (1,5),(2,2),(3,7)

What happens is that if I use points (type="b"), data series 1 and data series 2 will have different colors.
If I use lines (type="l"), data series 1 and data series 2 will both be black

1
Bit hard to say without knowing what subset1 actually is. Can you provide a chunk of the file via dput?thelatemail

1 Answers

0
votes

If I get you right, you want to plot two data series. However, what you are doing is plotting one data series using a col argument which affects the points if type='b'.

Also, it's confusing if your sample data used different colnames than you use in your plot command (net.trips vs. num.trips).

You can write a function that will split your data into subdata sets and draws a line for each:

subset1 <- data.frame(
            run = c(1,1,1,2,2,2), 
            step = c(1,2,3,1,2,3), 
            net.trips = c(2,4,3,5,2,7)
           )

plot.subdata <- function(x){
  # create an "empty" plot first
  plot(net.trips~step, data=x, type="n", main="Speed = 1.60")

  # and fill it with dataseries subsetted by run
  for(n in unique(x$run)){
    lines(net.trips~step, data = subset(x, x$run == n), col = n)
  }
}

plot.subdata(subset1)

It's not the most elegant way, but it will work.