[R] errorbar il lattice plot

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Fri Sep 28 18:15:10 CEST 2007


On 9/28/07, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
> Hi Everyone,
>
> I would like to add errorbars to a lattice plot. I already have a function
> that
> adds error bars to a plot (originally written by George Gilchrist) which
> takes
> as arguments the coordinates of the point and then the size of the error
> bar. I
> would like to integrate it in my lattice code but I seem to be stumped.
>
> I can put the error bar function inside my code as if it were a panel
> function,
> but it then sits there quite unused. I am not even clear how what data
> object to
> put the standard errors for the thing to work...

I'm not sure why you expect anything to happen. Your 'errbar' argument
gets passed to the panel function panel.xyplot, but of course it knows
nothing about any such argument and ignores it. Even if you manage to
get 'errbar' executed, it's not clear where the 'err' values are going
to come from. Finally, for a lattice panel function, you need to
replace segments() by panel.segments(), etc.

Lattice does contain a demo showing how to draw confidence intervals; see

file.show(system.file("demo/intervals.R", package = "lattice"))

There have been a couple of follow up messages dealing with how to
extend this to grouped displays; these are found easily by an online
search.

-Deepayan

>
> My code (the version that at least draws a plot):
>
> xyplot(inbreeding~generations|breeds, data = trellisplot, type = 'l', layout
> =
> c(2,5), col = 'black', lwd = 3,
> errbar = function(x, y, err, down = T, width = 0.005, lwd = 1, plot = T,
> colour="black")
> {
>    up <- y + err
>    dn <- y - err
>    x.range <- max(x) - min(x)
>    wid.lf <- x - (x.range * width)
>    wid.rt <- x + (x.range * width)
>    if(plot == F) {
>      return(data.frame(x, up, dn))
>    }
>    else {
>      if(down == F) {
>        segments(x, up, x, y, lwd = lwd, lty = 1, col = colour)
>        segments(wid.lf, up, wid.rt, up, lwd = lwd, lty = 1, col = colour)
>      }
>      else {
>        segments(x, up, x, dn, lwd = lwd, lty = 1, col = colour)
>        segments(wid.lf, up, wid.rt, up, lwd = lwd, lty = 1, col = colour)
>        segments(wid.lf, dn, wid.rt, dn, lwd = lwd, lty = 1, col = colour)
>      }
>      invisible(data.frame(x, up, dn))
>    }
> }
> )
>
>
> Regards,
>
> Federico Calboli
>
> --
> Federico C. F. Calboli
> Department of Epidemiology and Public Health
> Imperial College, St Mary's Campus
> Norfolk Place, London W2 1PG
>
> Tel  +44 (0)20 7594 1602     Fax (+44) 020 7594 3193
>
> f.calboli [.a.t] imperial.ac.uk
> f.calboli [.a.t] gmail.com
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list