[R] Lattice question.

Felix Andrews felix at nfrac.org
Mon Mar 30 01:29:52 CEST 2009


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>:
> 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}}
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Felix Andrews / 安福立
mobile: +61_410400963
http://www.neurofractal.org/felix/
3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8




More information about the R-help mailing list