mappa interattiva

library(readxl)        
library(dplyr)      

Caricamento pacchetto: 'dplyr'
I seguenti oggetti sono mascherati da 'package:stats':

    filter, lag
I seguenti oggetti sono mascherati da 'package:base':

    intersect, setdiff, setequal, union
library(leaflet)     
library(sf)          
Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
library(rnaturalearth) 
library(rnaturalearthdata)

Caricamento pacchetto: 'rnaturalearthdata'
Il seguente oggetto è mascherato da 'package:rnaturalearth':

    countries110
slavery <- read_excel("2023-Global-Slavery-Index-Data.xlsx",sheet=1)
co2 <- read_excel("CO2 PAESI.xlsx")


co2_2023 <- co2 %>%
  filter(reparto == "Manufacturing Industries and Construction") %>%
  select(Country, `Y_2023`) %>%
  rename(CO2_2023 = `Y_2023`)

# --- PULIZIA DATI LAVORO MINORILE ---
slavery <- slavery %>%
  mutate(Child_Labour = as.numeric(Child_labour_percentage)) %>%
  select(Country, Child_Labour)
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Child_Labour = as.numeric(Child_labour_percentage)`.
Caused by warning:
! NA introdotti per coercizione
# --- JOIN DATI CO2 + LAVORO MINORILE ---
combined_data <- left_join(co2_2023, slavery, by = "Country")

# --- JOIN CON MAPPA MONDO ---
world <- ne_countries(scale = "medium", returnclass = "sf")
map_data <- left_join(world, combined_data, by = c("name" = "Country"))

# --- MAPPA INTERATTIVA UNICA ---
leaflet(map_data) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~colorQuantile("Blues", CO2_2023)(CO2_2023),
    fillOpacity = 0.7,
    color = "white",
    weight = 1,
    popup = ~paste0(
      "<b>", name, "</b><br>",
      "CO₂ 2023 (Manufacturing): ", round(CO2_2023, 1), " Mt<br>",
      "Lavoro minorile: ", Child_Labour, " %"
    )
  ) %>%
  addLegend(
    "bottomright",
    pal = colorQuantile("Blues", map_data$CO2_2023),
    values = ~CO2_2023,
    title = "CO₂ 2023 and Child labor (%) <br>(Manufacturing Industries and Construction)"
  )