Skip to content

Instantly share code, notes, and snippets.

@jknowles
Created February 20, 2020 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jknowles/140d5c93849b176d95953ff2dff713ea to your computer and use it in GitHub Desktop.
Save jknowles/140d5c93849b176d95953ff2dff713ea to your computer and use it in GitHub Desktop.
How to order bar charts in ggplot2
# Load ggplot2
library(ggplot2)
# Load example data
data(mtcars)
# Create a character vector of car names
mtcars$name <- row.names(mtcars)
# Plot car names by mpg
ggplot(mtcars, aes(x = name, y = mpg)) +
geom_bar(stat = "identity") +
theme_bw()
# If we want we can sort X by another data element in our data
# Here we sort cars by mpg
# Reorder cars in order of mpg
ggplot(mtcars, aes(x = reorder(name, mpg), y = mpg)) +
geom_bar(stat = "identity") +
theme_bw()
# Flip coordinates
ggplot(mtcars, aes(x = reorder(name, mpg), y = mpg)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
# If we make car name a factor we get the same result:
mtcars$name_factor <- as.factor(mtcars$name)
# Flip coordinates
ggplot(mtcars, aes(x = reorder(name_factor, mpg), y = mpg)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
# If we want to sort by a specific order that is not ordered by
# another data element, we can specify the order in the factor
# creation by assigning specific levels
# What if we want an arbitrary order:
ggplot(mtcars, aes(x = name_factor, y = mpg)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
# Factor is ordered by levels in the data, which with characters
# turned into factors defaults to alphabetical
# Dput out the order based on first sorting the data by gear
mtcars <- mtcars[order(mtcars$gear),]
# This magic command gives you a way to copy and paste data from the console ->
# into your R script to fix data elements and hard code them in
dput(unique(mtcars$name))
my_order <- c("Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360",
"Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood",
"Lincoln Continental", "Chrysler Imperial", "Toyota Corona",
"Dodge Challenger", "AMC Javelin", "Camaro Z28", "Pontiac Firebird",
"Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Merc 240D", "Merc 230",
"Merc 280", "Merc 280C", "Fiat 128", "Honda Civic", "Toyota Corolla",
"Fiat X1-9", "Volvo 142E", "Porsche 914-2", "Lotus Europa", "Ford Pantera L",
"Ferrari Dino", "Maserati Bora")
mtcars$name_factor <- factor(mtcars$name_factor, levels = my_order)
ggplot(mtcars, aes(x = name_factor, y = mpg)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
# Reverse order and sorting outside of ggplot2 when working with factors
# this avoids labels and data from being separated and mislabeling occurring
mtcars$name_factor <- factor(mtcars$name_factor, levels = rev(my_order))
ggplot(mtcars, aes(x = name_factor, y = mpg)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment