[R] barchart (lattice) with text labels

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Feb 24 18:40:36 CET 2007


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



More information about the R-help mailing list