ggplot: Order bars in faceted bar chart per facet

2019-01-14 23:02发布

问题:

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:

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:

   partei   kat  wert
1      SP kand1 95.41
2   Grüne kand1 80.60
3      AL kand1 75.77
4     BDP kand1 54.02
5     glp kand1 47.91
6     CVP kand1 39.01
7     EVP kand1 36.20
8     FDP kand1 32.01
9     SVP kand1  5.71
10    EDU kand1  1.10
11     SP kand2 18.05
12  Grüne kand2  7.15
13     AL kand2  9.02
14    BDP kand2 62.30
15    glp kand2 39.18
16    CVP kand2 42.41
17    EVP kand2 23.14
18    FDP kand2 94.66
19    SVP kand2 29.93
20    EDU kand2 34.97
21     SP kand3  0.51
22  Grüne kand3  0.27
23     AL kand3  3.92
24    BDP kand3  9.21
25    glp kand3  2.53
26    CVP kand3  2.70
27    EVP kand3  3.52
28    FDP kand3 23.19
29    SVP kand3 92.49
30    EDU kand3 60.64
31     SP kand4 52.98
32  Grüne kand4 81.28
33     AL kand4 56.42
34    BDP kand4  7.52
35    glp kand4 13.65
36    CVP kand4  4.06
37    EVP kand4  9.96
38    FDP kand4  1.46
39    SVP kand4  0.94
40    EDU kand4  0.00
41     SP kand5  7.51
42  Grüne kand5  9.19
43     AL kand5  9.94
44    BDP kand5 25.30
45    glp kand5 69.58
46    CVP kand5 10.59
47    EVP kand5  9.23
48    FDP kand5 17.61
49    SVP kand5  3.60
50    EDU kand5  3.43
51     SP kand6  4.29
52  Grüne kand6  2.37
53     AL kand6  7.73
54    BDP kand6 13.14
55    glp kand6 11.67
56    CVP kand6 75.43
57    EVP kand6 19.34
58    FDP kand6  6.52
59    SVP kand6  2.43
60    EDU kand6  6.40
61     SP kand7  1.87
62  Grüne kand7  2.98
63     AL kand7  5.87
64    BDP kand7  6.70
65    glp kand7  1.29
66    CVP kand7  2.73
67    EVP kand7 80.91
68    FDP kand7  1.10
69    SVP kand7  1.58
70    EDU kand7 45.47

回答1:

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



回答2:

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:

all nicely ordered descending :-)