Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging or overlaying xyplots in a lattice panel

Trying to figure out how to add points or a data series to an existing lattice panel once it's been created. Does this have something to do with plot.points and/or the update function?

# Initialize first plot
library(lattice)
a <- xyplot(Sepal.Length~Sepal.Width, groups=Species, data=iris
            , subset=Species %in% levels(Species)[1:2])
print(a) 

# Create second plot to overlay or merge with the first plot
b <- xyplot(Sepal.Length~Sepal.Width, groups=Species, data=iris
            , subset=Species %in% levels(Species)[3])

# Initial attempt at merging plots:
library(latticeExtra)
print(c(a,b)) # this displays the data in an adjacent second panel
print(c(a,b,layout=c(1,1))) # and this only shows series "b"

Note: in this example, both plots "a" and "b" come from the original iris dataframe, but ideally, the solution would work with distinct dataframes.

Any ideas? Thanks!
Bryan

like image 872
Bryan Avatar asked Oct 22 '13 21:10

Bryan


1 Answers

You're looking for as.layer, I think; try this. It's in the latticeExtra library as well.

library(latticeExtra)
a + as.layer(b)

See documentation here: http://latticeextra.r-forge.r-project.org/#as.layer

like image 163
Aaron left Stack Overflow Avatar answered Nov 04 '22 14:11

Aaron left Stack Overflow