Assignment Description

Use kableExtra and the gapminder data we used earlier in the semester to re-create a table (provided by Dr. Suleiman) that shows the life population and life expectancy for the two least populated countries in the Americas and Europe for 1997.

Preparation

Load required libraries and prepare gapminder data for table visualization.

# Load required libraries
library (knitr)
library (tidyverse)
library (gapminder)
library (kableExtra)

# Extract 2 least populated countries in the Americas and Europe in 1997
gapminderSubset <- gapminder %>%
  filter ((year == 1997) & (continent == "Americas" | continent == "Europe")) %>%
  transmute (continent, country, lifeExp, pop) %>%
  group_by (continent) %>%
  arrange (continent, country, pop) %>%
  top_n (-2) %>%
  ungroup ()

# Prepare population statistic
populationData <- gapminderSubset %>%
  transmute (statistic = "population", country, pop) %>%
  pivot_wider (names_from = country, values_from = pop)

# Prepare life expectancy statistic
lifeExpectancyData <- gapminderSubset %>%
  transmute (statistic = "life expectancy", country, lifeExp) %>%
  pivot_wider (names_from = country, values_from = lifeExp)

# Join the statistics into a single table
tableData <- populationData %>%
  full_join (lifeExpectancyData)

Table

Use kableExtra to replicate the table shared by Dr. Suleiman.

# Replicate the chart shared by our professor
tableData %>%
  # No decimals in numeric data
  kable (digits = 0) %>%
  # kable styling: Table striping, width and character string positioning
  kable_styling ("striped", full_width = FALSE, position = "left") %>% 
  # Statistic data italicized
  column_spec (1, italic = TRUE) %>%
  # Extra header row grouping countries by continent
  add_header_above (c (" " = 1, "Americas" = 2, "Europe" = 2)) %>%
  # Footnote
  footnote (general = "Population and life expectancy for the two least populous countries in Americas and Europe in 1997.", footnote_as_chunk = TRUE, title_format = c ("italic", "underline"))
Americas
Europe
statistic Jamaica Trinidad and Tobago Iceland Montenegro
population 2531311 1138101 271192 692651
life expectancy 72 69 79 75
Note: Population and life expectancy for the two least populous countries in Americas and Europe in 1997.