Reorder geom_bar() not make axactly order in the plot

You have more than one row per value of TITULO. The ggplot will sum the VALOR within values of TITULO but the reorder isn't going to be so smart. You can group_by, summarize with sum to get the appropriate input to reorder().

d2<-structure(list(TITULO = c("ABENVS", "ABENVS", "ABENVS", "ABENVS", 
                              "ABENVS", "ABENVS", "ABENVS", "ABEVS", "ABEVS", "ABEVS", "ABEVS", 
                              "ABEVS", "ABEVS", "ABEVS", "ABEVS", "ABEVS", "ANLB", "BGEVS", 
                              "BGEVS", "BOENVS", "BOENVS", "BOENVS", "BOENVS", "BOENVS", "BOENVS", 
                              "BOENVS", "BOENVS", "BOEOM", "BOEVS", "BOEVS", "BOEVS", "BOEVS", 
                              "BOEVS", "BOEVS", "BOEVS", "BOEVS", "BOEVS", "BOEVS", "BPAZ", 
                              "BPEN", "BPEN", "CCA", "CCA", "CCA", "CCA", "CCA", "CCA", "CCA", 
                              "CCA", "CCA", "CCA", "CCA", "CCA", "CCA", "CCACP", "CCACP", "CCATP", 
                              "CCATP", "CCATP", "CCATP", "CCC", "CCETP", "CDT", "CDT", "CDT", 
                              "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", 
                              "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", 
                              "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", "CDT", 
                              "CDT", "CDT", "CDT", "CDT", "DPATAUT", "DPATAUT", "OTDP", "OTPENVS", 
                              "OTPENVS", "OTPENVS", "OTPEVS", "PCCBCTP", "TCCEVS", "TCCH", 
                              "TDERECON", "TDERECON", "TDERECON", "TDERECON", "TDERECON", "TDERECON", 
                              "TDERECON", "TDERECON", "TDERECON", "TDERECON", "TDPE", "TDPIT", 
                              "TDPIT", "TDPIT", "TIDIS", "TRD", "TSTF", "TSUV"), 
                   VALOR = c(0, 
                             417.121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                             0, 15508.706, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10420.323, 0, 
                             0, 247.521, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5086.858, 0, 
                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3134.988, 
                             0, 0, 5587.902, 0, 0, 0, 0, 0, 0, 4732.015, 0, 0, 0, 0, 0, 0, 
                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12603.703, 0)), 
              row.names = c(NA, -124L), class = c("tbl_df", "tbl", "data.frame"))

library(tidyverse)

d2 %>%
  group_by(TITULO) %>%
  summarize(VALOR = sum(VALOR)) %>%
  ggplot() +
  aes(x=reorder(TITULO, -VALOR) , y = VALOR, fill = TITULO) +
  geom_bar(stat="identity") +
  ggtitle("TITULOS EMISOR - BBVA ") +
  xlab("TÍTULO") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5,
                                   hjust=1))

Created on 2022-03-03 by the reprex package (v2.0.1)

1 Like