0

I'm quite new to lattice so for me its a trial and error run here.

It seems to customize lattice graphics cannot be done with the typical r-understanding.

I would like to add something like count(percentage) beneath each bar which is supposed to look like this when its done:

labelled bar chart

As you can see my code does not label the bars right. Not all of them and wrong. The first is supposed to be 20(0.1%).

Here my code:

grp1<-rep("grp1",20)
grp2<-rep("grp2",40)
grp3<-rep("grp3",60)
grp4<-rep("grp4",80)
grp <- c(grp1,grp2,grp3,grp4)

barchart(grp,horizontal=F, 
    par.settings = list(
    plot.polygon = list(col = c("#1E4150","#28556E","#32698C","#3C7DAA")) ),
        panel = function(x, y, subscripts, ...){  
            X <- table(x)
            panel.barchart(x,y,...) 
            percentages <- paste(round(table(X)/length(X),2),"%",sep="")
            lab1 <- paste( "(",percentages, sep="")
            lab1 <- paste( lab1,")", sep="")
            abs <- table(X)
            panel.text(1,-3, label = paste(abs,lab1), cex=0.8)
        } 
) 
daedalus
  • 10,873
  • 5
  • 50
  • 71
Druss2k
  • 275
  • 2
  • 5
  • 15

1 Answers1

4

I think maybe you wanted to do something more like this:

barchart(grp,horizontal=F, 
    par.settings = list(
    plot.polygon = list(col = c("#1E4150","#28556E","#32698C","#3C7DAA")) ),
        panel = function(x, y, subscripts, ...){  
            panel.barchart(x,y,...) 
            percentages <- paste(round(y/sum(y),2),"%",sep="")
            lab1 <- paste( "(",percentages,")", sep="")
            panel.text(x,-3, labels = paste(y,lab1), cex=0.8)
        } 
) 

enter image description here

joran
  • 169,992
  • 32
  • 429
  • 468
  • thk u very much! this was exaktly what ive been looking for! but if you dont mind i got a question. what does exactly function(x,y,subscripts) do? sure it hands over some values but what values exactly? – Druss2k May 30 '12 at 18:37