I have just started using leaflet, and have been having trouble getting the map to show the actual data points. I am using the wbstats library to attempt to show the population growth rate in each country, but it does not seem to be working. The code runs with no errors but none of the population growth data shows on the map, its just a blank map.
world_geo <- rnaturalearth::ne_countries(scale = 50, returnclass = "sf")
pop_data <- wb(country = "countries_only",
indicator = "SP.POP.GROW",
mrv = 1)
pop_geo <- left_join(world_geo, pop_data, by = c("iso_a2" = "iso2c"))
library(viridis)
ggplot(pop_geo) +
geom_sf(aes(fill = value)) +
scale_fill_viridis("value") +
ggtitle("Population Growth (annual % growth)") +
theme_bw()
pal <- colorNumeric("viridis", domain = pop_geo$value)
labels <- sprintf("%s
%s: %g%%",
pop_geo$name_long, pop_geo$indicator, round(pop_geo$value, 2)) %>%
lapply(htmltools::HTML)
l <- leaflet(pop_geo, height = 400, width = "100%") %>%
setView(20,25, zoom = 1) %>%
addTiles() %>%
addPolygons(
fillColor = ~pal(value),
weight = 1,
opacity = 1,
color = "grey",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 3,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 6px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal, values = ~value, opacity = 0.9,
title = NULL,
position = "bottomright",
labFormat = labelFormat(suffix = "%"))
l