Created
April 6, 2023 18:31
-
-
Save rich-iannone/120a047903f8452fa80b3f2612ad232a to your computer and use it in GitHub Desktop.
Population Ranges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gt) | |
library(tidyverse) | |
countrypops |> | |
dplyr::filter(year == 2021) |> | |
dplyr::select(country_code_2, population) |> | |
dplyr::mutate(population_class = cut( | |
population, | |
breaks = scales::breaks_log(n = 20)(population) | |
)) |> | |
dplyr::group_by(population_class) |> | |
dplyr::summarize( | |
count = dplyr::n(), | |
countries = paste0(country_code_2, collapse = ",") | |
) |> | |
dplyr::arrange(desc(population_class)) |> | |
dplyr::mutate(tier = dplyr::row_number(), .before = population_class) |> | |
gt() |> | |
tab_header(title = "All Countries Grouped by Population Range") |> | |
fmt_flag(columns = countries) |> | |
fmt_spelled_num() |> | |
fmt_index(columns = tier) |> | |
cols_label( | |
tier = "", | |
population_class = "Population Range", | |
count = "Count", | |
countries = "Countries" | |
) |> | |
cols_width( | |
population_class ~ px(150), | |
count ~ px(100) | |
) |> | |
cols_align(align = "center", columns = c(tier, count)) |> | |
opt_stylize(style = 6, color = "gray") |> | |
opt_table_font(font = google_font("IBM Plex Sans")) |> | |
opt_align_table_header(align = "left") |> | |
data_color(columns = count, method = "numeric", palette = "viridis") |> | |
fmt_bins( | |
columns = population_class, | |
fmt = ~ fmt_integer(., suffixing = TRUE) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment