Reorder geom_bar() not make axactly order in the plot

Hi Community

Im want make a bar plot but when try to order the x axis not work well. I need that the high values appear first (CCA, CDT, TSTF... etc). Im use fct_reorder but no work well. Im try with dplyr but not work. :upside_down_face:

library(tidyverse)
library(forcats)

d2 %>%
  mutate(TITULO = fct_reorder(TITULO, 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))

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"))

Thanks for guidence

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

Hi @arthur.t , is the thing that I need, excellent.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.