Tim Churches
2005-Jun-25 09:11 UTC
[R] Confidence interval bars on Lattice barchart with groups
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)} )) ##################################################################
Deepayan Sarkar
2005-Jun-25 12:23 UTC
[R] Confidence interval bars on Lattice barchart with groups
On 6/25/05, Tim Churches <tchur at optushome.com.au> wrote:> 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, ...)This is where the warnings are coming from. You are using positional matching to supply (unnamed) arguments, but the third argument of panel.barchart is not 'subscripts'. You should use panel.barchart(x, y, subscripts = subscripts, ...) instead.> 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)} > ))panel.groups will not help here, since placing multiple bars side by side needs specialized calculations, which are done within panel.barchart. To add bars to these, you will need to reproduce those calculations. Things are much easier with dotplot, e.g.: with(testdata, dotplot(geog_area ~ dsr | year, groups=sex, pch = 16, dsr_ul = dsr_ul, dsr_ll = dsr_ll, panel.groups function(x, y, ..., dsr_ll, dsr_ul, subscripts) { dsr_ll <- dsr_ll[subscripts] dsr_ul <- dsr_ul[subscripts] panel.segments(dsr_ll, as.numeric(y), dsr_ul, as.numeric(y), ...) panel.xyplot(x, y, ...) })) Deepayan
Seemingly Similar Threads
- Need some quick help with lattice - barchart
- Need help with panel.segment..
- Need help arranging the plot in different fashion than the default format
- Need help with R date handling and barchart with errorbars
- Samba4 not able to write to group writeable folder???