[R] Add dots at the mean of a bwplot using panel.points

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Nov 4 00:10:24 CET 2005


On 11/3/05, Andy Bunn <abunn at whrc.org> wrote:
> > How can I modify the example below to put a dot at the mean of each
> violin
> > plot? I assume I use panel.points but that's as far as I can go.
> >
> >      bwplot(voice.part ~ height, singer,
> >             panel = function(..., box.ratio) {
> >                 panel.violin(..., col = "transparent",
> >                              varwidth = FALSE, box.ratio = box.ratio)
> >                 #panel.points(mean(x.....))
> >             } )
>
> Well, I answered my own question and learned something of the dark art of
> lattice plots in the process. If this is an inane way to go about this,
> then
> somebody please say so.

panel.mean <- function(x, y, ...) {
    tmp <- tapply(x, y, FUN = mean)
    panel.points(tmp, seq(tmp), pch = 20, ...)
}

is more direct, but otherwise your solution is fine.

-Deepayan

> panel.mean <- function(x,y,...){
>   y <- as.numeric(y)
>   y.unique <- sort(unique(y))
>   for(Y in y.unique) {
>     X <- x[y == Y]
>     if (!length(X)) next
>     mean.value <- list(x = mean(X), y = Y)
>     do.call("lpoints", c(mean.value, pch = 20))
>   }
> }
>
> bwplot(voice.part ~ height, singer,
>             panel = function(...) {
>               panel.violin(...,col = "transparent")
>               panel.mean(...)
>               })




More information about the R-help mailing list