I'm sure this question has been asked before, but I'm having trouble finding a solution that works:
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).
For example, the data set is:
group_a <- 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) <- c("yvar", "xvar", "group", "sample")
group_b <- 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) <- c("yvar", "xvar", "group", "sample")
sample_data <- rbind(group_a, group_b)
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:
require(ggplot2)
p <- ggplot(sample_data, aes(x = xvar, y = yvar)) + geom_line(aes(color = group, linetype = group))
print(p)
Which produces something like:

So the line is split by group, but at each time point it follows each individual case vertically, rather than as a mean.
What I'm looking for is something more like what's suggested in this other answer: Plot time series with ggplot with confidence interval, but with multiple lines on the graph, and not necessarily a continuous ribbon plot.
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!




