Skip to Content

adding labels on bottom of barchart

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:

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)
        } 
)