Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

r - ggplot: Order bars in faceted bar chart per facet

I have a dataframe in R that I want to plot in a faceted ggplot bar chart.

I use this code in ggplot:

ggplot(data_long, 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) +
    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")

This produces this chart: enter image description here

You can see that only the last facet is in the desired descending order. I would like that all the facets are ordered in descending order, meaning that the label order changes. Therefore I also need that all facets have their own y-axis labels.

This is the data I'm using:

data_long = data.frame(
  partei = c("SP", "Grüne", "AL", "BDP", "glp", 
             "CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne", "AL", "BDP", 
             "glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne", "AL", 
             "BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP", "Grüne", 
             "AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU", "SP", 
             "Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP", "EDU", 
             "SP", "Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP", "SVP", 
             "EDU", "SP", "Grüne", "AL", "BDP", "glp", "CVP", "EVP", "FDP", 
             "SVP", "EDU"),
  kat = c("kand1", "kand1", "kand1", "kand1", "kand1", 
          "kand1", "kand1", "kand1", "kand1", "kand1", "kand2", "kand2", 
          "kand2", "kand2", "kand2", "kand2", "kand2", "kand2", "kand2", 
          "kand2", "kand3", "kand3", "kand3", "kand3", "kand3", "kand3", 
          "kand3", "kand3", "kand3", "kand3", "kand4", "kand4", "kand4", 
          "kand4", "kand4", "kand4", "kand4", "kand4", "kand4", "kand4", 
          "kand5", "kand5", "kand5", "kand5", "kand5", "kand5", "kand5", 
          "kand5", "kand5", "kand5", "kand6", "kand6", "kand6", "kand6", 
          "kand6", "kand6", "kand6", "kand6", "kand6", "kand6", "kand7", 
          "kand7", "kand7", "kand7", "kand7", "kand7", "kand7", "kand7", 
          "kand7", "kand7"),
  wert = c(95.41, 80.6, 75.77, 54.02, 47.91, 
           39.01, 36.2, 32.01, 5.71, 1.1, 18.05, 7.15, 9.02, 62.3, 39.18, 
           42.41, 23.14, 94.66, 29.93, 34.97, 0.51, 0.27, 3.92, 9.21, 2.53, 
           2.7, 3.52, 23.19, 92.49, 60.64, 52.98, 81.28, 56.42, 7.52, 13.65, 
           4.06, 9.96, 1.46, 0.94, 0, 7.51, 9.19, 9.94, 25.3, 69.58, 10.59, 
           9.23, 17.61, 3.6, 3.43, 4.29, 2.37, 7.73, 13.14, 11.67, 75.43, 
           19.34, 6.52, 2.43, 6.4, 1.87, 2.98, 5.87, 6.7, 1.29, 2.73, 80.91, 
           1.1, 1.58, 45.47)
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...