R ggplot2 Bar Chart
Bar charts display quantities for different categories. In ggplot2, two functions create bar charts: geom_bar() counts rows automatically, and geom_col() uses pre-computed values you supply. Knowing when to use each saves time and prevents errors.
geom_bar() vs geom_col()
geom_bar() → counts rows in your data automatically (stat="count") geom_col() → uses a value column you provide (stat="identity") Use geom_bar() when: your data has one row per observation Use geom_col() when: your data already has summary totals
geom_bar() — Count Automatically
library(ggplot2)
orders <- data.frame(
region = c("North","South","North","East","South","North","East","South"),
product = c("A","A","B","A","B","A","B","A")
)
# Count orders per region
ggplot(orders, aes(x=region)) +
geom_bar(fill="steelblue", color="white") +
labs(title="Orders by Region", x="Region", y="Number of Orders") +
theme_minimal()
geom_col() — Use Pre-computed Values
sales_summary <- data.frame(
region = c("North","South","East","West"),
revenue = c(45000, 62000, 38000, 51000)
)
ggplot(sales_summary, aes(x=region, y=revenue)) +
geom_col(fill="tomato", color="white", width=0.6) +
labs(title="Revenue by Region", x="Region", y="Revenue (₹)") +
scale_y_continuous(labels=scales::comma) +
theme_minimal()
Horizontal Bar Chart
ggplot(sales_summary, aes(x=revenue, y=reorder(region, revenue))) + geom_col(fill="steelblue") + labs(title="Revenue by Region (Ranked)", x="Revenue (₹)", y="Region") + theme_minimal()
Use reorder(region, revenue) to sort bars by value — always do this for horizontal bar charts so the longest bar is at top.
Grouped Bar Chart
dept_data <- data.frame(
dept = rep(c("HR","IT","Finance"), each=2),
year = rep(c("2023","2024"), 3),
budget = c(120,140, 300,350, 200,220)
)
ggplot(dept_data, aes(x=dept, y=budget, fill=year)) +
geom_col(position="dodge", color="white") + # "dodge" = side by side
scale_fill_manual(values=c("2023"="steelblue","2024"="tomato")) +
labs(title="Department Budget 2023 vs 2024",
x="Department", y="Budget (₹ thousands)", fill="Year") +
theme_minimal()
Stacked Bar Chart
ggplot(dept_data, aes(x=year, y=budget, fill=dept)) + geom_col(position="stack") + # "stack" = stacked labs(title="Total Budget by Year", x="Year", y="Budget", fill="Department") + theme_minimal() # 100% stacked ggplot(dept_data, aes(x=year, y=budget, fill=dept)) + geom_col(position="fill") + # "fill" = proportional (0 to 1) scale_y_continuous(labels=scales::percent) + labs(title="Budget Share by Year") + theme_minimal()
Adding Value Labels
ggplot(sales_summary, aes(x=region, y=revenue)) +
geom_col(fill="steelblue") +
geom_text(aes(label=paste0("₹",revenue/1000,"K")),
vjust=-0.5, size=3.5, fontface="bold") +
labs(title="Revenue by Region") +
ylim(0, 70000) +
theme_minimal()
Flipping Coordinates
ggplot(sales_summary, aes(x=reorder(region,-revenue), y=revenue)) + geom_col(fill="steelblue") + coord_flip() + # swap x and y axes labs(title="Revenue by Region", x=NULL, y="Revenue") + theme_minimal()
Color by Category
ggplot(sales_summary, aes(x=region, y=revenue, fill=region)) + geom_col(show.legend=FALSE) + scale_fill_brewer(palette="Set2") + # ColorBrewer palette labs(title="Revenue by Region") + theme_minimal()
Bar charts are the most common chart type in business reporting. Use vertical bars for few categories, horizontal bars for many or long names, grouped bars for comparisons across two dimensions, and stacked bars for part-to-whole relationships. Always sort bars when the order is not inherently meaningful.
