Need help with panel.segment..
On Mon, 2005-04-25 at 11:13 -0500, Ghosh, Sandeep wrote:
Hi All,
For the following code, I'm not sure why the error bars are appearing
horizontal. Can someone please tell me how to fix the problem.
testdata <- as.data.frame(t(structure(c
(1,2004,"LV1",3.8,2,87,2,2004,"LV1",3.2,3,28,3,2004,"LV1",3.4,3,88,4,2004,"LV1",3,2,26,5,2004,
"LV1",3.8,2,87,6,2004,"LV1",3.2,3,28,7,2004,"LV1",3.4,3,88,8,2004,"LV1",3,2,26,9,2004,
"LV1",3.8,2,87,10,2004,"LV1",3.2,3,28,11,2004,"LV1",3.4,3,88,12,2004,"LV1",3,2,26,1,2005,
"LV1",3.8,2,87,2,2005,"LV1",3.2,3,28,3,2005,"LV1",3.4,3,88,4,2005,"LV1",3,2,26), .Dim=c(6,16))));
colnames(testdata) <- c('month', 'year',
'dataset','mean','stdDev','miceCount');
testdata[c("month", "mean")] <- lapply(testdata[c("month", "mean")],
function(x) as.numeric(levels(x)[x]));
testdata <- testdata[do.call("order", testdata), ];
trellis.par.set(theme = col.whitebg());
with(testdata,
barchart(mean ~ month | year,
horizontal=FALSE,
layout=c(1,2),
origin = 0,
sd = as.numeric(as.character(stdDev)),
count = as.numeric(as.character(miceCount)),
panel = function(x, y, ..., sd, count, subscripts) {
panel.barchart(x, y, ...)
sd <- sd[subscripts]
count <- count[subscripts]
panel.segments(x - sd / sqrt(count),
as.numeric(y),
x + sd / sqrt(count),
as.numeric(y),
col = 'red', lwd = 2)
}))
<snip>
The original code that Deepayan provided to you did not have 'horizontal
= FALSE' in the barchart() call.
Thus, by including that in yours, you rotated the chart 90 degrees,
which means that you need to alter the panel.segments call to reflect
that change by transposing the x and y values:
trellis.par.set(theme = col.whitebg())
with(testdata,
barchart(mean ~ month | year,
horizontal=FALSE,
layout=c(1,2),
origin = 0,
sd = as.numeric(as.character(stdDev)),
count = as.numeric(as.character(miceCount)),
panel = function(x, y, ..., sd, count, subscripts) {
panel.barchart(x, y, ...)
sd <- sd[subscripts]
count <- count[subscripts]
# NOTE THE CHANGE HERE
panel.segments(as.numeric(x),
y - sd / sqrt(count),
as.numeric(x),
y + sd / sqrt(count),
col = 'red', lwd = 2)
}))
HTH,
Marc Schwartz