Exploring Life Expectancy and Fertility Trends Across Region

Author

AYOMIDE JOE-ADIGWE

Load packages

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs) # for the gapminder dataset
library(highcharter) # FOR INTERACTING
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use

Attaching package: 'highcharter'

The following object is masked from 'package:dslabs':

    stars

Load the gapminder dataset from the dslabs package library(dslabs)

data("gapminder")

Filter dataset to include only recent years for clarity

gapminder_recent <- gapminder %>% filter(year >= 1990)

Creating the heatmap 

Adds interactivity so users can hover over points to see more information

highchart() %>%
  hc_add_series(
    data = gapminder %>% filter(year >= 1990),
    type = "heatmap",
    hcaes(x = fertility, y = life_expectancy, group = region),
    marker = list(radius = 5, symbol = "circle")
  ) %>%
  hc_title(text = "Life Expectancy vs. Fertility Rate by Region") %>%
  hc_xAxis(title = list(text = "Fertility Rate (Children per Woman)")) %>%
  hc_yAxis(title = list(text = "Life Expectancy (Years)")) %>%
  hc_legend(enabled = TRUE)

To analyze trends in life expectancy by region over recent years, I used the ‘gapminder’ dataset from the ‘dslabs’ package. This dataset offers a comprehensive look at global development indicators, including life expectancy, fertility rates, and GDP across countries and regions. For clarity and focus on more recent trends, I filtered the dataset to include only data from 1990 onward.

To create the visualization, I used ‘ggplot2’ and ‘highcharter’. First, with ‘ggplot2’, I created a heatmap where ‘year’ is plotted on the x-axis, ‘region’ on the y-axis, and ‘life_expectancy’ represented by color intensity. The ‘geom_tile()’ function was used to make this heatmap, and I customized the color scheme with ‘scale_fill_viridis_c()’ for clear contrast. For a clean and engaging presentation, I used the minimal theme and adjusted the title and axis labels for readability.

Then, with ‘highcharter’, I added interactivity to allow users to hover over each point for detailed values. Here, life expectancy is plotted against fertility rates by region, adding an interactive layer that improves data exploration.