Search code examples
rgraphlatticetrellis

Plotting Observed Vs Predicted variables on the same graph in Lattice


I'm trying to plot observed and predicted variables on the same plot in lattice. My data is a repeated dataset and I've tried a few things which haven't worked. Any assistance would be much appreciated. Code is given below.

library(nlme)
library(lattice)
# add random conc predictions to data
Theoph$predConc <- rnorm(132, 5)

# My attempt at plotting both predConc and conc against time on the same plot
lattice::xyplot(predConc + conc ~ Time | Subject, groups=Subject, data=Theoph, type="l", layout = c(4,4))

As you can see, it doesn't seem to be doing what I want it to do. Ideally I would like the "conc" and "predConc" to be in different colours but appear together on each panel for each Id so I can compare the two easily.


Solution

  • As was suggested in the comments, it is fixed simply by dropping groups = Subject.

    lattice::xyplot(predConc + conc ~ Time | Subject, data = Theoph, type = "l",
                    auto.key = TRUE)
    

    Imgur