Paulo Kessler
Paulo Kessler

Reputation: 31

How to order a geom_col columns based on a summarised value in a pipe

I have tryed to understand the other results, but I could not. This is my dataset:

 > HIST
# A tibble: 1,071 x 16
     Ano        Leilao Fonte    UF     Vend                         Projeto
   <dbl>         <chr> <chr> <chr>    <chr>                           <chr>
 1  2008 2008 Leilao 1   Bio    SP  Abengoa UTE São Luiz (Abengoa São Luiz)
 2  2013    2013 A-5 1   Bio    MS AMANDINA                        Amandina
 3  2017      2017 A-6   Bio    MG    BEVAP         BIOENERGETICA AROEIRA 2
 4  2015    2015 A-5 1   Bio    BA     Bolt                         BOLTBAH
 5  2013    2013 A-5 1   Bio    BA     Bolt                    CAMPO GRANDE
 6  2013    2013 A-5 1   Bio    PI     Bolt                 CANTO DO BURITI
 7  2010      2010 LER   Bio    TO    Bunge                    PEDRO AFONSO
 8  2015      2015 LFA   Bio    SP  Clealco                 CLEALCO QUEIROZ
 9  2015      2015 A-3   Bio    SP  Clealco                 CLEALCO QUEIROZ
10  2008 2008 Leilao 1   Bio    MG     CMAA              UTE Vale do Tijuco
# ... with 1,061 more rows, and 10 more variables: CODPPA <dttm>, CAPEX <dbl>,
#   MW <dbl>, GF <dbl>, FC <dbl>, PPA <dbl>, RMW <dbl>, WACC <dbl>, TIR <dbl>,
#   VPL <dbl>

`

I want to make a graph sorted by the sum(MW), like this:

HIST %>%
  group_by(Fonte, UF)%>%
  summarise(SUMMW = sum(MW))%>%
  arrange(desc(SUMMW))%>%

  ggplot(aes(x = UF, y = SUMMW, fill = Fonte))+

  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_col()

But the problem is that I get the following chart, not ordered by the sum of MW. I would like this graph`s columns to be ordered by the height of the columns:

plot

thank you, Paulo

Upvotes: 2

Views: 4911

Answers (2)

giovannotti
giovannotti

Reputation: 138

I think the easiest way is to reorder your variable SUMMW in the aestetics function aes with reorder(UF, desc(SUMMW)):

HIST %>%
  group_by(Fonte, UF)%>%
  summarise(SUMMW = sum(MW))%>%
  arrange(desc(SUMMW))%>%

  ggplot(aes(x = reorder(UF, desc(SUMMW)), y = SUMMW, fill = Fonte))+

  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_col()

Upvotes: 7

Z.Lin
Z.Lin

Reputation: 29125

You can get calculate the height of each bar first & assign UF the appropriate order as a factor. Otherwise ggplot will plot UF's values in categorical order on the x-axis.

# create summary data frame from HIST
df <- HIST %>%
  group_by(Fonte, UF) %>%
  summarise(SUMMW = sum(MW))

# calculate total bar height for each UF value, & sort accordingly.
df2 <- df %>%
  group_by(UF) %>%
  summarise(bar.heights = sum(SUMMW)) %>%
  ungroup() %>%
  arrange(desc(bar.heights))

# convert UF in the summary data frame to factor, with levels in the sorted order
df$UF <- factor(df$UF, levels = df2$UF)
rm(df2) # you can remove df2 after this; it's not needed anymore

# plot
ggplot(df, 
       aes(x = UF, y = SUMMW, fill = Fonte))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  geom_col()

Upvotes: 2

Related Questions