title: How to facet data using 3 conditions using ggplot in R?
body: <p>I have a big data frame (more than 400,000 rows) named as <code>df</code>.</p>
  <h1>About Dataset</h1>
  <p>This data set is related to vehicle movements in every 0.1 seconds. The relevant variables for this question are explained below:<br />
  <code>class</code> &amp; <code>pclass</code> = Class of vehicle i.e. 1=motorcycle, 2=car, 3=truck<br />
  <code>id</code> = Unique ID of a vehicle<br />
  <code>frame</code> = Unique ID of a frame in which vehicle was observed. Every frame is 0.1 seconds long<br />
  <code>svel</code> = Velocity of a subject vehicle<br />
  <code>pvel</code> = Velocity of a vehicle which is in front of  (preceding) subject vehicle</p>
  <h1>Question</h1>
  <p>I want to do following with the data set:</p>
  <ol>
  <li>Subset the data set based on the <code>class</code> &amp; <code>pclass</code>.
  This will create 4 cases: car following car, car following truck,
  truck following car and truck following truck</li>
  <li>Create plots for each id in each of the above 4 cases which should be between <code>frame</code> and Velocity (both <code>svel</code> and <code>pvel</code> i.e. 2
  lines on a single plot)</li>
  </ol>
  <h1>What I've tried</h1>
  <pre><code>df &lt;- 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))
  </code></pre>
  <p>I wrote 2 pieces of code to create plots as follows:</p>
  <pre><code>  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 = &quot;Vehicle&quot;) +
        facet_grid(class~pclass)
  </code></pre>
  <p><img src="https://i.stack.imgur.com/4Grp7.png" alt="enter image description here" /></p>
  <p>and:</p>
  <pre><code>  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 = &quot;Vehicle&quot;) +
        facet_grid(class~pclass)
  </code></pre>
  <p><img src="https://i.stack.imgur.com/grIo1.png" alt="enter image description here" /></p>
  <p>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?</p>