[R] lattice panels with grouped extra data as text?

Rainer Hurling rhurlin at gwdg.de
Fri Jan 27 16:54:10 CET 2012


On 26.01.2012 17:33 (UTC+1), Rainer Hurling wrote:
> I have a problem with including extra data in a lattice graphic. I am
> trying to put some calculated values in an extra column of a boxplot. A
> minimal example should show what I am trying to do:
>
>
> foo <- data.frame(
> Treatment=rnorm(1:12,2),
> Variant=c("A","A","B","C","D","C","B","D","D","B","B","A"),
> Szenario=c("Dry","Wet","Dry","Dry","Wet","Dry","Wet","Wet","Dry","Wet","Dry","Dry"),
> Area=c(sample(100, 12)))
>
> require(lattice)
> require(latticeExtra)
>
> bwplot(Treatment ~ Variant | Szenario, data=foo,
>
> panel = function(...) {
> # Marking the extra column yellow
> panel.xblocks(c(4.5,5.0),
> c(rgb(255,255,0, alpha=127, max=255),
> rgb(255,255,0, alpha=127, max=255)))
> # Put in the extra values (instead of a string)
> panel.text(5, min(foo$Treatment), "sum of foo$area per panel", srt=90,
> adj=c(0,0.5), cex=2)
>
> panel.bwplot(...) },
>
> # Create extra space for a column
> xlim=range(0.5,5.5),
> scales = list(x = list(labels=c(NA,"A","B","C","D","")))
> )
>
>
> I would like to put summarized area values (from Foo$Area) in the yellow
> coloured columns of each panel. So afterwards there should be one sum in
> the first panel and another sum in the next panel (and so on, if more
> than two panels).
>
> It tried a little bit with groups and group.number but without success.

Hope it is ok that I am answering myself. Instead of groups and 
group.number I found a solution with panel.number().

The script is able to produce sums (of areas in this case) for each 
panel and print that number in the panel. Because I would like to use 
two panel variables (Szenario and Age) I expanded the data.frame.


foo <- data.frame(Treatment=rnorm(1:15,2),
   Variant=c("A","A","B","C","C","A","C","D","C","B","D","D","B","B","A"),
   Szenario=c("Dry","Wet","Dry","Dry","Wet","Moist","Wet","Dry",
     "Wet","Moist","Wet","Dry","Wet","Dry","Dry"),
   Age=c("Old","Young","Middle","Young","Old","Old","Middle","Old",
     "Middle","Young","Middle","Young","Old","Young","Middle"),
   Area=c(sample(100, 15)))

require(lattice)
require(latticeExtra)

bwplot(Treatment ~ Variant | Szenario + Age, data=foo,
        layout=c(length(levels(foo$Szenario)),length(levels(foo$Age))),
        panel = function(...) {
          panel.xblocks(c(4.5,5.0),
                        c(rgb(255,255,0, alpha=127, max=255),
                          rgb(255,255,0, alpha=127, max=255)))
          panel.text(5, min(foo$Treatment),
                     sum(foo$Area[foo$Szenario == rep(levels(foo$Szenario),
 
length(levels(foo$Szenario)))[panel.number()] &
                                  foo$Age == rep(levels(foo$Age),
 
each=length(levels(foo$Age)))[panel.number()]]),
                     srt=90, adj=c(0,0.5), cex=2)
          panel.bwplot(...)
        },
        xlim=range(0.5,5.5),
        scales = list(x = list(labels=c(NA,"A","B","C","D","")))
        )


At least this works for me. But I guess there a much more elegant 
solutions possible.


> Is there any easy solution for this?
>
> Any help is really appreciated.
> Rainer Hurling



More information about the R-help mailing list