ggplot: Order bars in faceted bar chart per facet

Because it's sometimes easier to see all code in action, here's a solution for you that generates all plots inside one call to lapply. There were some other issues to figure out (ordering, getting the colors right) and I like a puzzle.

#create list of plots
myplots <- lapply(split(dat,dat$kat), function(x){
  #relevel factor partei by wert inside this subset
  x$partei <- factor(x$partei, levels=x$partei[order(x$wert,decreasing=F)])

  #make the plot
  p <- ggplot(x, aes(x = partei, y = wert, fill = kat, width=0.75)) +
    geom_bar(stat = "identity") +
    scale_fill_discrete(drop=F)+ #to force all levels to be considered, and thus different colors
    theme_bw()+
    theme(legend.position="none")+
    labs(y="Wähleranteil (%)", x="", title=unique(x$kat))+
    coord_flip()
})

library(gridExtra)

do.call(grid.arrange,(c(myplots, ncol=3)))

enter image description here


using the comments above I came up with this code:

names <- levels(unique(data_long$kat))

plist <- list()
plist[]

for (i in 1:length(names)) {
    d <- subset(data_long,kat == names[i])
    d$partei <- factor(d$partei, levels=d[order(d$wert),]$partei)

    p1 <- ggplot(d, aes(x = partei, y = wert, fill = kat, width=0.75)) + 
    labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
    geom_bar(stat = "identity") +
    facet_wrap(~kat) +
    scale_y_continuous(limits=c(0, 100)) +
    coord_flip() +
    guides(fill=FALSE) +
    theme_bw() + theme( strip.background  = element_blank(),
                        panel.grid.major = element_line(colour = "grey80"),
                        panel.border = element_blank(),
                        axis.ticks = element_line(size = 0),
                        panel.grid.minor.y = element_blank(),
                        panel.grid.major.y = element_blank() ) +
    theme(legend.position="bottom") +
    scale_fill_brewer(palette="Set2")


    plist[[names[i]]] = p1
}   



do.call("grid.arrange", c(plist, ncol=4)

not as elegant though... but it gives this: generates this plot

all nicely ordered descending :-)