[R] Need help with panel.segment..
Ghosh, Sandeep
sghosh at lexgen.com
Mon Apr 25 18:13:29 CEST 2005
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:
> Hi All..
>
> Have a question.. For the following r code
>
> testdata <- as.data.frame(t(structure(c(
> "1/1/04","LV1",3.8,2,87,
> "2/1/04","LV1",3.2,3,28,
> "3/1/04","LV1",3.4,3,88,
> "4/1/04","LV1",3,2,26,
> "5/1/04","LV1",3.8,2,87,
> "6/1/04","LV1",3.2,3,28,
> "7/1/04","LV1",3.4,3,88,
> "8/1/04","LV1",3,2,26,
> "9/1/04","LV1",3.8,2,87,
> "10/1/04","LV1",3.2,3,28,
> "11/1/04","LV1",3.4,3,88,
> "12/1/04","LV1",3,2,26,
> "1/1/05","LV1",3.8,2,87,
> "2/1/05","LV1",3.2,3,28,
> "3/1/05","LV1",3.4,3,88,
> "4/1/05","LV1",3,2,26
> ), .Dim=c(5,16))));
Which makes all the columns factors. Odd choice.
> colnames(testdata) <-
> c('date','dataset','mean','stdDev','miceCount');
> testdata[c("date")] <- lapply(testdata[c("date")],
> function(x) as.date(levels(x)[x]));
> testdata[c("mean")] <- lapply(testdata[c("mean")], function(x)
> as.numeric(levels(x)[x]));
>
> On trying to print the data frame
>
> >testdata
>
> I get this..
>
> date dataset mean stdErr miceCount
> 1 -20454 LV1 3.8 2 87
> 2 -20423 LV1 3.2 3 28
> 3 -20394 LV1 3.4 3 88
> 4 -20363 LV1 3.0 2 26
> 5 -20333 LV1 3.8 2 87
> 6 -20302 LV1 3.2 3 28
> 7 -20272 LV1 3.4 3 88
> 8 -20241 LV1 3.0 2 26
> 9 -20210 LV1 3.8 2 87
> 10 -20180 LV1 3.2 3 28
> 11 -20149 LV1 3.4 3 88
> 12 -20119 LV1 3.0 2 26
> 13 -20088 LV1 3.8 2 87
> 14 -20057 LV1 3.2 3 28
> 15 -20029 LV1 3.4 3 88
> 16 -19998 LV1 3.0 2 26
>
> where as when I run this
>
> >dates <- c(lapply(testdata[c("date")], function(x)
> > as.date(levels(x)[x])));
>
> the ouput is
> $date
> [1] 1Jan4 1Feb4 1Mar4 1Apr4 1May4 1Jun4 1Jul4 1Aug4 1Sep4 1Oct4
> 1Nov4 1Dec4 [13] 1Jan5 1Feb5 1Mar5 1Apr5
>
> Question:
> 1. Can someone please explain me why the difference.
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.
> 2. I later want to plot the data using barchart eg (barchart(date ~
> mean | dataset, data=testdata);) in which case will the dates appear
> in assending order of dates or something special needs to be done for
> that.
If all you want is the dates in the right order, you could do:
testdata$date <- factor(testdata$date, levels = testdata$date)
> 3. Also I'll really appreciate if anyone can tell me if there's a way
> to get stdErrorBars on charts that are drawn using barchart function
> in lattice package.
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
More information about the R-help
mailing list