title: Plotting average of multiple groups across time in ggplot2
body: <p>I'm sure this question has been asked before, but I'm having trouble finding a solution that works:</p>
  
  <p>I have a data frame comprising two groups of 5 samples each, where each sample has ten observations spaced equally across time. I would like to plot this dataset as a time series with two lines linking the average of each group at each time point. At each time point I would like to have some measure of variability (e.g. 95% confidence interval). </p>
  
  <p>For example, the data set is:</p>
  
  <pre><code>group_a &lt;- data.frame(runif(50, min=80, max=100), 1:10, rep("a", 10), c(rep("i", 10), rep("ii", 10), rep("iii", 10), rep("iv", 10), rep("v", 10)))
  
  names(group_a) &lt;- c("yvar", "xvar", "group", "sample")
  
  group_b &lt;- data.frame(runif(50, min=60, max=80), 1:10, rep("b", 10), c(rep("vi", 10), rep("vii", 10), rep("viii", 10), rep("ix", 10), rep("x", 10)))
  
  names(group_b) &lt;- c("yvar", "xvar", "group", "sample")
  
  sample_data &lt;- rbind(group_a, group_b)
  </code></pre>
  
  <p>So each time point (xvar) has 10 cases (sample) of observations (yvar), split equally into two groups (group). The closest I have come to the answer I'm looking for is by the following:</p>
  
  <pre><code>require(ggplot2)
  
  p &lt;- ggplot(sample_data, aes(x = xvar, y = yvar)) + geom_line(aes(color = group, linetype = group))
  
  print(p)
  </code></pre>
  
  <p>Which produces something like:</p>
  
  <p><img src="https://i.stack.imgur.com/wLagX.png" alt=""></p>
  
  <p>So the line is split by group, but at each time point it follows each individual case vertically, rather than as a mean. </p>
  
  <p>What I'm looking for is something more like what's suggested in this other answer: <a href="https://stackoverflow.com/questions/51993044/plot-time-series-with-ggplot-with-confidence-interval">Plot time series with ggplot with confidence interval</a>, but with multiple lines on the graph, and not necessarily a continuous ribbon plot. </p>
  
  <p>Does anyone have any suggestions? I know this should be really simple, but I'm relatively new to R and ggplot and apparently can't find the right search terms (or am missing something really obvious). Any help is very much appreciated!</p>