An R Tutorial On Visualizing Population Pyramids

Abbad Bouchra under the supervision of Dr.Farid FLICI

Summary

In this tutorial, we explored how to create population pyramids using R and ggplot2. Population pyramids are essential tools for visualizing the distribution of population across different age groups and genders. Here’s a breakdown of what we covered:

  1. Introduction

  2. Loading Libraries

  3. Data Preparation

  4. Creating Population Pyramids

  5. Adjustments and Customization

  6. Population Projection

By following this tutorial, you should now have a solid understanding of how to create and customize population pyramids in RMarkdown using ggplot2.

Introduction

In this tutorial, we will learn how to create population pyramids using the ggplot2 package in Rmarkdown. Population pyramids are graphical illustrations that show the distribution of population numbers by and age groups.

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

Now, let’s create an initial population pyramid showing both males and females.

Population Pyramid By 5 Age Groups

To improve the visualization, let’s define age groups and categorize the ages accordingly.

Create population Pyramid

Now, let’s create the population pyramid using the defined age groups and population data for males and females.

Reshape Data for Population Pyramid Plotting

Next, let’s reshape the pyram data frame using the reshape2 package to prepare it for plotting the population pyramid.

Plot Population Pyramid

Let’s plot the population pyramid using the reshaped data.

More Options

Adjusted Stacked Population Pyramid

Let’s create a stacked population pyramid with adjusted y-axis breaks and labels.

Population Pyramid with ggplot2

Let’s create a population pyramid using ggplot2.

Population Projection

In this section, we will use the given dataset to create a population projection.

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