views:

191

answers:

1

Hi, I have two questions concerning customizing a barchart in lattice. I tried to add labels to the barchart respectively edit the scale, but my code doesn't work. What I'm doing wrong? There should be some values including "%".

The second question is how lines can be drawn between the top and the bottom of the plotting area behind the bars and located at the printed scale values. Thank you for any suggestions and help, Sebastian.

Here is the code:

library(lattice)
library(plyr)
data(postdoc, package="latticeExtra")
colnames(postdoc) <- c("Legendtext 1", "2", "3", "4", "5")
colors <- c(rgb(166,27,30,maxColorValue = 255),                             
        rgb(192,80,77,maxColorValue = 255),
        rgb(24,65,83,maxColorValue = 255),
        rgb(60,143,167,maxColorValue = 255),
        rgb(130,184,208,maxColorValue = 255))
colorset <- simpleTheme(col=colors,                                          
                    border="white")
data.p <- prop.table(postdoc, margin=1)
data <- data.p * 100
pl <- barchart(data,
           par.settings = colorset,                                      
           box.ratio = 1,                                                
           xlab = " ",                                                   
           panel = function(...) {   # Code for panel function by rcs (http://stackoverflow.com/questions/3220702/display-values-in-stacked-lattice-barchart-r) - thank you very much!
             panel.barchart(...) 
             tmp <- list(...)
             tmp <- data.frame(x=tmp$x, y=tmp$y)
             # calculate positions of text labels                        
             df <- ddply(tmp, .(y),
                         function(x) {
                           data.frame(x, pos=cumsum(x$x)-x$x/2)
                         })
             panel.text(x=df$pos, y=df$y,                                
                        label=sprintf("%.0f", df$x),                     
                        cex=0.7, col="white")
             panel.axis(side = c("bottom"),
                        at = list(0, 20, 50, 75, 100),
                        labels = list("0%", "25%", "50%", "75%", "100%"),
                        ticks = TRUE)       
           },
           auto.key=list(columns=5, space="bottom",                          
                         cex=0.8, size=1.4, adj=1,
                         between=0.2, between.colums=0.1, 
                         size = 1.3, points = FALSE, rectangles = TRUE))
+2  A: 

Question 1: The following works for me (half=FALSE)

panel.axis(side=c("bottom"),
           at=c(0, 25, 50, 75, 100),
           labels=c("0%", "25%", "50%", "75%", "100%"),
           rot=0, half=FALSE, ticks=TRUE)

Question 2: Add a call to panel.grid (or panel.abline) before panel.barchart() in your panel function:

panel.grid(h=FALSE, v=-1)
rcs
Thank you so much!! I used panel.abline because it includes more possibilities to edit graphical parameters. Instead of panel.axis it was finally better for me to use the scales argument, because the labels should be outside the plotting area and I had some trouble with clip and viewport. Your help was very important for me to finish the graph and to undersand more about lattice. Many thanks again!
SebastianW