plotting longitudinal data

Hey everyone

I have longitudinal data with 4 timepoints. I want to plot a line for every person in my sample and add a mean line of the change of the whole sample. I managed to plot a line for each person (see code below), but I dont achieve to plot the meanline of the sample...

Does anyone have an advice?

R-code:
xyplot(CSIf~time, mydata, group = codec,type=c('r'), points = TRUE)

thank you so much
Michelle

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Do you want to add "straight" lines for each person, or the exact data? If the latter, you could use:

library(lme4)
#> Loading required package: Matrix
library(ggplot2)
data("sleepstudy")

ids <- sample(levels(sleepstudy$Subject), size = 8, replace = FALSE)
sleepstudy <- dplyr::filter(sleepstudy, Subject %in% ids)

ggplot(sleepstudy, aes(x = Days, y = Reaction)) +
  geom_line(aes(color = Subject)) +
  geom_smooth(method = "lm")

Created on 2019-09-21 by the reprex package (v0.3.0)

Else, this code might be what you're looking for:

library(lme4)
#> Loading required package: Matrix
library(ggplot2)
data("sleepstudy")

ids <- sample(levels(sleepstudy$Subject), size = 8, replace = FALSE)
sleepstudy <- dplyr::filter(sleepstudy, Subject %in% ids)

ggplot(sleepstudy, aes(x = Days, y = Reaction)) +
  geom_smooth(aes(color = Subject), method = "lm", se = FALSE) +
  geom_smooth(method = "lm", se = FALSE, color = "black")

Created on 2019-09-21 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.