Mastering Bar Charts in ggplot2

A Systematic Guide to Categorical Data Visualization

Author

Abdullah Al Shamim

Published

February 9, 2026

Introduction

Bar charts are essential for comparing the frequency or count of categorical variables. In this tutorial, we will use the starwars dataset to build a professional bar chart layer by layer.


1. Environment & Typography Setup

First, we load the necessary libraries and configure the Poppins Google font to give our plots a modern, professional look.

Code
library(tidyverse)
library(showtext)
library(sysfonts)

# Add the Google font "Poppins"
font_add_google("Poppins", "poppins")
showtext_auto()

# Set Poppins as the default font for all plots
theme_set(theme_test(base_size = 15) + 
            theme(text = element_text(family = "poppins")))

2. Basic Bar Chart

We begin by mapping the sex variable to the X-axis. geom_bar() will automatically count the observations for each category.

Code
starwars %>% 
  ggplot(aes(x = sex)) +
  geom_bar()


3. Aesthetic Refinement

To make the plot visually appealing, we add color and adjust the transparency.

Code
starwars %>% 
  ggplot(aes(x = sex)) +
  geom_bar(fill = "purple", alpha = 0.6) +
  theme_test() +
  labs(title = "Distribution of Sex in Starwars",
       subtitle = "Basic aesthetic customization",
       x = "Sex",
       y = "Count")


4. Systemic Data Cleaning

Missing values (NAs) can clutter a graph. We use drop_na() to target specific columns for cleaning.

Code
starwars %>% 
  # Precise cleaning: only remove rows where sex is missing
  drop_na(sex) %>% 
  ggplot(aes(x = sex)) +
  geom_bar(fill = "purple", alpha = 0.6) +
  theme_test() +
  labs(title = "Cleaned Bar Chart",
       subtitle = "Removing NA values from the sex variable",
       x = "Sex",
       y = "Count")


5. Improving Readability (Flip & Sort)

Coordinate Flip

Flipping the chart is useful when you have many categories or long labels.

Code
starwars %>% 
  drop_na(sex) %>% 
  ggplot(aes(x = sex)) +
  geom_bar(fill = "purple", alpha = 0.6) +
  coord_flip() +
  theme_test() +
  labs(title = "Horizontal Bar Chart",
       x = "Sex",
       y = "Count")

Sorting by Frequency

The most professional way to present a bar chart is by sorting the categories from most frequent to least frequent.

Code
starwars %>% 
  drop_na(sex) %>% 
  ggplot(aes(x = fct_infreq(sex))) +
  geom_bar(fill = "purple", alpha = 0.6) +
  theme_test() +
  labs(title = "Frequency Sorted Bar Chart",
       subtitle = "Ordered from highest to lowest frequency",
       x = "Biological Sex",
       y = "Total Characters",
       caption = "Data: starwars dataset | Prepared by Abdullah Al Shamim")


Systemic Summary Toolkit

Function Role Systemic Importance
geom_bar() Statistical Geometry Automatically calculates counts.
drop_na(col) Targeted Cleaning Ensures data integrity without over-filtering.
fct_infreq() Factor Reordering Provides instant visual hierarchy.
coord_flip() Spatial Adjustment Optimizes plot for readability.