[R] Overlay plots from different data sets using the Lattice package
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Jan 15 02:51:10 CET 2008
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
More information about the R-help
mailing list