[R] Confidence interval bars on Lattice barchart with groups
Tim Churches
tchur at optushome.com.au
Sat Jun 25 11:11:21 CEST 2005
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)}
))
##################################################################
More information about the R-help
mailing list