rplotggplot2mixed-modelslsmeans

Line plot of mixed models / lsmeans results (with ggplot?)


I have longitudinal repeated measures on individuals over 4 timepoints. Following a mixed models analysis with time as fixed effect and random slopes I have used lsmeans to estimate the mean values at each time point as well as 95% confidence intervals. I would now like to plot a line graph with time points (x) and mean values of my outcome variable (y) with the CIs. Can I use e.g. ggplot to plot the results that I got from lsmeans? Or is there another smart way to plot this?

The results that I get from lsmeans, and that I would like to plot (lsmean, lower.CL, upperCL over time), are:

$lsmeans
time    lsmean        SE df  lower.CL upper.CL
0    21.967213 0.5374422 60 20.892169 23.04226
1    16.069586 0.8392904 60 14.390755 17.74842
2    13.486802 0.8335159 60 11.819522 15.15408
3     9.495137 0.9854642 60  7.523915 11.46636

Confidence level used: 0.95 

Solution

  • Is this what you meant?

    # To convert from lsmeans output (d <- lsmeans(paramaters))
    d <- summary(d)$lsmeans[c("lsmean", "lower.CL", "upper.CL")]
    
    library(ggplot2)
    ggplot(d, aes(time)) +
        geom_line(aes(y = lsmean)) +
        geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL),
                      width = 0.2) +
        geom_point(aes(y = lsmean), size = 3, 
                   shape = 21, fill = "white") +
        labs(x = "Time", y = "ls mean",
             title = "ls mean result over time") +
        theme_bw()
    

    Dirty solution