library(leaflet)
library(tidyverse)
library(spData)
library(sf)
data(world)
world <- st_as_sf(world)
wpp_2022 <- read_rds("wpp_2022.rds")
my_wpp <- wpp_2022 |>
filter(year == 2024)
world_data <- world |>
left_join(my_wpp, join_by(iso_a2 == ISO2))
world_data <- world_data |>
filter(
!is.na(TFR)
)
bins <- c(0, 1.5, 2.1, 3, 4, 5, Inf)
pal <- colorBin("YlOrRd", domain = world_data$TFR, bins = bins)
labels <- sprintf("<strong>%s</strong><br/>%g",
world_data$name_long, world_data$TFR) |> lapply(htmltools::HTML)
leaflet(world_data) |>
addProviderTiles(providers$Esri.WorldTopoMap) |>
addPolygons(
fillColor = ~pal(TFR),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.6,
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.6,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
) |>
addLegend(
pal = pal, values = ~TFR, opacity = 0.6, title = NULL,
position = "bottomright"
)