title: Repeated measures plot: overlay mean trajectory and error bars (ggplot2)
body: <p>I have made two plots of some repeated measures scores. The first plot is of individuals scores (i.e., scores for each participant are plotted). The second is a plot of mean scores and error bars. I want to combine these two plots so that the second plot overlays the first, or add the mean trajectory and error bars to the first plot. How can this be done?</p>
  <p>Example data and the code for creating the plots are below.</p>
  <p><a href="https://i.stack.imgur.com/95QYZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/95QYZ.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/6HAns.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6HAns.png" alt="enter image description here" /></a></p>
  <p><strong>CODE</strong></p>
  <pre><code># Renames variables for reshaping  
  names(data_wide) [names(data_wide) == &quot;score.baseline&quot;] &lt;- &quot;score.0&quot;
  names(data_wide) [names(data_wide) == &quot;score.wave1&quot;] &lt;- &quot;score.1&quot;
  names(data_wide) [names(data_wide) == &quot;score.wave2&quot;] &lt;- &quot;score.2&quot;
  names(data_wide) [names(data_wide) == &quot;score.wave3&quot;] &lt;- &quot;score.3&quot;
  
  #Convert wide-formatted data into long  
  data_long &lt;- reshape(as.data.frame(data_wide), 
                               idvar=&quot;ID&quot;, 
                               varying=c(&quot;score.0&quot;, &quot;score.1&quot;, &quot;score.2&quot;, &quot;score.3&quot;),
                               direction=&quot;long&quot;)
  
  # Prepare means data
  
  data_long.group &lt;- data_long %&gt;% 
                      group_by(time) %&gt;% 
                      summarise(
                      score_mean = (mean(score, na.rm=TRUE)),
                      score_sd = (sd(score, na.rm=TRUE))
                      ) 
  
  # Plot individual scores
  
  ggplot(data_long, aes(x = factor(time), y = score, color = as.factor(ID), group = ID)) + 
    geom_line(size=0.2) + 
    theme_classic(base_size = 18) + 
    theme(legend.position = &quot;none&quot;) + 
    labs(title = &quot;Trajectories&quot;, y = &quot;data score&quot;, x = &quot;Wave&quot;) 
  
  # Plot group-level means and SDs 
  
  ggplot(data_long.group, aes(x = time, y = score_mean)) + 
    geom_line(size=0.2) + 
    theme_classic(base_size = 18) + 
    theme(legend.position = &quot;none&quot;) + 
    labs(title = &quot;Trajectories&quot;, y = &quot;data score&quot;, x = &quot;Wave&quot;) +
    geom_errorbar(aes(ymin=score_mean-score_sd, ymax=score_mean+score_sd), width=.2,
                   position=position_dodge(0.05))
  
  </code></pre>
  <p><strong>EXAMPLE DATA</strong></p>
  <pre><code>data_wide &lt;- structure(
      list(ID = c(1, 2, 3, 4, 5, 6), 
      score.baseline = c(4, 4, 5, 4, 6, 4), 
      score.wave1 = c(3.5, 5.67, 5.33, 4, 6.67, 4.5), 
      score.wave2 = c(4, 5, NA, 4, 6.67, 4), 
      score.wave3 = c(6, 6, 4.67, 4.33, 6.67, 3)), 
      row.names = c(1L, 2L, 3L, 4L, 5L, 6L), 
      class = c(&quot;data.frame&quot;))
  
  </code></pre>