How to create a bar chart with ggplot2 in R

· 1 min read · Updated March 14, 2026 · beginner
r ggplot2 visualization bar-chart

Bar charts are one of the most common visualizations. ggplot2 makes them flexible and consistent.

geom_bar() vs geom_col()

Use geom_bar() to count observations automatically:

library(ggplot2)

ggplot(mpg, aes(x = class)) +
  geom_bar()

Use geom_col() when you already have the values:

cars <- data.frame(
  model = c("Sedan", "SUV", "Truck", "Coupe"),
  sales = c(120, 95, 60, 45)
)

ggplot(cars, aes(x = model, y = sales)) +
  geom_col()

Common Customizations

Add color with fill:

ggplot(cars, aes(x = model, y = sales, fill = model)) +
  geom_col()

Flip to horizontal bars with coord_flip():

ggplot(cars, aes(x = model, y = sales)) +
  geom_col() +
  coord_flip()

Stack bars with fill:

ggplot(mpg, aes(x = class, fill = drv)) +
  geom_bar()

Group bars side-by-side with position = "dodge":

ggplot(mpg, aes(x = class, fill = drv)) +
  geom_bar(position = "dodge")

See Also