An R Tutorial On Visualizing Population Pyramids -Part 2-
Abbad Bouchra under the supervision of Dr.Farid FLICI
Load Necessary Libraries
First, we need to load the necessary libraries for data manipulation and visualization.
# Set the CRAN mirror
options(repos = c(CRAN = "https://cran.rstudio.com/"))# Load necessary packages and suppress warnings
if (!requireNamespace("readxl", quietly = TRUE)) {
install.packages("readxl")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
if (!requireNamespace("reshape", quietly = TRUE)) {
install.packages("reshape")
}
if (!requireNamespace("reshape2", quietly = TRUE)) {
install.packages("reshape2")
}
if (!requireNamespace("pyramid", quietly = TRUE)) {
install.packages("pyramid")
}
if (!requireNamespace("animation", quietly = TRUE)) {
install.packages("animation")
}
if (!requireNamespace("ggthemes", quietly = TRUE)) {
install.packages("ggthemes")
}
# Load necessary packages and suppress warnings
suppressWarnings(suppressPackageStartupMessages(library(readxl)))
suppressWarnings(suppressPackageStartupMessages(library(ggplot2)))
suppressWarnings(suppressPackageStartupMessages(library(reshape)))
suppressWarnings(suppressPackageStartupMessages(library(reshape2)))
suppressWarnings(suppressPackageStartupMessages(library(pyramid)))
suppressWarnings(suppressPackageStartupMessages(library(animation)))
suppressWarnings(suppressPackageStartupMessages(library(ggthemes)))
suppressWarnings(suppressPackageStartupMessages(library(tidyverse)))Upload Population Data
Assume that you have an Excel file.
Prepare Data for Visualization
Now, we’ll prepare the data for creating population pyramids. We’ll select data for males and females separately.
Create Population Pyramid:
This section demonstrates how to create an initial population pyramid displaying both male and female populations.

Adjust the Title and Sizes:
Enhance the appearance of the population pyramid by adjusting the title’s position and modifying text sizes.

Population Pyramid Using Lines:
Create a population pyramid using lines instead of bars for a smoother representation.

Explanation of Adjustments:
Line Plot : Replaced bars with lines for a smoother representation of the population distribution.
Color Customization : Used blue for males and red for females, making the plot more visually distinct.
Theme Adjustments : Enhanced the theme with larger and bold titles, adjusted axis labels and text sizes, and repositioned the legend to the top.
Y-axis Labels : Applied absolute values to y-axis labels for better readability.
Flip Coordinates : Retained coord_flip() for the horizontal layout of the pyramid.
Population Pyramid By Age Groups:
Improve the visualization by defining age groups and categorizing the ages accordingly.
Create Population Pyramid:
Create the population pyramid using the defined age groups and population data for males and females.

Enhanced Population Pyramid Plot Using Lines:
Create an enhanced population pyramid plot using lines for a clearer representation.

Combined Population Pyramid Plot:
Incorporate elements from both bar and line plots to create a comprehensive and visually appealing population pyramid.

Explanation of Combined Elements:
Bar Plots: Added bar plots for males and females with light colors and semi-transparency (alpha = 0.4) for a subtle background representation of the population.
Line Plots: Added line plots for males and females with distinct colors for a clear and precise representation of the population trends.
Custom Colors: Used
scale_fill_manualandscale_color_manualto differentiate between male and female population visually.Labels and Titles: Set appropriate labels and titles for clarity.
Theme Customizations: Included theme settings for better readability and visual appeal, including text rotation and gridlines.
Axis and Coordinates: Adjusted y-axis labels to show absolute values and flipped coordinates for a horizontal pyramid representation.
The Modified Combined Plot Code with a Custom Background Color

This work has been conducted by Abbad Bouchra under the supervision of Flici Farid at CREAD (Centre de Recherche en Économie Appliquée et du Développement).
Last updated