[R] labelling a stacked barchart (lattice)

Weidong Gu anopheles123 at gmail.com
Thu Aug 4 21:28:05 CEST 2011


Marc,

Try this one

barchart(data=dta, ~x, group=y,
stack=T,col=sort(brewer.pal(7,"Purples")), xlab="Percent",
box.width=.5, scales=list(tick.number=10),
panel=function(x,y,...){
panel.barchart(x,y,...)
panel.text(cumsum(x)-dta$x/2,y,labels=dta$x)
panel.text(cumsum(x)-dta$x/2,1.3,labels=as.character(dta$y))
})

Weidong Gu

On Wed, Aug 3, 2011 at 9:01 PM, M/K Zodet <mkzodet at comcast.net> wrote:
> All:
>
> Below is my code for creating a basic horizontal, stacked barchart.  I'd like to label the plot in two ways:  1) place the x values in each piece and 2) place the y values above each piece (angled).  I'm currently using lattice, but I'm open to suggestions using ggplot2.
>
> Questions:
>
> 1.  Can this be done?...I assume yes.  So, what are the best options/functions for doing this.
> 2.  Is there a way to alter the transparency of the bar fill with the brewer palette?  I know I can alter this w/ heat.~, topo.~, cm.colors, etc.
>
> Thanks in advance.
>
> Marc
> Using R for Mac OS X GUI 1.40-devel Leopard build 64-bit
>
>
> dta <- data.frame(x=c(46.0, 14.7, 16.4, 15.8, 7.0), y=c("Back", "Neck", "Extrem", "MuscSkel", "Oth"))
> dta
> barchart(data=dta, ~x, group=y, stack=T, col=sort(brewer.pal(7,"Purples")),
>         xlab="Percent", box.width=.5, scales=list(tick.number=10))
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list