RE: [R] Need help with R date handling and barchart with errorbars

From: Marc Schwartz <MSchwartz_at_medanalytics.com>
Date: Fri 29 Apr 2005 - 10:04:17 EST

On Thu, 2005-04-28 at 16:00 -0500, Ghosh, Sandeep wrote:
> 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

If the code that you have above is ALL of the code that gets passed to R, then you are missing a:

library(lattice)

call preceding the graphics related code. Without it, you will get something like:

  Error in eval(expr, envir, enclos) : couldn't find function "barchart"

One thing that you should do is to move the

trellis.par.set(theme = col.whitebg())

so that it is after the png() call, otherwise you get a screen plot device opening up and it affects the screen device and not the png device, since you have not opened the png device yet.

You could also combine both calls into a single trellis.device() call. See ?trellis.device for more information.

If that is not the error that you are getting, you will need to provide more specific information. Your code above seems to generally work, whether pasted directly into the R console or source()'d, so perhaps there is a problem with JGR for which you would need to contact the author directly.

With respect to the use of print(), you should review R FAQ 7.22:

  Why do lattice/trellis graphics not work?

HTH, Marc Schwartz



R-help@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 Received on Fri Apr 29 10:10:26 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:28 EST