Skip to content

Need help with panel.segment..

2 messages · Ghosh, Sandeep, Marc Schwartz

#
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)
              }))


Thanks,
Sandeep

-----Original Message-----
From: Deepayan Sarkar [mailto:deepayan at stat.wisc.edu]
Sent: Thursday, April 21, 2005 9:13 PM
To: r-help at stat.math.ethz.ch
Cc: Ghosh, Sandeep
Subject: Re: [R] Need help with R date handling and barchart with
errorbars
On Thursday 21 April 2005 17:44, Ghosh, Sandeep wrote:
Which makes all the columns factors. Odd choice.
No difference (except that the print method for data.frame's doesn't 
know about 'date's.).  Note that you have managed to create dates 
approximately 2000 years in the past.
If all you want is the dates in the right order, you could do:

testdata$date <- factor(testdata$date, levels = testdata$date)
The S language encourages the user to program the little things that are 
not readily available. In this case (making a guess about what sort of 
error bar you want):


with(testdata, 
     barchart(date ~ as.numeric(as.character(mean)) | dataset,
              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)
              }))


which would have been more readable if everything in your data frame 
were not factors. (It's none of my business, but I think dotplots would 
be a better choice than barcharts if you want to add error bars.)

Deepayan
#
On Mon, 2005-04-25 at 11:13 -0500, Ghosh, Sandeep wrote:
<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