Lattice and LatticeExtra when used for producing 2 plots(Simulated and Real) on the same pane results in an error

Lattice and LatticeExtra when used for producing 2 plots(Simulated and Real) on the same pane result in the following error message:
Error in UseMethod("as.layer") :
no applicable method for 'as.layer' applied to an object of class "NULL"

The R code run in RStudio is as follows:

library(lattice)
library(latticeExtra)

#converting my_df.txt to a data frame called my_df by importing the dataset using RStudio's import Dataset #feature. It's attached as a PDF file called my_df.pdf. Please see the line below.

my_df.pdf (63.8 KB)
The asLayer.R code is saved as a PDF file called asLayer.pdf
asLayer.pdf (54.3 KB)

##The simulated plot
plot.Simulation <- xyplot(CV~Times,
data= subset(my_df, Times > 0.01),
type="l", # 'l' for "loess fit"
main= "Simulated vs Actual CVs",
xlab = "Time (hour)",
ylab = "Concentration (mg/mL)",
scales = list(y=list(limits=c(0,12))))
plot.Simulation # to display the simulated plot

#The real plot
MyData <- read.csv("OTC.IM.Plasma.20.txt", sep = "\t", skip = 5)
head(MyData)
X = MyData$Time
Y = MyData$CV
plot.RealData <- plot(X, Y, type = c("p"), col = 'blue', main = "Actual Data", xlab = c('Time (hours)'), ylab = c('CV (mg/mL)'))

#Layer Simulated with Real Data plot:
plot.Simulation + as.layer(plot.RealData) # This results in the error noted at the very top.

If you read the error message carefully, you see that it complains about plot.RealData being NULL .
The result of the plot function is indeed NULL and that the plot is produced is a side effect.

Maybe you meant to use the dotplot function instead of plot?

Hi Hans,
I was just trying to reproduce code that illustrated it wasn't necessary to use ggplot and that lattice and lattice Extra could be used to produce an output of 2 plots, one from a PBPK model and another from actual (the author called it real) data.
The author was using xyplot.
Thanks,
Mo (I can be also reached via direct message)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.