views:

532

answers:

3

I would like to place the value for each bar in barchart (lattice) at the top of each bar. However, I cannot find any option with which I can achieve this. I can only find options for the axis.

A: 

I would have suggested using the new directlabels package, which can be used with both lattice and ggplot (and makes life very easy for these labeling problems), but unfortunately it doesn't work with barcharts.

Shane
+6  A: 

Create a custom panel function, e.g.

library("lattice")
p <- barchart((1:10)^2~1:10, horiz=FALSE, ylim=c(0,120),
              panel=function(...) { 
                args <- list(...);
                panel.text(args$x, args$y+2, args$y);
                panel.barchart(...)
              })
print(p)

lattice barchart with labels

rcs
A: 

If you are using the groups parameter you will find the labels in @rcs's code all land on top of each other. This can be fixed by extending panel.text to work like panel.barchart, which is easy enough if you know R.

I can't post the code of the fix here for licencing reasons, sorry.

Alex Brown