Skip to content
Prev 69410 / 398513 Next

Need some quick help with lattice - barchart

Hi Deepayan,

Thanks a lot for your last Friday response. I have yet not got a chance to implement the month names for x -axis ticks yet, but will be on it as soon as some of the bugs reported by users have been resolved. In regard to that one of the users pointed out a problem with appeared to happen whenever the mean data getting plotted is less than 1.


Here's the r code generated by my prog

library(lattice)

testdata <- as.data.frame(t(structure(c(
1,2005,0.53,0.05858,159,
2,2005,0.52,0.05413,143,
3,2005,0.49,0.04986,160,
4,2005,0.5,0.05679,166,
5,2005,0.5,0.05938,32,
1,2004,0.54,0.05774,94,
2,2004,0.56,0.05748,101,
3,2004,0.54,0.0571,101,
4,2004,0.56,0.07045,96,
5,2004,0.55,0.0584,92,
6,2004,0.55,0.06136,96,
7,2004,0.55,0.06219,119,
8,2004,0.55,0.06302,124,
9,2004,0.55,0.06094,127,
10,2004,0.54,0.06482,137,
11,2004,0.54,0.06485,142,
12,2004,0.53,0.05624,157,
1,2003,0.58,0.07141,152,
2,2003,0.57,0.06181,87,
3,2003,0.54,0.05462,116,
4,2003,0.53,0.06139,124,
5,2003,0.52,0.05209,110,
6,2003,0.52,0.0596,125,
7,2003,0.54,0.06634,138,
8,2003,0.52,0.05226,84,
9,2003,0.52,0.05892,128,
10,2003,0.51,0.06095,109,
11,2003,0.53,0.06355,87,
12,2003,0.55,0.0591,117,
1,2002,0.58,0.05102,59,
2,2002,0.56,0.06422,61,
3,2002,0.55,0.06219,64,
4,2002,0.56,0.06965,65,
5,2002,0.56,0.0557,87,
6,2002,0.57,0.06578,73,
7,2002,0.57,0.06617,85,
8,2002,0.58,0.06153,76,
9,2002,0.58,0.06301,107,
10,2002,0.59,0.0747,117,
11,2002,0.59,0.0737,92,
12,2002,0.59,0.06888,100,
1,2001,0.51,0.07221,28,
2,2001,0.48,0.06705,59,
3,2001,0.51,0.06753,71,
4,2001,0.52,0.0738,37,
5,2001,0.49,0.07891,92,
6,2001,0.48,0.06521,66,
7,2001,0.49,0.05509,53,
8,2001,0.53,0.07104,61,
9,2001,0.53,0.06878,45,
10,2001,0.54,0.06661,38,
11,2001,0.51,0.06486,52,
12,2001,0.56,0.05969,59,
7,2000,0.51,0.07261,29,
8,2000,0.54,0.07678,25,
9,2000,0.53,0.07148,47,
10,2000,0.51,0.06535,54,
11,2000,0.44,0.09065,36,
12,2000,0.48,0.04639,4,
), .Dim=c(5,59))))
colnames(testdata) <- c('month', 'year', 'mean','stdDev','miceCount')
testdata$month <- factor(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')

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='DEXA | M | Total Body | BMC - 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);
	   }
)))

I'm having a feeling that the as.numeric conversions is probably the cause of the problem. 

Please advise at your earliest convinience. 

-Sandeep


-----Original Message-----
From: Ghosh, Sandeep 
Sent: Friday, May 06, 2005 11:16 AM
To: Deepayan Sarkar
Cc: 'Sundar Dorai-Raj'
Subject: RE: [R] Re: Need some quick help with lattice - barchart


Want to add a little icing to the cake. Instead of showing the tick to be mnth numbers I want them to be month labels. For this I declare an array 
monthLabels <- c('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec').

I know how to do but not sure how to implement in R syntax. How do I get the current row handle in barchart.. subscripts seems to give the current row index but is only available in panel function. what I nee is function that return 
function(index)(
for(testdata)
	list <- monthLabels [testdata$month[index]]
)

As always any help is greatly appreciated.

-Sandeep 

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


Sorry. I copied your original script incorrectly. Changing to factor 
does indeed work.


--sundar

Ghosh, Sandeep wrote on 5/5/2005 12:22 PM: