apexcharter/data-raw/life_expectancy.R

69 lines
2.0 KiB
R
Raw Permalink Normal View History

2023-06-14 11:40:03 +02:00
# Package -----------------------------------------------------------------
library(data.table)
library(gapminder)
# Data --------------------------------------------------------------------
2024-05-13 11:17:46 +02:00
life_expec_long <- as.data.table(gapminder::gapminder)
life_expec_long <- life_expec_long[year %in% c(1972, 2007), list(country, year, lifeExp)]
2023-06-14 11:40:03 +02:00
# life_expec <- life_expec[country %in% sample(unique(country), 10)]
2024-05-13 11:17:46 +02:00
life_expec_long <- life_expec_long[country %in% c("Botswana", "Ghana", "Iran", "Liberia", "Malaysia", "Mexico",
2023-06-14 11:40:03 +02:00
"Nigeria", "Pakistan", "Philippines", "Zambia")]
2024-05-13 11:17:46 +02:00
life_expec_long[, country := as.character(country)]
life_expec <- dcast(life_expec_long, country ~ year, value.var = "lifeExp")
2023-06-14 11:40:03 +02:00
life_expec[, type := fifelse(`1972` > `2007`, "decreased", "increased")]
# Use data ----------------------------------------------------------------
setDF(life_expec)
usethis::use_data(life_expec, internal = FALSE, overwrite = TRUE, compress = "xz")
2024-05-13 11:17:46 +02:00
setDF(life_expec_long)
usethis::use_data(life_expec_long, internal = FALSE, overwrite = TRUE, compress = "xz")
2023-06-14 11:40:03 +02:00
# Test example ------------------------------------------------------------
pkgload::load_all()
2024-05-13 11:17:46 +02:00
apex(life_expec, aes(country, x = `1972`, xend = `2007`), type = "dumbbell") %>%
2023-06-14 11:40:03 +02:00
ax_plotOptions(
bar = bar_opts(
dumbbellColors = list(list("#3d85c6", "#fb6003"))
)
2024-05-13 11:17:46 +02:00
) %>%
ax_colors("#BABABA") %>%
2023-06-14 11:40:03 +02:00
ax_labs(
title = "Life expectancy : 1972 vs. 2007",
subtitle = "Data from Gapminder dataset",
x = "Life expectancy at birth, in years"
)
2024-05-13 11:17:46 +02:00
apex(life_expec, aes(country, x = `1972`, xend = `2007`, group = type), type = "dumbbell") %>%
2023-06-14 11:40:03 +02:00
ax_xaxis(type = "category", categories = unique(life_expec$country)) %>%
ax_plotOptions(
bar = bar_opts(
dumbbellColors = list(list("#3d85c6", "#fb6003"), list("#3d85c6", "#fb6003"))
)
2024-05-13 11:17:46 +02:00
) %>%
ax_colors(c("#3d85c6", "#fb6003")) %>%
2023-06-14 11:40:03 +02:00
ax_labs(
title = "Life expectancy : 1972 vs. 2007",
subtitle = "Data from Gapminder dataset",
x = "Life expectancy at birth, in years"
)