Skip to content

Lattice question.

3 messages · Rolf Turner, Felix Andrews

#
Hi.

I am trying to do histograms in lattice, and I want to get both
counts and percents in the same plot.  To try to be clearer ---
there are 3 levels to my factor; I'd like to get a 2 x 3 array
of plots where the top row consist of histograms by counts and
the bottom consists of (the corresponding) histograms by percent.
I tried the following:

# Demo.

library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
                                             TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c(1,2),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX,        panel=function 
(x,...,type) {
                 panel.histogram(x,...,type=c("count","percent")[gps])
         }
))

But I got 2 x 3 array of plots each containing only the text string  
``Error using packet
<1, ..., 6> object "gps" not found.''

I tried various other fiddle-arounds and got nowhere.

Is it possible to do what I want?  If so, how?

Thanks.

	cheers,

		Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
Modifying your example...


library(lattice)
set.seed(42)
XX <- data.frame(y=runif(300,0,10),a=factor(sample(letters[1:3],300,
                                           TRUE,c(0.5,0.3,0.2))))
XX <- rbind(XX,XX)
XX$gps <- rep(c("count","percent"),each=300)
print(histogram(~y|a*gps,as.table=TRUE,data=XX,
panel=function(x,...,type) {
               panel.histogram(x,...,type=c("count","percent")[current.row()])
       }
))


but the use of current.row() breaks if the layout changes.
Also, the y scales are the same here for count and percent data, which
may not be appropriate. You may have to set the y scales for each
panel explicitly. Or, another option is:

a <- histogram(~y|a, data = XX, subset = gps=="count", type="count")
b <- histogram(~y|a, data = XX, subset = gps=="density", type="density")
library(latticeExtra)
update(c(count=a, density=b), layout=c(3,2), ylab=c("count", "density"))



2009/3/30 Rolf Turner <r.turner at auckland.ac.nz>:

  
    
#
Thanks very much for your input.
On 30/03/2009, at 12:29 PM, Felix Andrews wrote:

            
I kind of get the feeling that doing what I want will require
	constant ad hoc adjustment, so this is not really a worry.
Indeed.  The thought had crossed my mind ....
Yes, I'd like to ... but I can't figure out how.  I tried
	a bit of an experiment and got the impression that
	panel.histogram() did not respond to specifications of ylim.
	I may just have done something silly ....
That looks very interesting; I'll give it a try.

	Thanks.

		cheers,

			Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}