miroth
September 20, 2019, 2:44pm
1
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 repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
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
system
Closed
October 11, 2019, 10:48pm
4
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.