1

I have a big data frame (more than 400,000 rows) named as df.

About Dataset

This data set is related to vehicle movements in every 0.1 seconds. The relevant variables for this question are explained below:
class & pclass = Class of vehicle i.e. 1=motorcycle, 2=car, 3=truck
id = Unique ID of a vehicle
frame = Unique ID of a frame in which vehicle was observed. Every frame is 0.1 seconds long
svel = Velocity of a subject vehicle
pvel = Velocity of a vehicle which is in front of (preceding) subject vehicle

Question

I want to do following with the data set:

  1. Subset the data set based on the class & pclass. This will create 4 cases: car following car, car following truck, truck following car and truck following truck
  2. Create plots for each id in each of the above 4 cases which should be between frame and Velocity (both svel and pvel i.e. 2 lines on a single plot)

What I've tried

df <- data.frame(id=rep(c(1,2,3,4,5,6,7,8,9,10),each=5),
                 frame=rep(1:5,5),
                 class=rep(c(2,3,3,3,2,2,3,2,2,2), each=5),
                 svel=c(15,20,30,15,25,69,45,25,36,45,25,45,45,45,44,40,38,39,39,40,33,34,35,26,50,50,50,50,45,44,43,46,40,35,34,33,32,31,30,32,34,36,38,42,44,46,48,50,52,56),
                 pclass=rep(c(0,2,3,3,3,2,2,3,2,2), each=5),
              pvel=c(0,0,0,0,0,15,20,30,15,25,69,45,25,36,45,25,45,45,45,44,40,38,39,39,40, 33,34,35,26,50,50,50,50,45,44,43,46,40,35,34,33,32,31,30,32,34,36,38,42,44))

I wrote 2 pieces of code to create plots as follows:

  ggplot(data=df, aes(group=id)) + 
      geom_line(mapping=aes(x=frame, y=svel, linetype='subject vehicle')) +
      geom_line(mapping=aes(x=frame, y=pvel, linetype='preceding vehicle')) +
      scale_linetype(name = "Vehicle") +
      facet_grid(class~pclass)

enter image description here

and:

  ggplot(data=df, aes(color=as.factor(id))) + 
      geom_line(mapping=aes(x=frame, y=svel)) +
      geom_point(mapping=aes(x=frame, y=pvel)) +
      scale_linetype(name = "Vehicle") +
      facet_grid(class~pclass)

enter image description here

They somewhat solve the problem of faceting but I want to create a separate plot for every vehicle ID. You can see that right now each plot contains more than 1 id. How can I do that?

Community
  • 1
  • 1
umair durrani
  • 5,597
  • 8
  • 45
  • 85
  • Facets are based on Tufte's idea of "small multiples", and are useful when all the plots off the same data frame, with a data column(s) specifying which data goes in each facet. If your plots are different, then you're better off using `grid.arrange` or one of the other tools mentioned [in this answer](http://stackoverflow.com/q/22996454/903061). – Gregor Thomas May 27 '14 at 20:26
  • Also, since you have two problems that aren't interrelated, asking two different questions probably makes sense. If you want help figuring out how to format your data so facets work, that's a worthwhile question. If you just want to know how to stick plots together, it will probably be closed as a duplicate. – Gregor Thomas May 27 '14 at 20:29
  • @shujaa faceting is important but since data is same I think it would be easy to reply to both in one answer – umair durrani May 27 '14 at 20:50
  • I ran your code. I don't see the grid line issue. Is it illustrated in this data? I'd encourage you to continue trying with the intended gridline functions (panel.grid.major et al.)--make sure you call them *after* you add a theme otherwise they will be overwritten. Also, the data you shared only has one `VehicleID` so it doesn't illustrate your faceting problem either. However that can probably be solved by not using a for loop and instead using `+ facet_wrap(~ Vehicle.ID)`. – Gregor Thomas May 27 '14 at 21:39
  • Thanks for your suggestion. I will investigate more on gridlines. I've provided some sample data and shown some progress, please see how it can be improved – umair durrani May 27 '14 at 22:09
  • Your question is gigantic. It has many small parts that are unrelated. Once again, I suggest you try to break your problem into small pieces, work on them one at a time, and post a small question showing exactly where you're stuck on each piece. Right now 3/4 of your question is unrelated to the title, and it would take someone a long time to answer completely. If you ask small questions, it lets one person answer one piece at a time. In that way, small questions are easier to answer and provide a better resource for future users looking for help. – Gregor Thomas May 27 '14 at 22:19
  • Extra details are removed now. Only asking about faceting now – umair durrani May 27 '14 at 22:31

1 Answers1

1

Probably you want a list of plots. I like to use plyr::dlply for this. Wrap the code to create a plot in a function, call it something like makePlot. (Don't worry about group = id in your function, plyr will only pass data with 1 id at a time.)

Then you can do this:

library(plyr)
myplots <- dlply(.data = df, .variables = id, .fun = makePlot)

Then, myplots should be a list of ggplots. You can print them one at a time with print(myplots[[1]]), you can continue to modify them, and if you want to arrange them all, this question should at least get you a very good start.

If, on the other hand, you like the faceting approach and would like to use it for id, you can give facet_grid a bigger formula, like facet_grid(class ~ id + pclass), but this could make for a very big plot if you have a lot of ids.

Edits:

I don't know offhand what's wrong with dlply and your makePlot. You can always do a loop instead:

myplots <- list()
for (i in unique(df$id)) {
    myplots[[i]] <- makePlot(filter(df, id == i))
}

Looking more carefully at your function and data, you could restructure things like this:

library(reshape2)
dfm <- melt(df, id.vars = c("id", "frame", "class", "pclass"),
            variable.name = "Vehicle", value.name = "Velocity")
levels(dfm$Vehicle) <- c("Subject Vehicle", "Preceding Vehicle")

Then a plot for an individual id gets as simple as

ggplot(data=subset(dfm, id == 1), aes(x = frame, y = Velocity)) +
       geom_line(mapping=aes(linetype = Vehicle))
Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • I can't seem to create a plotting function. I tried this: `makePlot <- function(x){ ggplot(data=x) + geom_line(mapping=aes(x=frame, y=svel, linetype='subject vehicle')) + geom_line(mapping=aes(x=frame, y=pvel, linetype='preceding vehicle')) + scale_linetype(name = "Vehicle") + facet_grid(class~pclass)}` – umair durrani May 27 '14 at 23:07