Reorder to have largest to smallest geom_bars() - reorder()

Hello,
I'm attempting to create a nice bar plot which the columns are arranged from the largest to smallest.
I thought reorder() function on the x aesthetics would do the trick... I was wrong.

Please find my reprex bellow, any insight is welcome.

df <- tibble::tribble(
  ~case_id,    ~collision_severity, ~weather_1, ~weather_2,                    ~lighting, ~road_surface, ~road_condition_1, ~road_condition_2, ~statewide_vehicle_type_at_fault, ~killed_victims, ~pedestrian_killed_count, ~bicyclist_killed_count, ~motorcyclist_killed_count, ~injured_victims, ~pedestrian_injured_count, ~bicyclist_injured_count, ~motorcyclist_injured_count, ~collision_date, ~collision_time, ~latitude, ~longitude,
  "90967864",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-04-04",      "16:40:00",  34.00895, -118.41632,
  "8943937",         "other injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-12",      "19:30:00",        NA,         NA,
  "90955953",                 "pain",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-03-18",      "06:10:00",  34.01322, -118.48904,
  "90951291",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-03-12",      "18:10:00",  34.03114, -118.43421,
  "91034615",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-07-10",      "15:10:00",  34.03158, -118.18798,
  "91023126",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-20",      "18:50:00",  34.15891, -118.63798,
  "8947061",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-05",      "17:10:00",   34.0012,  -117.9138,
  "91052595",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-08",      "13:00:00",  34.20753, -117.86526,
  "8982400",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-10-29",      "13:10:00",        NA,         NA,
  "8915111",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-07-16",      "10:30:00",        NA,         NA,
  "8854771",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "pickup or panel truck",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-04-17",      "07:50:00",        NA,         NA,
  "91020607",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-18",      "18:00:00",  33.92823, -118.10314,
  "91113406",         "other injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-10-23",      "05:35:00",  34.31018, -118.48873,
  "8909266",         "other injury",   "cloudy",         NA,                   "daylight",         "dry",          "normal",                NA,          "pickup or panel truck",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-26",      "17:25:00",        NA,         NA,
  "8885748", "property damage only",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-06-07",      "11:50:00",        NA,         NA,
  "91002354",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-05-24",      "17:58:00",  34.03628, -118.02464,
  "90974571",        "severe injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               3L,                        0L,                       0L,                          1L,    "2019-04-20",      "02:43:00",  34.34095, -118.52141,
  "8979326", "property damage only",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-08-05",      "22:55:00",        NA,         NA,
  "91001741",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-25",      "15:10:00",   34.0362,  -118.3222,
  "8808299",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          2L,    "2019-06-23",      "14:25:00",        NA,         NA,
  "91065692",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-23",      "06:15:00",  33.87671, -118.09548,
  "9013814", "property damage only",    "clear",         NA,               "dusk or dawn",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-12-18",      "17:00:00",        NA,         NA,
  "8936866",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-27",      "19:15:00",        NA,         NA,
  "9020373",                 "pain",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-11-04",      "09:50:00",        NA,         NA,
  "90989526",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-03",      "09:25:00",  33.97418, -118.12426,
  "9100514",         "other injury",    "clear",         NA, "dark with no street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               6L,                        0L,                       0L,                          5L,    "2019-12-21",      "17:02:00",  33.88993, -118.30477,
  "9013220",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-12-17",      "10:10:00",        NA,         NA,
  "8959291", "property damage only",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-09-19",      "07:15:00",  34.01001, -118.12108,
  "9034445",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-15",      "08:30:00",        NA,         NA,
  "8893570",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-29",      "11:40:00",        NA,         NA
)

# This is the working bar plot:
df %>% group_by(collision_severity) %>% ggplot(aes(x = collision_severity, fill = collision_severity)) + geom_bar(alpha = 0.85, show.legend = FALSE) +     
  labs(title = "2019, Motorcycle Accident Collisions by Severity in LA",
       y = "Count",
       x = NULL)+ theme_minimal() +  
  theme(plot.title = element_text(size = 8),
        axis.text.x = element_text(size = 8)
  ) +
  scale_fill_viridis(end = 0.85, discrete=TRUE)  +
  scale_x_discrete(guide = guide_axis(n.dodge=2))
#> Error in df %>% group_by(collision_severity) %>% ggplot(aes(x = collision_severity, : could not find function "%>%"


# My attempt to have it reorder from largest to smallest, I thought the "Reorder" function was the one to use here.
df %>% group_by(collision_severity) %>% ggplot(aes(x = reorder(collision_severity), fill = collision_severity)) + geom_bar(alpha = 0.85, show.legend = FALSE) +     
  labs(title = "2019, Motorcycle Accident Collisions by Severity in LA",
       y = "Count",
       x = NULL)+ theme_minimal() +  
  theme(plot.title = element_text(size = 8),
        axis.text.x = element_text(size = 8)
  ) +
  scale_fill_viridis(end = 0.85, discrete=TRUE)  +
  scale_x_discrete(guide = guide_axis(n.dodge=2))
#> Error in df %>% group_by(collision_severity) %>% ggplot(aes(x = reorder(collision_severity), : could not find function "%>%"

Created on 2021-11-22 by the reprex package (v2.0.1)

Thanks.
TB.

Hi @technobrat,
One way to achieve this is to calculate and then plot from the frequency "table":

df %>% 
  group_by(collision_severity) %>%
  summarise(freq = n()) %>% 
  ggplot(aes(x = reorder(collision_severity, -freq), 
             y = freq,
             fill = collision_severity)) + 
  geom_col(alpha = 0.85, show.legend = FALSE) +     
  labs(title = "2019, Motorcycle Accident Collisions by Severity in LA",
       y = "Count",
       x = NULL) + 
  theme_minimal() +  
  theme(plot.title = element_text(size = 8),
        axis.text.x = element_text(size = 8)) +
  scale_fill_viridis(end = 0.85, discrete=TRUE)  +
  scale_x_discrete(guide = guide_axis(n.dodge=2))
1 Like

Another option is to use factor levels to change the order

library(tidyverse)
library(viridis)
#> Loading required package: viridisLite

df <- tibble::tribble(
    ~case_id,    ~collision_severity, ~weather_1, ~weather_2,                    ~lighting, ~road_surface, ~road_condition_1, ~road_condition_2, ~statewide_vehicle_type_at_fault, ~killed_victims, ~pedestrian_killed_count, ~bicyclist_killed_count, ~motorcyclist_killed_count, ~injured_victims, ~pedestrian_injured_count, ~bicyclist_injured_count, ~motorcyclist_injured_count, ~collision_date, ~collision_time, ~latitude, ~longitude,
    "90967864",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-04-04",      "16:40:00",  34.00895, -118.41632,
    "8943937",         "other injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-12",      "19:30:00",        NA,         NA,
    "90955953",                 "pain",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-03-18",      "06:10:00",  34.01322, -118.48904,
    "90951291",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-03-12",      "18:10:00",  34.03114, -118.43421,
    "91034615",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-07-10",      "15:10:00",  34.03158, -118.18798,
    "91023126",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-20",      "18:50:00",  34.15891, -118.63798,
    "8947061",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-05",      "17:10:00",   34.0012,  -117.9138,
    "91052595",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-08",      "13:00:00",  34.20753, -117.86526,
    "8982400",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-10-29",      "13:10:00",        NA,         NA,
    "8915111",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-07-16",      "10:30:00",        NA,         NA,
    "8854771",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "pickup or panel truck",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-04-17",      "07:50:00",        NA,         NA,
    "91020607",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-18",      "18:00:00",  33.92823, -118.10314,
    "91113406",         "other injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-10-23",      "05:35:00",  34.31018, -118.48873,
    "8909266",         "other injury",   "cloudy",         NA,                   "daylight",         "dry",          "normal",                NA,          "pickup or panel truck",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-26",      "17:25:00",        NA,         NA,
    "8885748", "property damage only",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-06-07",      "11:50:00",        NA,         NA,
    "91002354",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-05-24",      "17:58:00",  34.03628, -118.02464,
    "90974571",        "severe injury",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               3L,                        0L,                       0L,                          1L,    "2019-04-20",      "02:43:00",  34.34095, -118.52141,
    "8979326", "property damage only",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                               NA,              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-08-05",      "22:55:00",        NA,         NA,
    "91001741",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-25",      "15:10:00",   34.0362,  -118.3222,
    "8808299",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          2L,    "2019-06-23",      "14:25:00",        NA,         NA,
    "91065692",         "other injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-23",      "06:15:00",  33.87671, -118.09548,
    "9013814", "property damage only",    "clear",         NA,               "dusk or dawn",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-12-18",      "17:00:00",        NA,         NA,
    "8936866",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-08-27",      "19:15:00",        NA,         NA,
    "9020373",                 "pain",    "clear",         NA,    "dark with street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               2L,                        0L,                       0L,                          1L,    "2019-11-04",      "09:50:00",        NA,         NA,
    "90989526",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-05-03",      "09:25:00",  33.97418, -118.12426,
    "9100514",         "other injury",    "clear",         NA, "dark with no street lights",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               6L,                        0L,                       0L,                          5L,    "2019-12-21",      "17:02:00",  33.88993, -118.30477,
    "9013220",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-12-17",      "10:10:00",        NA,         NA,
    "8959291", "property damage only",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,          "motorcycle or scooter",              0L,                       0L,                      0L,                         0L,               0L,                        0L,                       0L,                          0L,    "2019-09-19",      "07:15:00",  34.01001, -118.12108,
    "9034445",                 "pain",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-09-15",      "08:30:00",        NA,         NA,
    "8893570",        "severe injury",    "clear",         NA,                   "daylight",         "dry",          "normal",                NA,                  "passenger car",              0L,                       0L,                      0L,                         0L,               1L,                        0L,                       0L,                          1L,    "2019-06-29",      "11:40:00",        NA,         NA
)

df %>% 
    group_by(collision_severity) %>% 
    ggplot(aes(x = forcats::fct_infreq(collision_severity), fill = collision_severity)) +
    geom_bar(alpha = 0.85, show.legend = FALSE) +     
    labs(title = "2019, Motorcycle Accident Collisions by Severity in LA",
         y = "Count",
         x = NULL)+ theme_minimal() +  
    theme(plot.title = element_text(size = 8),
          axis.text.x = element_text(size = 8)) +
    scale_fill_viridis(end = 0.85, discrete=TRUE)  +
    scale_x_discrete(guide = guide_axis(n.dodge=2))

Created on 2021-11-22 by the reprex package (v2.0.1)

2 Likes

@DavoWW Thank you for your reply!

Awesome!!!

I tried using scales::comma_format() and formattable::comma() for the Y count axis to get the thousand separators but no cigar. Any ideas?