1

I would like to create a plot that looks like this: enter image description here

I have been trying to do this, but not sure how to do it. So here is my reproducible data:

   library(ggplot2)
   library(nlme)
   mydata <- data.frame(SID=sample(1:150,400,replace=TRUE),age=sample(50:70,400,replace=TRUE), sex=sample(c("Male","Female"),200, replace=TRUE),time= sample(1:3,400, replace=TRUE), Vol =rnorm(400),HCD =rnorm(400))
   mydata$time <- as.numeric(mydata$time)
   model = lme(Vol ~ age+sex*time+time* HCD, random=~1|SID, na.action="na.omit",data=mydata);summary(model)
  gp <- ggplot(mydata, aes(x=age, y=Vol, group=SID))+geom_line(aes(group=time, col=sex))
 print(gp)

Unfortunately, this is what I get: enter image description here

I tried to limit the time points to two points, but still doesn't look great. Any suggestions on how I can improve this?

Many thanks!

user6121484
  • 143
  • 2
  • 15
  • 1
    Where does the model you've made come into this? Is every line represented by two points or is it variable? its not exactly clear which points of data you want to be the "lines". Despite this you can use `interaction` with the grouping in ggplots, maybe you are after something like `+geom_line(aes(group=interaction(SID, sex, time), col=sex))` – zacdav Oct 20 '16 at 05:36
  • Every line represents the change in Vol over time per subject, plotting against age for that subject (on x-axis). The interaction indeed gives me what I was looking for, thank you. However, I noticed that some lines are vertical or some are much longer than the others. Why is that? Is there a way to make it nicer? – user6121484 Oct 20 '16 at 14:56

0 Answers0