Ghosh, Sandeep
2005-Apr-21 22:44 UTC
[R] Need help with R date handling and barchart with errorbars
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)))); 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>testdataI 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. 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. 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. Any help or advise is greatly appreciated... Thanks, Sandeep. [[alternative HTML version deleted]]
Mulholland, Tom
2005-Apr-22 01:45 UTC
[R] Need help with R date handling and barchart with errorbars
Fristly when you are using a package (in this case date) put it in your email. Dates are stored as numbers and if I recall correctly as.date will be the number of days since sometime in 1960. As with other objects there are generally methods that will ensure that the correct printed format will occur. So what that means is that packages that are aware of date or it's methods will print formatted dates. In other cases however you will be required to do that formatting for that package. Since I don't use date I can't tell you how prevalent support is. So barplot(table(testdata$date)) gives you the numeric format while barplot(table(date.mmddyy(testdata$date))) gives you the mmddyy format I don't know about lattice but I know that in the past I've had issues using dates (POISX in particular) but a lot of work has gone in over the past year (which is how long it is since I used the package) and I think these things are handled much better than they were. I think getting stuck in and checking the list for dates and lattice will give you enough starting points such as http://finzi.psych.upenn.edu/R/Rhelp02a/archive/45948.html. I'm sure there are more. Tom> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Ghosh, Sandeep > Sent: Friday, 22 April 2005 6:45 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Need help with R date handling and barchart with > errorbars > > > 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)))); > 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. > > 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. > > 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. > > Any help or advise is greatly appreciated... > > Thanks, > Sandeep. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Deepayan Sarkar
2005-Apr-22 02:12 UTC
[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
Ghosh, Sandeep
2005-Apr-28 21:00 UTC
[R] Need help with R date handling and barchart with errorbars
Hi, I'm having problems getting the resulting R image generated as an png file when trying to feed the R cmds through a java prog to R. I was facing the same problem once before and Deepayan had suggested to use "print" along with the chart cmd like print(barchart(...)), but the print cmd was not needed when running the r cmds directly inside R Console. Below is the r code that is generated by my java prog. and using JGR (http://stats.math.uni-augsburg.de/JGR/) fed to R. testdata <- as.data.frame(t(structure(c( 4,2001,8.7,4.62,83, 4,2002,10.2,3.6,153, 4,2003,10.2,3.82,239, 4,2004,9.4,3.34,344, 5,2001,10.7,5.8,168, 5,2002,11.2,3.79,179, 5,2003,10.2,4.16,245, 5,2004,9.7,3.52,433, 6,2001,10.0,4.24,115, 6,2002,10.5,4.04,171, 6,2003,10.2,3.92,242, 6,2004,9.5,3.72,442, 7,2000,5.13,1.25,6, 7,2001,9.2,5.03,146, 7,2002,10.2,4.45,170, 7,2003,10.6,3.92,240, 7,2004,8.8,3.43,482, 8,2000,7.2,1.34,20, 8,2001,9.2,4.29,109, 8,2002,9.9,4.25,180, 8,2003,10.0,3.83,231, 8,2004,8.6,3.4,505, 9,2000,7.23,2.44,226, 9,2001,9.1,4.17,63, 9,2002,9.4,4.08,223, 9,2003,10.6,3.54,238, 9,2004,9.7,3.69,578, 10,2000,6.13,0.69,68, 10,2001,7.6,4.19,111, 10,2002,9.3,3.81,233, 10,2003,10.7,3.59,214, 10,2004,9.6,3.41,596, 11,2000,6.33,2.14,80, 11,2001,8.2,4.62,91, 11,2002,9.9,3.99,187, 11,2003,10.4,3.6,245, 11,2004,9.5,3.63,579, 12,2000,8.0,3.55,36, 12,2001,10.0,4.56,106, 12,2002,10.4,4.29,191, 12,2003,9.7,3.34,246, 12,2004,9.3,3.4,650, 1,2001,8.7,4.15,86, 1,2002,10.0,4.2,157, 1,2003,8.8,4.18,217, 1,2004,9.1,3.7,206, 1,2005,9.5,3.62,634, 2,2001,9.2,5.34,127, 2,2002,11.1,3.42,126, 2,2003,9.7,3.89,227, 2,2004,9.0,3.59,171, 2,2005,9.2,4.09,95, 3,2001,8.8,3.77,124, 3,2002,10.1,4.38,113, 3,2003,10.3,3.83,235, 3,2004,8.7,3.28,392, ), .Dim=c(5,56)))); colnames(testdata) <- c('month', 'year', 'mean','stdDev','miceCount'); testdata$month <- as.numeric(testdata$month); testdata$year <- factor(testdata$year); testdata <- testdata[do.call("order", testdata), ]; trellis.par.set(theme = col.whitebg()); monthLabel <- c( 'Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec' ); png('391.png', width=600, height=as.numeric(length(levels(testdata$year))*400), pointsize=8); print(with(testdata, barchart(as.numeric(mean) ~ month | year, data=testdata, layout=c(1,length(levels(testdata$year))), horizontal=FALSE, scales=list(x=list(labels=monthLabel)), 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(as.numeric(x), y - sd/sqrt(count), as.numeric(x), y + sd/sqrt(count), col = 'red', lwd = 2) }))); dev.off(); As always, any help is greatly appreciated -----Original Message----- From: Deepayan Sarkar [mailto:deepayan at stat.wisc.edu] Sent: Friday, April 22, 2005 6:19 PM To: Ghosh, Sandeep Subject: Re: [R] Need help with R date handling and barchart with errorbars On Friday 22 April 2005 14:27, you wrote:> Hi Deepayan, > > Thanks so much for the response. Please bear with me as I'm very new > to R. Just for a little background, the data to plot is > going to come to me from a java ArrayList where each row will be like > the rows I have in my sample data. Instead of writing to a file and > then loading it into R (to save time) we want to skip the step and > somehow directly load the data into a R dataframe. The easiest way I > found was, the approach of converting the whole ArrayList into a csv > format and then using templates create the dataframe code as below > and load it into R.That may well be the best way (I'm not familiar with R-Java interfaces). Deepayan
Reasonably Related Threads
- Need help with panel.segment..
- Need help arranging the plot in different fashion than the default format
- Need some quick help with lattice - barchart
- Multiple samba instances on same machine in v4.8 and beyond
- [PATCH 1/1] appliance: init: Avoid running degraded md devices