Generate multiple lattice plots on a grid using lapply in R -
how plot multiple lattice plots onto single lattice plot plots generated using lapply function?
the following demonstration of have tried far using built in mtcars
dataset.
require(lattice) response <- c("cyl","disp","hp","drat") par(mfrow=c(2,2)) lapply(response, function(variable) { print(xyplot(mtcars$mpg ~ mtcars[variable])) })
this produces plots desired. seems ignoring par(mfrow=c(2,2))
instruction , plotting each plot separately.
if don't want use built-in facetting or viewport options of lattice, can replicate behavior of par(mfrow)
following,
require(lattice) response <- c("cyl","disp","hp","drat") # save plots in list pl <- lapply(response, function(variable) { xyplot(mtcars$mpg ~ mtcars[variable]) }) library(gridextra) # arrange them in 2x2 grid do.call(grid.arrange, c(pl, nrow=2))
Comments
Post a Comment