This answer shows how to use groups
and panel.superpose
to display overlapping histograms in the same panel, assigning different colors to each his
panel.histogram()
doesn't have a formal groups=
argument, and if you examine its code, you'll see that it handles any supplied groups=
argument differently and in a less standard way than panel.*()
functions that do. The upshot of that design decision is that (as you've found) it's not in general easy to pass in to it vectors of graphical parameters specifying per-group appearance
As a workaround, I'd suggest using latticeExtra's +()
and as.layer()
functions to overlay a number of separate histogram()
plots, one for each group. Here's how you might do that:
library(lattice)
library(latticeExtra)
## Split your data by group into separate data.frames
foo.df <- data.frame(x=c(rnorm(10),rnorm(10)+2), cat=c(rep("A", 10),rep("B", 10)))
foo.A <- subset(foo.df, cat=="A")
foo.B <- subset(foo.df, cat=="B")
## Use calls to `+ as.layer()` to layer each group's histogram onto previous ones
histogram(~ x, data=foo.A, ylim=c(0,75), breaks=seq(-3, 5, 0.5),
lwd=2, col="transparent", border="black") +
as.layer(
histogram(~ x, data=foo.B, ylim=c(0,75), breaks=seq(-3, 5, 0.5),
lwd=2, col="cyan", border="red")
)