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 plotstheme_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 missingdrop_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.