[R] Confidence interval bars on Lattice barchart with groups

From: Tim Churches <tchur_at_optushome.com.au>
Date: Sat 25 Jun 2005 - 19:11:21 EST


I am trying to add confidence (error) bars to lattice barcharts (and dotplots, and xyplots). I found this helpful message from Deepayan Sarkar and based teh code below on it:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/50299.html

However, I can't get it to work with groups, as illustrated. I am sure I am missing something elementary, but I am unsure what.

Using R 2.1.1 on various platforms. I am aware of xYplot in the Hmisc library but would prefer to avoid any dependency on a non-core R library, if possible.

Tim C

##################################################################
# set up dummy test data
testdata <- data.frame(
dsr=c(1,2,3,4,5,6,7,8,9,10,0,1,2,3,4,5,6,7,8,9,
      2,3,4,5,6,7,8,9,10,11,3,4,5,6,7,8,9,10,11,12),
year=as.factor(c(1998,1998,1998,1998,1998,1998,1998,1998,1998,1998,
                 1999,1999,1999,1999,1999,1999,1999,1999,1999,1999,
                 2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,
                 2001,2001,2001,2001,2001,2001,2001,2001,2001,2001)),
geog_area=c('North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle',
            'North','South','East','West','Middle'),
sex=c('Male','Male','Male','Male','Male',

'Female','Female','Female','Female','Female',
'Male','Male','Male','Male','Male',
'Female','Female','Female','Female','Female',
'Male','Male','Male','Male','Male',
'Female','Female','Female','Female','Female',
'Male','Male','Male','Male','Male',
'Female','Female','Female','Female','Female'),
age=c('Old','Old','Old','Old','Old',
'Young','Young','Young','Young','Young',
'Old','Old','Old','Old','Old',
'Young','Young','Young','Young','Young',
'Old','Old','Old','Old','Old',
'Young','Young','Young','Young','Young',
'Old','Old','Old','Old','Old',
'Young','Young','Young','Young','Young'))

# add dummy lower and upper confidence limits testdata$dsr_ll <- testdata$dsr - 0.7
testdata$dsr_ul <- testdata$dsr + 0.5

# examine the test data
testdata

# check that a normal barchart with groups works OK - it does barchart(geog_area ~ dsr | year, testdata, groups=sex, origin = 0)

# this works as expected, but not sure what teh error messages mean with(testdata,barchart(geog_area ~ dsr | year + sex,

              origin = 0,
              dsr_ul = dsr_ul,
              dsr_ll = dsr_ll,
              panel = function(x, y, ..., dsr_ll, dsr_ul, subscripts) {
                  panel.barchart(x, y, subscripts, ...)
                  dsr_ll <- dsr_ll[subscripts]
                  dsr_ul <- dsr_ul[subscripts]
                  panel.segments(dsr_ll,
                                 as.numeric(y),
                                 dsr_ul,
                                 as.numeric(y),
                                 col = 'red', lwd = 2)}
              ))

# no idea what I am doing wrong here, but there is not one bar per group... something
# to do with panel.groups???
with(testdata,barchart(geog_area ~ dsr | year, groups=sex,

              origin = 0,
              dsr_ul = dsr_ul,
              dsr_ll = dsr_ll,
              panel = function(x, y, ..., dsr_ll, dsr_ul, subscripts,
groups) {
                  panel.barchart(x, y, subscripts, groups, ...)
                  dsr_ll <- dsr_ll[subscripts]
                  dsr_ul <- dsr_ul[subscripts]
                  panel.segments(dsr_ll,
                                 as.numeric(y),
                                 dsr_ul,
                                 as.numeric(y),
                                 col = 'red', lwd = 2)}
              ))
##################################################################

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jun 25 19:16:46 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:02 EST