Why are my bars in ABC order?
We make a bar chart and the bars line up from A to Z, not from biggest to smallest. Readers have to hunt for the winner. Here is why ggplot2 does this, and three ways to put the bars in the order we want.
What we need
- R 4.1 or newer
- ggplot2
- A small table (we make it below)
Make a table and a chart
Here are made-up votes for a class's favorite fruit. The bars come out in ABC order: apple, banana, grape, mango, orange.
library(ggplot2)
fruit <- data.frame(
fruit = c("mango", "apple", "orange", "banana", "grape"),
votes = c(12, 30, 7, 22, 15)
)
ggplot(fruit, aes(x = fruit, y = votes)) +
geom_col()Loading the checked output…

Why it happens
ggplot2 puts text in ABC order unless it is told otherwise. The order lives in the column's levels, not in the order of the rows. We can see the levels R will use:
levels(factor(fruit$fruit))Loading the checked output…
Fix 1: biggest first
reorder() sorts the bars by another column. The minus sign means biggest first.
ggplot(fruit, aes(x = reorder(fruit, -votes), y = votes)) +
geom_col() +
labs(x = NULL)Loading the checked output…

Fix 2: our own order
Sometimes the order should follow meaning, like days of the week or small to large. Set the levels ourselves: the order we list is the order of the bars.
fruit$fruit <- factor(
fruit$fruit,
levels = c("orange", "mango", "grape", "banana", "apple")
)
ggplot(fruit, aes(x = fruit, y = votes)) +
geom_col()Loading the checked output…

Fix 3: sideways, biggest on top
Long labels read better sideways. Put the names on the y axis, and sort them so the biggest bar sits on top.
ggplot(fruit, aes(x = votes, y = reorder(fruit, votes))) +
geom_col() +
labs(y = NULL)Loading the checked output…

Taste test
Check the order we will get before we draw. The first level is the bar nearest the start of the axis.
levels(reorder(fruit$fruit, -fruit$votes))Loading the checked output…
Kitchen tip: the habit that prevents this
The order of bars comes from the levels of the column on the axis. Sort the levels (with reorder() or factor(levels = ...)), not the rows.
Three common mix-ups
- Sorting the table with arrange() and expecting the bars to follow. They will not; only the levels count.
- A typo in factor(levels = ...): that bar turns into NA and disappears.
- Using reorder(fruit, votes) without the minus sign and getting smallest first.
Quick check
We sort our table so the biggest votes come first, then draw the bars. What order do the bars come in?
Practice: try it here
These are made-up counts of trees on a street. Change the chart p so the bars go from the most trees to the fewest.
Change the code in the box, then press Run to see what it does, and Check my answer when it looks right. It all runs in this browser, so nothing is sent anywhere.
Wrap kind in reorder(), and sort by the count. A minus sign puts the biggest first.
library(ggplot2)
trees <- data.frame(
kind = c("oak", "maple", "pine", "elm"),
count = c(14, 25, 6, 11)
)
p <- ggplot(trees, aes(x = reorder(kind, -count), y = count)) +
geom_col() +
labs(x = NULL)
p- bars follow the levels
- reorder(name, -value): biggest first
- factor(levels = ...): our order
- y = reorder(name, value): sideways
Keep the recipe card
A one-picture summary to pin next to a screen or send to a friend who is stuck on the same thing.
Trying this on other data? Check the row counts and results before and after each step. Package versions change, so if something here works differently, please tell us. Every output on this page comes from running the code above in R; please cross-check before relying on it for our work.