[R] Overlay plots from different data sets using the Lattice package
Erin Steiner
steiner.erin at gmail.com
Tue Jan 15 02:30:36 CET 2008
#After spending the entire day working on this question, I have
decided to reach out for support:
#I am trying to overlay a densityplot from one data set over a
histogram of another, if I were to plot the two individually, they
would look like:
# data frame construction
data.frame.A <- data.frame(rnorm(12*8), c(rep("one", 4), rep("two",
4), rep("three", 4)), c("red", "orange", "yellow", "green"))
names(data.frame.A) <- c("vals", "factor.1", "factor.2")
data.frame.B <- data.frame(rnorm(12*15), c(rep("one", 4), rep("two",
4), rep("three", 4)), c("red", "orange", "yellow", "green"))
names(data.frame.B) <- names(data.frame.A)
# stand alone plots
histogram(~ vals|factor.1*factor.2, data.frame.A, type = "density")
densityplot(~ vals | factor.1*factor.2, data.frame.B, plot.points = F)
# I found http://www.ats.ucla.edu/STAT/R/faq/overlay_trellis.htm
xyplot(write~read | ses.f, hsb2,
panel=function(x, y, subscripts){
panel.xyplot(x, y, pch=16)
panel.lmline(x, y, lty=4)
panel.xyplot(hsb2$science[subscripts], hsb2$math[subscripts], pch=3)
panel.lmline(hsb2$science[subscripts], hsb2$math[subscripts])
}, as.table=T, subscripts=T)
# but it seems that the overlay only works in cases where the data
comes from the same data.frame and has the same subscripting.
# I have attempted a variety of incarnations of:
histogram(~ vals|factor.1*factor.2, data.frame.A, type = "density",
panel=function(x, ...){
panel.histogram(x,...)
panel.densityplot(~ vals | factor.1*factor.2, data.frame.B,
plot.points = F) })
# which obviously doesn't work.
# I'm not sure if the problem lies in my poor understanding of writing
functions or my poor understanding of Lattice. Is it indeed possible?
# Thank you for your time.
# Erin
More information about the R-help
mailing list