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

时间:2015-11-30 14:33:24

标签: 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")

这会生成此图表: enter image description here

您可以看到只有最后一个方面处于所需的降序。我希望所有方面按降序排序,这意味着标签顺序会发生变化。因此,我还需要所有方面都有自己的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

4 个答案:

答案 0 :(得分:17)

因为有时候更容易看到所有代码在运行,所以这里有一个解决方案,可以在一次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)))

enter image description here

答案 1 :(得分:4)

使用上面的评论我想出了这段代码:

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)

虽然不那么优雅...... 但它给出了这个: generates this plot

所有顺序下降: - )

答案 2 :(得分:2)

最简单的解决方案是使用ggcharts软件包中的bar_chart()函数。请注意,与@Heroka的答案不同,所有子图都有一个公共的x轴。

chart <- ggcharts::bar_chart(
  data_long,
  partei,
  wert,
  fill = kat,
  facet = kat
)
chart

enter image description here

bar_chart()的结果是ggplot类型的对象,因此您可以对其应用任何ggplot2函数。

chart +
  labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
  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(),
    legend.position = "none"
  ) +
  scale_fill_brewer(palette = "Set2")

![enter image description here

答案 3 :(得分:0)

无需其他软件包,您可以通过简单的ggplot实现此目的:

  1. 为每行创建一个附加变量
  2. 使其成为一个因素,重新排列级别
  3. 将图的标签更改为原始值

完整的解决方案:

data_long %>% 
  mutate(kat_partei = paste0(kat, '_', partei),
         kat_partei = forcats::fct_reorder(kat_partei, wert)) %>% 
  ggplot(aes(x = kat_partei, y = wert, fill = kat, width=0.75)) + 
  geom_bar(stat = "identity", show.legend = FALSE) +
  scale_x_discrete(name=NULL, labels=function(x) sub('^.*_(.*)$', '\\1', x)) +
  scale_fill_brewer(palette="Set2") +
  coord_flip() +
  facet_wrap(~kat, scales='free_y') +
  labs(y = "Wähleranteil [ % ]") +
  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())

更多提示:

  • 使用geom_col()代替geom_bar(stat = "identity")
  • 使用show.legend参数代替guides(fill=FALSE)