Skip to content

R - Bar Charts

A bar chart uses rectangular bars to represent categorical data, where the length of each bar is proportional to the value it represents. It’s one of the most common and effective ways to compare values across different groups.

As with other visualizations, the ggplot2 package is the modern standard in R. It provides a powerful and declarative system for creating sophisticated bar charts. We will focus on two main types of geoms: geom_bar() and geom_col().

ggplot2 makes an important distinction:

  • geom_col(): Use this when you have a data frame where one column represents the categories (x-axis) and another column represents the bar heights (y-axis). This is the most common scenario for plotting summarized data.
  • geom_bar(): Use this when you want ggplot2 to calculate the counts of each category in a single column and use those counts as the bar heights. It’s essentially a shortcut for creating a histogram for categorical data.

Example: Bar Chart from Summarized Data (geom_col)

Section titled “Example: Bar Chart from Summarized Data (geom_col)”

Let’s create a bar chart showing monthly revenue. Our data is already summarized, so we’ll use geom_col().

# Load the necessary libraries
library(ggplot2)
library(tibble)
# Create a tibble with our data
monthly_revenue <- tibble(
month = c("Mar", "Apr", "May", "Jun", "Jul"),
revenue = c(27000, 32000, 28000, 39000, 41000)
)
# Create the bar chart
revenue_chart <- ggplot(data = monthly_revenue, aes(x = month, y = revenue)) +
geom_col(fill = "#2E86C1", width = 0.7) + # Use geom_col for pre-existing values
labs(
title = "Monthly Revenue Performance",
subtitle = "Q2 2023",
x = "Month",
y = "Revenue (in USD)"
) +
# Reorder months based on their appearance in the data, not alphabetically
scale_x_discrete(limits = monthly_revenue$month) +
# Format y-axis labels to look like currency
scale_y_continuous(labels = scales::dollar_format()) +
theme_minimal()
# Display the plot
print(revenue_chart)
# Save the plot
ggsave("monthly_revenue_chart.png", width = 7, height = 5)

This code produces a professional-looking bar chart. The x-axis correctly orders the months as they appear in our data frame. The y-axis displays revenue formatted as currency (e.g., ‘$30,000’). The bars are blue, and the titles provide clear context.

A key strength of ggplot2 is its ability to create complex plots by mapping variables to aesthetics like fill. This makes creating grouped or stacked bar charts incredibly simple. We just need to ensure our data is in a ‘long’ format.

# Load libraries
library(ggplot2)
library(tibble)
# Create data in a 'long' format, which is ideal for ggplot2
regional_revenue <- tibble(
month = rep(c("Mar", "Apr", 'May'), each = 3),
region = rep(c("East", "West", "North"), times = 3),
revenue = c(2, 9, 3, 11, 9, 4, 8, 7, 3) * 1000
)
# Create a STACKED bar chart (the default)
stacked_chart <- ggplot(regional_revenue, aes(x = month, y = revenue, fill = region)) +
geom_col() + # `fill = region` tells ggplot to color bars by region and stack them
labs(title = "Stacked Revenue by Region", x = "Month", y = "Total Revenue") +
theme_classic()
print(stacked_chart)
# Create a GROUPED (or 'dodged') bar chart
grouped_chart <- ggplot(regional_revenue, aes(x = month, y = revenue, fill = region)) +
# position = "dodge" places bars next to each other
geom_col(position = "dodge") +
labs(title = "Grouped Revenue by Region", x = "Month", y = "Revenue") +
# Use a color-blind friendly palette
scale_fill_brewer(palette = "Set2") +
theme_bw()
print(grouped_chart)

Executing this code produces two plots. The first is a stacked bar chart, where for each month, the revenues from different regions are stacked on top of each other, showing the total. The second is a grouped bar chart, where the bars for each region are placed side-by-side for each month, making direct comparison between regions easier. ggplot2 automatically creates a legend for the region variable.