[R] Error Bars in lattice- barcharts
Ivan Gregoretti
ivangreg at gmail.com
Wed Apr 7 22:03:07 CEST 2010
Hi Sam and everybody,
Can you educate me a little bit on the use of barchart?
A command like this
barchart(~table(someProperty), data=A)
produces a barchart with horizontal bars.
I want to produce that graph but with VERTICAL bars.
clearly, horizontal=FALSE does not work.
Thank you,
Ivan
> sessionInfo()
R version 2.10.0 (2009-10-26)
x86_64-redhat-linux-gnu
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lattice_0.18-3
loaded via a namespace (and not attached):
[1] grid_2.10.0
Ivan Gregoretti, PhD
National Institute of Diabetes and Digestive and Kidney Diseases
National Institutes of Health
5 Memorial Dr, Building 5, Room 205.
Bethesda, MD 20892. USA.
Phone: 1-301-496-1592
Fax: 1-301-496-9878
On Fri, Feb 19, 2010 at 7:22 PM, Sam Albers <tonightsthenight at gmail.com> wrote:
> Hello,
>
> I am attempting to write a script that adds error bars to a barchart. I
> basing my attempt heavily on the following thread:
>
> http://tolstoy.newcastle.edu.au/R/e2/help/06/10/2791.html
>
> I can't seem to get around the problem that was discussed in the thread. The
> following example should illustrate my problem. Sorry about the messy
> example but I am 1) trying to make it as close as possible to my actual work
> and 2) my skill level is spotty at best. Can anyone suggest a way to do this
> or even another way to make a grouped barchart with error bars? I'm not
> married to this method although I prefer working with lattice. Thanks for
> any help in advance!
>
> Sam
>
> #Generating the data
> library(lattice)
>
> temp <- abs(rnorm(81*5))
> err <- as.data.frame(temp)
> err$section=c("down","down","down","mid","mid","mid", "up","up", "up")
>
> err$depth=c("Surface","D50", "2xD50")
>
> err$err.date=c("05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","28/08/2009",
> "28/08/2009", "28/08/2009","28/08/2009", "28/08/2009",
> "28/08/2009","28/08/2009", "28/08/2009", "28/08/2009")
>
>
> err.split <-
> with(err,
> split(temp, list(depth,section, err.date)))
>
> #I've tried to alter the panel function according to the thread to produce
> vertical error bars in my barcharts
>
> 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, lx, ux, subscripts, pch = 16, ...) {
> x <- as.numeric(x)
> y <- as.numeric(y)
> lx <- as.numeric(lx[subscripts])
> ux <- as.numeric(ux[subscripts])
>
> panel.arrows(x, ly, x, uy, col = 'black',
> length = 0.25, unit = "native",
> angle = 90, code = 3)
> panel.barchart(x, y, pch = pch, ...)
> }
>
> se <-function(x) sqrt(var(x)/length(x))
>
>
>
> err.ucl <- sapply(err.split,
> function(x) {
> st <- boxplot.stats(x)
> c(mean(x), mean(x) + se(x), mean(x) -se(x))
> })
>
>
>
> err.ucl <- as.data.frame(t(err.ucl))
> names(err.ucl) <- c("mean", "upper.se", "lower.se")
> err.ucl$label <- factor(rownames(err.ucl),levels = rownames(err.ucl))
>
> # add factor, grouping and by variables
> err.ucl$section=c("down","down","down","mid","mid","mid", "up","up", "up")
> err.ucl$depth=c("Surface","D50", "2xD50")
>
> #There has got to be a better way of doing this
> err.ucl$err.date=c("05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/09/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","05/10/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","12/09/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","13/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","19/10/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","21/09/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","26/10/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","27/09/2009","28/08/2009",
> "28/08/2009", "28/08/2009","28/08/2009", "28/08/2009",
> "28/08/2009","28/08/2009", "28/08/2009", "28/08/2009")
>
> #This produces the figure I am looking for minus the error bars.
>
> with(err.ucl, barchart(mean ~ err.date | section, group=depth,
> layout=c(1,3),
> horizontal=FALSE,
> scales=list(x=list(rot=45)),
> ))
>
>
> # Deepayan's original example. I am unsure how to diagnose the packet error.
> This is where I run into problems
>
> with(err.ucl, barchart(mean ~ err.date | section, group=depth,
> layout=c(1,3),
> horizontal=FALSE,
> scales=list(x=list(rot=45)),
> ly=lower.se,
> uy=upper.se,
> prepanel=prepanel.ci,
> panel=panel.superpose,
> panel.groups=panel.ci
> ))
>
>
>
>
>
> --
> *****************************************************
> Sam Albers
> Geography Program
> University of Northern British Columbia
> 3333 University Way
> Prince George, British Columbia
> Canada, V2N 4Z9
> phone: 250 960-6777
> *****************************************************
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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