ggplot:每个方面的刻面条形图中的订单条

Mar*_*rio 14 r ggplot2 facets geom-bar

我在R中有一个数据框,我想在一个刻面的ggplot条形图中绘制.

我在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")
Run Code Online (Sandbox Code Playgroud)

这会生成此图表: 在此输入图像描述

您可以看到只有最后一个方面处于所需的降序.我希望所有方面按降序排序,这意味着标签顺序会发生变化.因此我还需要所有方面都有自己的y轴标签.

这是我正在使用的数据:

   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
Run Code Online (Sandbox Code Playgroud)

Her*_*oka 18

因为有时候更容易看到所有代码在运行,所以这里有一个解决方案,可以在一次lapply调用中生成所有绘图.还有其他一些问题需要弄清楚(排序,让颜色正确),我喜欢拼图.

#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)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述