RE: [R] Need some quick help with lattice - barchart

From: Ghosh, Sandeep <sghosh_at_lexgen.com>
Date: Fri 06 May 2005 - 05:01:37 EST


Hi Sundar,

Greatly appreicate your response, but I get this error on running the barchart cmd

with(testdata, print(barchart(mean ~ month | year, data = testdata,

          layout = c(1, nlevels(testdata$year)),
          horizontal = FALSE,
          ylim = c(1, max(testdata$mean) + max(testdata$stdDev)),
          main = 'Marble Burying - Level I',
          xlab = 'Months',
          ylab = 'Mean',
          sd = testdata$stdDev,
          scales = list(x = list(at = 1:7, labels = unique(sort(testdata$month)))),
          panel = function(x, y, ..., sd, subscripts) {
            panel.barchart(x, y, ...)
            sd <- sd[subscripts]
            panel.segments(x, y - sd, x, y + sd, col = 'red', lwd = 2)
          })

))

Error in calculateGridLayout(x, rows.per.page, cols.per.page, number.of.cond, :

        Invalid value for labels

-Sandeep

-----Original Message-----
From: Sundar Dorai-Raj [mailto:sundar.dorai-raj@pdf.com] Sent: Thursday, May 05, 2005 1:44 PM
To: Ghosh, Sandeep
Cc: r-help@stat.math.ethz.ch; MSchwartz@medanalytics.com Subject: Re: [R] Need some quick help with lattice - barchart

Ghosh, Sandeep wrote on 5/5/2005 11:10 AM:
> For the following code below, the x-axis ticks are 1,2,3,4,5,6,7 when I was expection them to be 1,2,8,9,10,11,12. Please help me figure out where is the mistake.
>
>
> library(lattice)
>
> testdata <- as.data.frame(t(structure(c(
> 1,2005,9.24,6.18,634,
> 2,2005,8.65,6.05,96,
> 8,2004,6.81,6.51,16,
> 9,2004,9.0,7.29,8,
> 10,2004,8.84,6.18,524,
> 11,2004,8.54,6.35,579,
> 12,2004,9.97,6.3,614,
> 12,2005,8.75,5.84,32,
> ), .Dim=c(5,8))))
>
> 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), ]
>
> png('lexstar_3241.png', width=600, height=as.numeric(length(levels(testdata$year))*200), pointsize=8)
> trellis.par.set(theme = col.whitebg())
>
> with(testdata, print(barchart(as.numeric(mean) ~ month | year, data=testdata,
> layout=c(1,length(levels(year))),
> horizontal=FALSE,
> scales=list(y=list(limits=c(1,max(as.numeric(mean))+max(as.numeric(stdDev))))),
> main='Marble Burying - Level I',
> xlab='Months',
> ylab='Mean',
> sd = as.numeric(as.character(stdDev)),
> panel= function(x, y, ..., sd, subscripts) {
> panel.barchart(x, y, ...);
> sd <- sd[subscripts];
> panel.segments(as.numeric(x), y - sd, as.numeric(x), y + sd, col = 'red', lwd = 2);
> }
> )))
>
> dev.off()
>
> Any help is greatly appreciated...
>
> Thanks,
> Sandeep
>
> [[alternative HTML version deleted]]

Hi Sandeep,

First, you are overusing as.numeric and as.character. Is there a reason for this that the example doesn't demonstrate.

Second, scales$y$limits is the same as setting ylim. The latter is more readable in my opinion.

As to your actual question, you should supply a scales argument for the x-axis. Below is what I came up with:

barchart(mean ~ month | year, data = testdata,

          layout = c(1, nlevels(testdata$year)),
          horizontal = FALSE,
          ylim = c(1, max(testdata$mean) + max(testdata$stdDev)),
          main = 'Marble Burying - Level I',
          xlab = 'Months',
          ylab = 'Mean',
          sd = testdata$stdDev,
          scales = list(x = list(at = 1:7,
                          labels = unique(sort(testdata$month)))),
          panel = function(x, y, ..., sd, subscripts) {
            panel.barchart(x, y, ...)
            sd <- sd[subscripts]
            panel.segments(x, y - sd, x, y + sd, col = 'red', lwd = 2)
          })


HTH, --sundar



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 May 06 05:05:52 2005

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