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 heapmap 

gapminder %>%
  filter(year >= 1990) %>%
  ggplot(aes(x = year, y = region, fill = life_expectancy)) +
  geom_tile(color = "white") +  # Creates a heatmap with white gridlines between tiles
  scale_fill_viridis_c(option = "C") +  # Color scale for life expectancy
  labs(
    title = "Life Expectancy by Region Over Time (1990-Present)",  # Title
    x = "Year",                # X-axis label
    y = "Region"               # Y-axis label
  ) +
  theme_minimal(base_size = 15) +  # Minimal theme for readability
  theme(
    plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),  # Centered, bold title
    axis.text.x = element_text(angle = 45, hjust = 1)  # Rotate x-axis labels
  )

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)