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

Ghosh, Sandeep sghosh at lexgen.com
Thu May 5 21:22:32 CEST 2005


It worked for me..Here are the commands..

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 <- factor(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()

Thanks a lot Deepayan..

-Sandeep

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



Deepayan Sarkar wrote on 5/5/2005 12:03 PM:
> On Thursday 05 May 2005 13:10, Ghosh, Sandeep wrote:
> 
>>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.
> 
> [...]
> 
>>colnames(testdata) <- c('month', 'year', 'mean','stdDev','miceCount')
>>testdata$month <- as.numeric(testdata$month)
> 
> [...]
> 
>>with(testdata, print(barchart(as.numeric(mean) ~ month | year,
>>data=testdata, layout=c(1,length(levels(year))),
>>    horizontal=FALSE,
> 
> [...]
> 
> 'month' is numeric, so it's being coerced to be a shingle. Try using 
> 'factor(month)' instead in the formula.
> 
> Deepayan
> 

Hi Deepayan,

That was my original thought too. But when I tried it I got:

Error in unit(x0, default.units, units.per.obs) :
	'x' must be numeric

Changing horizontal to TRUE produces the plot, but I'm sure that's not 
what Sandeep wants.

--sundar




More information about the R-help mailing list