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:
- 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 - Create plots for each id in each of the above 4 cases which should be between
frame
and Velocity (bothsvel
andpvel
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)
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)
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?