I am trying to do a histogram lattice plot and I would like the
histogram to be filled with a different colour in each panel.
Note: I want every bar in each histogram to be the same colour,
but that there should be different colours *between* histograms.
Can't seem to get this to work. I thought that something like
the following would be a goer:
set.seed(42)
X <- rnorm(200)
A <- factor(sample(letters[1:5],200,TRUE))
DF <- data.frame(x=X,a=A)
print(histogram(~x|a,data=DF,col=2:6,type="count",
panel=function(x,...,subscripts,col) {
panel.histogram(x,...,col=col[subscripts])
}))
However it somewhat mysteriously colours the first bar/rectangle
of the histogram appropriately in the last three panels, leaving
all of the others blank, and leaves all bars blank in the first
two panels.
Can I do what I want? How? Thanks for any advice (other than
``Go stick your head in a pig.'' :-) )
cheers,
Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
On Tue, Oct 7, 2008 at 8:54 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:> > I am trying to do a histogram lattice plot and I would like the > histogram to be filled with a different colour in each panel. > > Note: I want every bar in each histogram to be the same colour, > but that there should be different colours *between* histograms. > > Can't seem to get this to work. I thought that something like > the following would be a goer: > > set.seed(42) > X <- rnorm(200) > A <- factor(sample(letters[1:5],200,TRUE)) > DF <- data.frame(x=X,a=A) > print(histogram(~x|a,data=DF,col=2:6,type="count", > panel=function(x,...,subscripts,col) { > panel.histogram(x,...,col=col[subscripts]) > })) > > However it somewhat mysteriously colours the first bar/rectangle > of the histogram appropriately in the last three panels, leaving > all of the others blank, and leaves all bars blank in the first > two panels. > > Can I do what I want? How? Thanks for any advice (other than > ``Go stick your head in a pig.'' :-) )You could always use ggplot2: library(ggplot2) qplot(X, data=DF, geom="histogram", fill=A, facets = . ~ a, binwidth=0.5) Hadley -- http://had.co.nz/
On 10/7/08, Rolf Turner <r.turner at auckland.ac.nz> wrote:> > I am trying to do a histogram lattice plot and I would like the > histogram to be filled with a different colour in each panel. > > Note: I want every bar in each histogram to be the same colour, > but that there should be different colours *between* histograms. > > Can't seem to get this to work. I thought that something like > the following would be a goer: > > set.seed(42) > X <- rnorm(200) > A <- factor(sample(letters[1:5],200,TRUE)) > DF <- data.frame(x=X,a=A) > print(histogram(~x|a,data=DF,col=2:6,type="count", > panel=function(x,...,subscripts,col) { > panel.histogram(x,...,col=col[subscripts]) > })) > > However it somewhat mysteriously colours the first bar/rectangle > of the histogram appropriately in the last three panels, leaving > all of the others blank, and leaves all bars blank in the first > two panels.That's because you are ending up with 'col[subscripts]' being a vector, most elements of which are NA.> Can I do what I want? How?print(histogram(~x|a,data=DF,col=2:6,type="count", panel=function(x,...,col) { panel.histogram(x,...,col=col[packet.number()]) })) -Deepayan