title: How to graph "before and after" measures using ggplot with connecting lines and subsets?
body: <p>I’m totally new to ggplot, relatively fresh with R and want to make a smashing ”before-and-after” scatterplot with connecting lines to illustrate the movement in percentages of different subgroups before and after a special training initiative. I’ve tried some options, but have yet to:</p>
  
  <ul>
  <li>show each individual observation separately (now same values are overlapping)</li>
  <li>connect the related before and after measures (x=0 and X=1) with lines to more clearly illustrate the direction of variation</li>
  <li>subset the data along class and id using shape and colors</li>
  </ul>
  
  <p>How can I best create a scatter plot using ggplot (or other) fulfilling the above demands?</p>
  
  <h1>Main alternative: geom_point()</h1>
  
  <p>Here is some sample data and example code using genom_point</p>
  
  <pre><code>    x &lt;- c(0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1) # 0=before, 1=after
      y &lt;- c(45,30,10,40,10,NA,30,80,80,NA,95,NA,90,NA,90,70,10,80,98,95) # percentage of ”feelings of peace"
      class &lt;- c(0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,1) # 0=multiple days 1=one day
      id &lt;- c(1,1,2,3,4,4,4,4,5,6,1,1,2,3,4,4,4,4,5,6) # id = per individual
  
      df &lt;- data.frame(x,y,class,id)
  
      ggplot(df, aes(x=x, y=y), fill=id, shape=class) + geom_point()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/t0ojZ.png" alt="Click here for example image based on geom_point()"></p>
  
  <h1>Alternative: scale_size()</h1>
  
  <p>I have explored stat_sum() to summarize the frequencies of overlapping observations, but then not being able to subset using colors and shapes due to overlap.</p>
  
  <pre><code>    ggplot(df, aes(x=x, y=y)) +
        stat_sum()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/AhhaV.png" alt="Click here for example image based on scale_size()"></p>
  
  <h1>Alternative: geom_dotplot()</h1>
  
  <p>I have also explored geom_dotplot() to clarify the overlapping observations that arise from using  genom_point() as I do in the example below, however I have yet to understand how to combine the before and after measures into the same plot.</p>
  
  <pre><code>    df1 &lt;- df[1:10,] # data before
      df2 &lt;- df[11:20,] # data after
  
      p1 &lt;- ggplot(df1, aes(x=x, y=y)) +
        geom_dotplot(binaxis = "y", stackdir = "center",stackratio=2,
             binwidth=(1/0.3))
  
      p2 &lt;- ggplot(df2, aes(x=x, y=y)) +
        geom_dotplot(binaxis = "y", stackdir = "center",stackratio=2,
             binwidth=(1/0.3))
  
      grid.arrange(p1,p2, nrow=1) # GridExtra package
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/12mi0.png" alt="Click here for example image based on geom_dotplot()"></p>