Reputation: 11
I am trying to build a line plot showing the change of a certain variable over time by group. My data has values from a single person stored in each row and I am struggling to plot the mean and deviation of the said value over time. Here goes an example of a dataset.
ID | Group | Value | Time |
---|---|---|---|
1 | MDD | 14.2 | 8 |
1 | MDD | 15.3 | 9 |
2 | NO-MDD | 11.2 | 8 |
2 | NO-MDD | 12.3 | 9 |
2 | NO-MDD | 12.8 | 5 |
3 | NO-MDD | 9.1 | 3 |
3 | NO-MDD | 11.3 | 1 |
4 | MDD | 10.8 | 5 |
4 | MDD | 14.6 | 18 |
4 | MDD | 15.3 | 37 |
The time variable stores values from 0 to 37 but not for each participant. It is a large dataset though, so there are enough measurements to calculate SD for each time point. My main problem is the way the data is arranged. I was thinking on reshaping the data but not quite sure if that would be helpful. Here is the expected output drawn by hand, sorry!
Thank you in advance for any suggestions you can provide!
Upvotes: 1
Views: 64
Reputation: 123783
One option to achieve your desired result with ggplot2
would be to use multiple stat_summary
layers to add the mean line and points and the errorbars.
As you example data did not allow to compute a standard deviation by time and group I use some fake random example data:
set.seed(123)
df <- data.frame(
Time = sample(1:37, 100, replace = TRUE),
Group = sample(c("MDD", "NO-MDD"), 100, replace = TRUE),
Value = rnorm(100)
)
df$Value <- df$Value + ifelse(df$Group == "MDD", 2, 0)
library(ggplot2)
ggplot(df, aes(Time, Value, color = Group)) +
stat_summary(fun = mean, geom = "line") +
stat_summary(fun.data = mean_se, geom = "errorbar") +
stat_summary(fun = mean, geom = "point")
Upvotes: 1