0

I have data that I would like to show in a faceted bar chart. I came up with this:

Bar chart with ggplot

However, the facets should be ordered by wert2 from the katcolumn where the region is de. This means, the "SVP"-facet should be at the very left, then "SP" and so on.

How can I achieve this ordering? The order within the facets should always be "de", "fr", "it".

And a second question is: How can I manually set the color for each facet based on the facet title? It should be the same hue within one facet and the wert1 column should be a bit brighter.

The ggplot code:

ggplot(data_long, aes(x=region, y=wert)) + 
geom_bar(aes(fill = kat), position = "dodge", width=.5, stat="identity") +
labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
facet_grid(. ~ type) +
theme_bw() + theme( strip.background  = element_blank(),
                    panel.grid.major = element_line(colour = "grey80"),
                    panel.border = element_blank(),
                    axis.ticks = element_blank(),
                    panel.grid.minor.x=element_blank(),
                    panel.grid.major.x=element_blank() ) +
theme(legend.position="bottom") +
guides(fill=guide_legend(nrow=1,byrow=TRUE))

The data:

    type region     label   kat wert
1     SVP     de    SVP de wert1 29.3
2     SVP     fr    SVP fr wert1 20.2
3     SVP     it    SVP it wert1 10.3
4   Grüne     de  Grüne de wert1  8.1
5   Grüne     fr  Grüne fr wert1 10.2
6   Grüne     it  Grüne it wert1  6.6
7     FDP     de    FDP de wert1 13.1
8     FDP     fr    FDP fr wert1 20.2
9     FDP     it    FDP it wert1 24.3
10     SP     de     SP de wert1 17.6
11     SP     fr     SP fr wert1 23.2
12     SP     it     SP it wert1 16.8
13    EVP     de    EVP de wert1  2.6
14    EVP     fr    EVP fr wert1  1.5
15    EVP     it    EVP it wert1  0.0
16    glp     de    glp de wert1  6.4
17    glp     fr    glp fr wert1  2.8
18    glp     it    glp it wert1  0.1
19 andere     de andere de wert1  2.4
20 andere     fr andere fr wert1  1.3
21 andere     it andere it wert1  3.3
22    CVP     de    CVP de wert1 11.5
23    CVP     fr    CVP fr wert1 13.4
24    CVP     it    CVP it wert1 20.3
25    BDP     de    BDP de wert1  7.0
26    BDP     fr    BDP fr wert1  0.8
27    BDP     it    BDP it wert1  0.3
28    SVP     de    SVP de wert2 32.9
29    SVP     fr    SVP fr wert2 21.0
30    SVP     it    SVP it wert2 12.1
31  Grüne     de  Grüne de wert2  6.7
32  Grüne     fr  Grüne fr wert2  9.1
33  Grüne     it  Grüne it wert2  3.4
34    FDP     de    FDP de wert2 14.3
35    FDP     fr    FDP fr wert2 22.1
36    FDP     it    FDP it wert2 23.3
37     SP     de     SP de wert2 18.4
38     SP     fr     SP fr wert2 21.1
39     SP     it     SP it wert2 16.1
40    EVP     de    EVP de wert2  2.4
41    EVP     fr    EVP fr wert2  1.5
42    EVP     it    EVP it wert2  0.0
43    glp     de    glp de wert2  5.5
44    glp     fr    glp fr wert2  2.4
45    glp     it    glp it wert2  0.9
46 andere     de andere de wert2  2.7
47 andere     fr andere fr wert2  1.3
48 andere     it andere it wert2  2.3
49    CVP     de    CVP de wert2 10.2
50    CVP     fr    CVP fr wert2 14.9
51    CVP     it    CVP it wert2 20.1
52    BDP     de    BDP de wert2  5.2
53    BDP     fr    BDP fr wert2  1.1
54    BDP     it    BDP it wert2  0.2
Mario
  • 1,789
  • 11
  • 28
  • Here is how you order your factors http://www.cookbook-r.com/Manipulating_data/Changing_the_order_of_levels_of_a_factor/ – nist Dec 02 '15 at 10:47
  • ok, but then I have to order them manually - is there no way to order them automatically by the values of a certain column? – Mario Dec 02 '15 at 10:53
  • @Mario you can do that, this might help http://stackoverflow.com/questions/10758243/r-order-a-factor-based-on-value-in-one-or-more-other-columns – Heroka Dec 02 '15 at 11:44

1 Answers1

0

Ok, using the information from nist I created this code:

partysubset <- data_long[data_long$kat == "wert2" & data_long$region == "de",c("type","kat","wert")]
partysubset$type <- factor(partysubset$type, levels=partysubset[order(-partysubset$wert),]$type)
data_long$type <- factor(data_long$type, levels = c(levels(partysubset$type)))

It takes the rows where the values to order are, orders them and inserts them as levels in the data.frame

ordered facets

Mario
  • 1,789
  • 11
  • 28