[R] Histogram in Lattice with 3 factors

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Feb 11 23:46:41 CET 2008


On 2/11/08, willem vervoort <willemvervoort at gmail.com> wrote:
> Dear R-help list,
>
> I am trying to construct a lattice histogram using 3 factors.
>
> My dataframe looks like this: (simulating a waterbalance over
> groundwater with different salinities)
>
> s      days   model    EC    EC_max
> 0.4     1        "A"       10      9
> 0.42   2        "A"       10      9
> 0.44   3        "A"       10       9
>   :        :          :           :         :
> 0.4      1        "B"      10      9
>   :        :          :          :         :
> 0.4      1        "A"      30        9
>   :        :          :          :           :
> 0.4      1        "A"      30        36
>
> Anyway you get the gist
> EC_max has two levels 9 and 36, EC has 3 levels 10, 30 and 70, and
> model has two levels ("A" and "B"). There are say 365 days and s is
> the variable of interest (soil saturation)
>
> Can maybe be reproduced with:
> data <- data.frame(s = rnorm(2*3*365*2),rep(1:365,12), model =
> sort(rep(c("A","B"),6*365)),
>             EC = rep(sort(rep(c(10,30,70),365*2)),2), EC_max =
> rep(sort(rep(c(9,36),3*365)),2))
>
> I would like to plot histograms with the three factors using Lattice
> so I had the following code:
>
> my.strip <- function(which.given, ..., factor.levels) {
>             levs <- if (which.given == 1)  c("Model A","Model B")
>                     else {if(which.given == 2) paste("EC =
> ",as.character(EC),"dS/m")
>                       else paste("ECmax = ",as.character(EC_max),"dS/m")}
>             strip.default(which.given, ..., factor.levels = levs)
>          }
>
> histogram(~s|model*as.factor(EC)*as.factor(EC_max),data=Store,xlab="soil
> saturation",type="density",strip=my.strip)
>
> But I am doing something wrong, because it plots the histogram for
> factor level EC_max =9 first and than straight over it the histogram
> for factor level 36, so only 6 panels on the graph rather than 12.
>
> I searched the archives, but no luck so far.

Look up the 'layout' argument in ?xyplot. By default, for 2 or more
conditioning variables, the levels of the first two define columns and
rows, and the rest are spread out over multiple pages. In your
example, you could try layout = c(6, 2) for starters.

-Deepayan



More information about the R-help mailing list