[R] barchart (lattice) with text labels

Mark and Heather Lyman lymanmh at xmission.com
Sat Feb 24 20:20:37 CET 2007


Deepayan Sarkar wrote:
> On 2/24/07, Mark and Heather Lyman <lymanmh at xmission.com> wrote:
>> I would like to place the value for each bar in barchart (lattice) at
>> the top of each bar. Something like the following code produces.
>>
>> library(lattice)
>>
>> mypanelfunc <- function(x, y, ...)
>> {
>>   panel.barchart(x, y, ...)
>>   panel.text(x, y, labels=as.character(round(x,2)), ...)
>>   }
>>
>> myprepanelfunc <- function(x, y, ...) list(xlim=c(0, max(x)+.1))
>>
>> mydata <- expand.grid(a=factor(1:5), b=factor(1:3), c=factor(1:2))
>> mydata$x <- runif(nrow(mydata))
>>
>> barchart(a~x|b, mydata, groups=c, panel=mypanelfunc,
>> prepanel=myprepanelfunc, adj=c(-0.1,0.5))
>>
>> However, I cannot figure out how to shift the values to correspond with
>> their respective grouped bar.
>
> You should look at panel.barchart and try to reproduce the
> calculations done there.
>
> Deepayan
>
That's what I was afraid of. I made a half-hearted attempt at that, but 
when I didn't work, I hoped there was an easier way. Well, I will give 
it another try.

Mark Lyman



More information about the R-help mailing list