[R] barchart panel.text add label value and percent

Peter Ehlers ehlers at ucalgary.ca
Sun Jan 30 14:24:05 CET 2011


On 2011-01-26 04:56, Christophe Bouffioux wrote:
> Hello everybody,
>
> i need some help to display text as label in my barchart
> the label is the combination of x value + text
> text= calculated percentage =>  per
> it display properly the x value
> but, wrongly repeats the text of the fisrt level LangueTXT factor on the
> second
>
> any solution?
> Thanx very much
> Christophe
>
>
> ######## here is the code ##########
> library(lattice)
> Langue<- c(1, 1, 1, 2, 2, 2, 2)
> n03interessantscore<- c(1, 2, 3, 1, 2, 3, 4)
> count<- c(89, 148, 16, 88, 192, 28, 7)
> sumcount<- c(253, 253, 253, 315, 315, 315, 315)
> per<- c('35.2%','58.5%','6.3%','27.9%','61%','8.9%','2.2%')
> LangueTXT<- c('Nl','Nl','Nl','Fr','Fr','Fr','Fr')
>
>
> databar<- data.frame(Langue, n03interessantscore, count, sumcount, per,
> LangueTXT)
>
> barchart(n03interessantscore ~ count| LangueTXT, data=databar,
>           layout=c(1,max(databar$Langue)), stack=TRUE, rectangles=TRUE,
> horizontal=TRUE,
>           ylab="Score",
>           xlab="Count",
>           col="grey",
>           main="3.0z Trouvez-vous ce rapport intéressant?",
>           border="NA",
>           panel= function(y,x,...){panel.grid(h=0, v=-1, col="gray")
>           Y<- tapply(y, y, unique)
>            panel.barchart(x,y,...)
>            panel.text((x-0.1*x), Y, label= paste(round(x,0),'-',
> databar$per), cex=0.9)}
>           )
>

I haven't seen an answer to this yet.
You need subscripts; replace

   panel= function(y,x,...){

with

   panel = function(x, y, subscripts, ...){

and

   databar$per

with

   databar$per[subscripts]


Peter Ehlers



More information about the R-help mailing list