map

path_to_sheet <- here("data_raw", "CO2 data.xlsx")

Egen_data <- partial(
  .f = read_excel,
  path = path_to_sheet,
  sheet = "Elec_gen",
  col_names = FALSE
)

Egen_data <- suppressMessages(Egen_data(range = "A2:B79"))


Egen_data <- Egen_data |>
  rename(country_name = `...1`, value = `...2`)
  
Egen_data <- Egen_data |>
  mutate(country_name = ifelse(country_name == "United States", "United States of America", country_name))|>
  mutate(country_name = ifelse(country_name == "Democratic Republic of Congo", "Dem. Rep. Congo", country_name))|>
  mutate(country_name = ifelse(country_name == "South Sudan", "S. Sudan", country_name))|>
  mutate(country_name = ifelse(country_name == "Central African Republic", "Central African Rep.", country_name))|>
  mutate(country_name = ifelse(country_name == "Cote d'Ivoire", "Côte d'Ivoire", country_name))



world_map <- ne_countries(scale = "medium", returnclass = "sf")
world_egen_map <- left_join(world_map, Egen_data, by = c("name" = "country_name"))


leaflet_map <- leaflet(data = world_egen_map) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~colorQuantile(palette = "YlGnBu", domain = na.omit(world_egen_map$value), n = 5)(value),
    weight = 1,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 2,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = ~paste("Country:", name, "& Solar Power Capacity:", value,"GWh"),
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    )
  ) %>%
  addLegend(
    position = "bottomright",
    pal = colorQuantile(palette = "YlGnBu", domain = na.omit(world_egen_map$value), n = 5),
    values = ~value,
    title = "Percentile Ranking of Major Solar Power Capacity Countries in 2022",
    labFormat = labelFormat(prefix = ""),
    opacity = 0.7
  ) %>%
  setView(lng = -96.796, lat = 32.7767, zoom = 1.5) 

leaflet_map
htmlwidgets::saveWidget(leaflet_map, file="leaflet_map.html")
Warning: `includeHTML()` was provided a `path` that appears to be a complete HTML document.
✖ Path: leaflet_map.html
ℹ Use `tags$iframe()` to include an HTML document. You can either ensure `path` is accessible in your app or document (see e.g. `shiny::addResourcePath()`) and pass the relative path to the `src` argument. Or you can read the contents of `path` and pass the contents to `srcdoc`.
leaflet