Erin Steiner
2008-Jan-15 01:30 UTC
[R] Overlay plots from different data sets using the Lattice package
#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
hadley wickham
2008-Jan-15 01:38 UTC
[R] Overlay plots from different data sets using the Lattice package
On Jan 14, 2008 7:30 PM, Erin Steiner <steiner.erin at gmail.com> wrote:> #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)It isn't lattice, but this is pretty easy to do with ggplot2: install.packages("ggplot2") library(ggplot2) qplot(vals, ..density.., data = data.frame.A, geom="histogram", facets = factor.1 ~ factor.2, binwidth = 1) + geom_density(data=data.frame.B) In ggplot2, every "layer" on the plot can have a different dataset. You can find out more about ggplot2 at http://had.co.nz/ggplot2/, but I'm still working on the documentation for these more advanced features. Hadley -- http://had.co.nz/
Deepayan Sarkar
2008-Jan-15 01:51 UTC
[R] Overlay plots from different data sets using the Lattice package
On 1/14/08, Erin Steiner <steiner.erin at gmail.com> wrote:> #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)The first step would be to combine the two data sources: df.comb <- make.groups(data.frame.A, data.frame.B) I would then just overlay two density plots: densityplot(~vals | factor.1 * factor.2, df.comb, groups = which, plot.points=FALSE, auto.key = TRUE) but you could do a histogram and a densityplot too: histogram(~vals | factor.1 * factor.2, df.comb, type = "density", groups = which, panel = panel.superpose, panel.groups = function(x, group.number, col, ...) { if (group.number == 1) panel.histogram(x, ...) else panel.densityplot(x, ..., plot.points = FALSE) }) -Deepayan
Dieter Menne
2008-Jan-15 18:27 UTC
[R] Overlay plots from different data sets using the Lattice package
hadley wickham <h.wickham <at> gmail.com> writes:> qplot(vals, ..density.., data = data.frame.A, geom="histogram", facets > = factor.1 ~ factor.2, binwidth = 1) + geom_density(data=data.frame.B)uhh.. how should I understand "y (?)=..density.." Dieter
hadley wickham
2008-Jan-15 18:37 UTC
[R] Overlay plots from different data sets using the Lattice package
On Jan 15, 2008 12:27 PM, Dieter Menne <dieter.menne at menne-biomed.de> wrote:> hadley wickham <h.wickham <at> gmail.com> writes: > > > qplot(vals, ..density.., data = data.frame.A, geom="histogram", facets > > = factor.1 ~ factor.2, binwidth = 1) + geom_density(data=data.frame.B) > > uhh.. how should I understand "y (?)=..density.."Yes, that does require a little explanation. The .. tells ggplot the variable (density) is not in the original dataset, but is instead produce by the statistical transformation (stat_bin for the histogram and stat_density for the density plot). By default the histogram uses ..count.. (the raw count) while density uses ..density.., so y ..density.. tells them both to use the same scale (you could also use y = ..count.. but count on a density is plot is a little less common) Hadley -- http://had.co.nz/