Assignment 5

Author

Sadie Bulger

As best you can, recreate this table showing the population and life expectancy for the two least populated countries in the Americas and Europe for 1997. Your assignment must show all code in the output. Use as many code chunks as you feel necessary, I recommend first getting the output to match the output below. Hint: you will probably find it easiest to use two pivot_wider() functions with a union() to replicate the data layout. Use the gapminder dataset.

The Grading Rubric is available at the end of this document.

The word Skál! should link to https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/ it doesn’t in the image because it is an image. The following packages are useful and loaded for you.

library(tidyverse)
library(gapminder)
library(gt)
library(gtExtras)

This is your work area. Add as many code cells as you need.

pop_data <- gapminder |>
  filter(country %in% c("Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"),
         year == 1997) |>
  select(country, population = pop) |>
  mutate(metric = "population") |>
  pivot_wider(names_from = country, values_from = population)
pop_data
# A tibble: 1 × 5
  metric     Iceland Jamaica Montenegro `Trinidad and Tobago`
  <chr>        <int>   <int>      <int>                 <int>
1 population  271192 2531311     692651               1138101
life_exp_data <- gapminder |>
   filter(country %in% c("Jamaica", "Trinidad and Tobago", "Iceland", "Montenegro"),
         year == 1997) |>
  select(country, lifeExp) |>
  mutate(metric = "life expectancy") |>
  pivot_wider(names_from = country, values_from = lifeExp)
life_exp_data
# A tibble: 1 × 5
  metric          Iceland Jamaica Montenegro `Trinidad and Tobago`
  <chr>             <dbl>   <dbl>      <dbl>                 <dbl>
1 life expectancy    79.0    72.3       75.4                  69.5
data_join <- union(pop_data, life_exp_data) |>
    select(metric, Jamaica, `Trinidad and Tobago`, Iceland, Montenegro) |>
    mutate(across(c(Jamaica, `Trinidad and Tobago`, Iceland, Montenegro),
                ~ ifelse(metric == "population",
                         scales::comma(round(.x, 0)),
                         sprintf("%.1f", round(.x, 1)))))
data_join
# A tibble: 2 × 5
  metric          Jamaica   `Trinidad and Tobago` Iceland Montenegro
  <chr>           <chr>     <chr>                 <chr>   <chr>     
1 population      2,531,311 1,138,101             271,192 692,651   
2 life expectancy 72.3      69.5                  79.0    75.4      
data_join <- data_join |>
  gt() |>
    tab_spanner(
    label = md("**Europe**"),
    columns = c('Iceland', 'Montenegro')) |>
  tab_spanner(
    label = md("**Americas**"),
    columns = c(Jamaica, `Trinidad and Tobago`))
data_join
metric
Americas
Europe
Jamaica Trinidad and Tobago Iceland Montenegro
population 2,531,311 1,138,101 271,192 692,651
life expectancy 72.3 69.5 79.0 75.4
data_join <- data_join |>
    cols_label(
    metric = fontawesome::fa("chart-bar"),
    Iceland = md("**Iceland**"),
    Montenegro = md("**Montenegro**"),
    Jamaica = md("**Jamaica**"),
    'Trinidad and Tobago' = md("**Trinidad and Tobago**"))
data_join
Americas
Europe
Jamaica Trinidad and Tobago Iceland Montenegro
population 2,531,311 1,138,101 271,192 692,651
life expectancy 72.3 69.5 79.0 75.4
data_join <- data_join |>
  tab_header(title = md("🇮🇸**Iceland**"),
             subtitle = md("**Fewer people, longer lives**")) |>
  tab_style(style = cell_text(align = "left"),
            locations = cells_title(groups = c("title", "subtitle"))) |>
  tab_source_note(md("[Skál!](https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/)"))
data_join
🇮🇸Iceland
Fewer people, longer lives
Americas
Europe
Jamaica Trinidad and Tobago Iceland Montenegro
population 2,531,311 1,138,101 271,192 692,651
life expectancy 72.3 69.5 79.0 75.4
Skál!
data_join1 <- data_join |>
  gt_theme_guardian() |>
  opt_stylize(1) |>
  tab_style(style = cell_fill(color = "#9ecae1"),
            locations = cells_body(columns = Iceland))
data_join1
🇮🇸Iceland
Fewer people, longer lives
Americas
Europe
Jamaica Trinidad and Tobago Iceland Montenegro
population 2,531,311 1,138,101 271,192 692,651
life expectancy 72.3 69.5 79.0 75.4
Skál!
data_join2 <- data_join |>
  gt_theme_guardian() |>
  tab_options(
    table.background.color = "#f7f7f7",          # slightly lighter grey
    heading.background.color = "#f7f7f7",
    column_labels.background.color = "#f7f7f7",
    column_labels.font.weight = "bold",
    table_body.border.bottom.color = "#ffffff",
    table.font.size = px(14)
  ) |>
  tab_style(                                    # top blue line above header
    style = cell_borders(sides = "top", color = "#9ecae1", weight = px(2)),
    locations = cells_title(groups = "title")
  ) |>
  tab_style(                                    # bottom blue line below header
    style = cell_borders(sides = "bottom", color = "#9ecae1", weight = px(2)),
    locations = cells_title(groups = "subtitle")
  ) |>
  tab_style(                                    # rows grey shading (population & life expectancy)
    style = cell_fill(color = "#ebebeb"),
    locations = cells_body(rows = metric %in% c("population", "life expectancy"))
  ) |>
  tab_style(                                    # title/subtitle styling
    style = cell_text(size = px(20), weight = "bold", color = "#2171b5"),
    locations = cells_title(groups = c("title", "subtitle"))
  ) |>
    tab_style(style = cell_fill(color = "#9ecae1"),
            locations = cells_body(columns = Iceland))
data_join2
🇮🇸Iceland
Fewer people, longer lives
Americas
Europe
Jamaica Trinidad and Tobago Iceland Montenegro
population 2,531,311 1,138,101 271,192 692,651
life expectancy 72.3 69.5 79.0 75.4
Skál!

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Narrative: typos and grammatical errors
(7%)
Document formatting: correctly implemented instructions
(7%)
Accuracy of replicated image as a gt table
(78% )
Submitted properly to Brightspace
(8%)
NA NA You must submit according to instructions to receive any credit for this portion.