[R] adding error bars to lattice plots

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Oct 13 23:46:15 CEST 2006


On 10/13/06, Daniel E. Bunker <deb37 at columbia.edu> wrote:
> Dear Deepayan and Sundar,
>
> Thank you so much for your help with this.  However, I may have phrased
> my problem too specifically, assuming that *in general* I could apply
> your response to all Lattice graphics.
>
> What I need is a barchart or vertical dotchart, with error bars, across
> three treatments, with a form that should look something like:
> barchart(median~fac1|by1, groups=group1).
>
> Your solution works great with the problem I had posed
> <xyplot(voice.part ~ median|by1, groups=group1, [snip]>, yet when I
> switch the axes for a vertical dotchart <xyplot(median~voice.part|by1,
> groups=group1, [snip]> the error bars remain horizontal and do not
> follow the switched axes.
>
> I tried to alter all 'lx' and 'ux' to 'ly' and 'uy' in panel.ci and
> prepanel.ci, but to no avail.  I'm afraid I have not (YET) managed to
> understand just how Lattice works.
>
> Re. moving panel.abline() from panel.groups to panel, does that mean I
> need to rewrite panel.superpose?

Only if you consider writing a function B that uses function A
rewriting function A.

> Again, any advice would be greatly appreciated.

Continuing with the singer example, the following works for me:


prepanel.ci <- function(x, y, ly, uy, subscripts, ...)
{
    y <- as.numeric(y)
    ly <- as.numeric(ly[subscripts])
    uy <- as.numeric(uy[subscripts])
    list(ylim = range(y, uy, ly, finite = TRUE))
}

panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...)
{
    x <- as.numeric(x)
    y <- as.numeric(y)
    ly <- as.numeric(ly[subscripts])
    uy <- as.numeric(uy[subscripts])
    panel.arrows(x, ly, x, uy, col = 'black',
                 length = 0.25, unit = "native",
                 angle = 90, code = 3)
    panel.xyplot(x, y, pch = pch, ...)
}


with(singer.ucl,
     xyplot(median ~ voice.part, groups=group1,
            ly = lower, uy = upper,
            prepanel = prepanel.ci,
            panel = function(x, y, ...) {
                panel.abline(v = unique(as.numeric(x)),
                             col = "grey")
                panel.superpose(x, y, ...)
            },
            panel.groups = panel.ci,
            pch = 16))

Let us know if anything is not obvious.

-Deepayan



More information about the R-help mailing list