|
| 1 | +library(countrycode) |
| 2 | +library(eurostat) |
| 3 | +library(tidyverse) |
| 4 | +library(sf) |
| 5 | +library(maps) |
| 6 | + |
| 7 | +# unzip the data |
| 8 | +# unzip("data/residenti_stranieri_in_italia.zip", exdir = "data") |
| 9 | +# unzip("data/residenti_in_italia.zip", exdir = "data") |
| 10 | + |
| 11 | +# rename the files |
| 12 | +# file.rename("data/DCIS_POPRES1 - Popolazione residente al 1° gennaio - intero ds.csv", "data/residenti_in_italia.csv") |
| 13 | +# file.rename("data/DCIS_POPSTRCIT1 - Stranieri residenti al 1° gennaio - Cittadinanza - intero ds.csv", "data/residenti_stranieri_in_italia.csv") |
| 14 | +# |
| 15 | + |
| 16 | +# load the data ----------------------------------------------------------------------------------------------- |
| 17 | + |
| 18 | +# stranieri in italia |
| 19 | +read_delim("data/residenti_stranieri_in_italia.csv", delim = "|") |> |
| 20 | + tibble() |> |
| 21 | + janitor::clean_names() |> |
| 22 | + janitor::remove_empty(c("rows", "cols")) |> |
| 23 | + janitor::remove_constant() -> df_stranieri |
| 24 | + # mutate(across(where(is.character), as.factor)) |> |
| 25 | + # filter(value > 30) |
| 26 | + |
| 27 | +# residenti in italia |
| 28 | +read_delim("data/residenti_in_italia.csv", delim = ",") |> |
| 29 | + tibble() |> |
| 30 | + janitor::clean_names() |> |
| 31 | + janitor::remove_empty(c("rows", "cols")) |> |
| 32 | + janitor::remove_constant() |> |
| 33 | + # mutate(across(where(is.character), as.factor)) |> |
| 34 | + # filter(value > 30) |> |
| 35 | + select(itter107, time, sesso, value) -> df_residenti |
| 36 | + |
| 37 | +# join the two datasets |
| 38 | +left_join( |
| 39 | + df_stranieri, |
| 40 | + df_residenti, |
| 41 | + by = c("itter107", "time", "sesso"), |
| 42 | + suffix = c("", "_residenti") |
| 43 | +) |> |
| 44 | +mutate( |
| 45 | + ratio = value / value_residenti |
| 46 | +) |> |
| 47 | + select(itter107, territorio, sesso, iso, paese_di_cittadinanza, time, value, ratio) -> df |
| 48 | + |
| 49 | +# read the continents and the macroregions |
| 50 | +df |> |
| 51 | + distinct(iso, nome = paese_di_cittadinanza) |> |
| 52 | + mutate( |
| 53 | + iso = case_when( |
| 54 | + nome == "Kosovo" ~ "XK", |
| 55 | + nome == "Namibia" ~ "NA", |
| 56 | + nome == "Regno unito" ~ "GB", |
| 57 | + TRUE ~ iso |
| 58 | + ), |
| 59 | + category = case_when( |
| 60 | + iso %in% c("EU28", "WORLD", "999") ~ "macroregion", # 999 apolide |
| 61 | + str_detect(iso, "_") ~ "macroregion", |
| 62 | + TRUE ~ "country" |
| 63 | + ), |
| 64 | + name = countrycode(iso, origin = "iso2c", destination = "country.name.en"), |
| 65 | + name = ifelse(is.na(name), nome, name), |
| 66 | + name_map = case_when( |
| 67 | + name == "United Kingdom" ~ "UK", |
| 68 | + name == "United States" ~ "USA", |
| 69 | + name == "Czechia" ~ "Czech Republic", |
| 70 | + name == "Bosnia & Herzegovina" ~ "Bosnia and Herzegovina", |
| 71 | + name == "Côte d’Ivoire" ~ "Ivory Coast", |
| 72 | + name == "Congo - Kinshasa" ~ "Democratic Republic of the Congo", |
| 73 | + name == "Congo - Brazzaville" ~ "Republic of Congo", |
| 74 | + name == "Myanmar (Burma)" ~ "Myanmar", |
| 75 | + name == "Palestinian Territories" ~ "Palestine", |
| 76 | + TRUE ~ name |
| 77 | + ), |
| 78 | + emoji = countrycode(iso, origin = "iso2c", destination = "unicode.symbol"), |
| 79 | + continent = countrycode(iso, origin = "iso2c", destination = "continent"), |
| 80 | + region = countrycode(iso, origin = "iso2c", destination = "region"), |
| 81 | + region23 = countrycode(iso, origin = "iso2c", destination = "region23"), |
| 82 | + iso = case_when( |
| 83 | + iso == "GB" ~ "UK", |
| 84 | + TRUE ~ iso |
| 85 | + ) |
| 86 | + ) -> mapping_world_locations |
| 87 | + |
| 88 | +mapping_italian_locations <- df |> |
| 89 | + distinct(code = itter107) |> |
| 90 | + left_join(df |> select(itter107, name = territorio), by = c("code" = "itter107")) |> |
| 91 | + distinct(code, name) |> |
| 92 | + mutate( |
| 93 | + category = case_when( |
| 94 | + str_detect(code, "^[A-Z]{3}$") ~ "macroregione", |
| 95 | + str_detect(code, "^[A-Z]{3}[0-9]{1}$") ~ "regione", |
| 96 | + str_detect(code, "^[A-Z]{3}[0-9]{2}$") ~ "provincia", |
| 97 | + str_detect(code, "^[0-9]{6}$") ~ "comune", |
| 98 | + ) |
| 99 | + ) |> |
| 100 | + filter(!is.na(name)) |
| 101 | + |
| 102 | +# MAPS ----------------------------------------------------------------------------------------------- |
| 103 | +# Get the borders of the world countries |
| 104 | +world_borders <- map_data("world", exact = F) |
| 105 | + |
| 106 | +world_borders <- st_as_sf(world_borders, coords = c("long", "lat"), crs = 4326, remove = FALSE) %>% |
| 107 | + arrange(group, order) %>% # Assicurati che i punti siano nell'ordine corretto |
| 108 | + group_by(region, group) %>% |
| 109 | + summarise(geometry = st_combine(geometry), .groups = 'drop') %>% |
| 110 | + st_cast("POLYGON") |
| 111 | + |
| 112 | +world_borders <- st_cast(world_borders, "MULTIPOLYGON") |
| 113 | + |
| 114 | +italian_borders <- get_eurostat_geospatial(resolution = 60) |> |
| 115 | + filter(CNTR_CODE %in% c("IT")) |
| 116 | + |
| 117 | + |
| 118 | +## get the countries that are not in the world map |
| 119 | +mapping_world_locations |> |
| 120 | + left_join(df |> summarise(value = max(value), .by = paese_di_cittadinanza), by = c("nome" = "paese_di_cittadinanza")) |> |
| 121 | + anti_join(map_data("world", exact = F), by = c("name_map" = "region")) |> |
| 122 | + filter(category == "country") |> |
| 123 | + arrange(desc(value)) |
| 124 | + |
| 125 | +# map_data("world", exact = F) |> |
| 126 | +# distinct(region) |> |
| 127 | +# filter(!region %in% mapping_world_locations$name_map) |> |
| 128 | +# arrange(region) |
| 129 | + |
| 130 | +# resize the df to work in shinyapp.com and github.com |
| 131 | +df |> |
| 132 | + filter( |
| 133 | + # !(ratio < 1e-8 & territorio != "Italia"), |
| 134 | + value > 5 |
| 135 | + ) -> df_small |
| 136 | + |
| 137 | + |
| 138 | +write_rds(df, "Stranieri in Italia/data/residenti_in_italia.rds") |
| 139 | +write_rds(df_small, "Stranieri in Italia/data/residenti_in_italia_small.rds") |
| 140 | +write_rds(italian_borders, "Stranieri in Italia/data/italian_borders.rds") |
| 141 | +write_rds(world_borders, "Stranieri in Italia/data/world_borders.rds") |
| 142 | +write_rds(mapping_world_locations, "Stranieri in Italia/data/mapping_world_locations.rds") |
| 143 | +write_rds(mapping_italian_locations, "Stranieri in Italia/data/mapping_italian_locations.rds") |
| 144 | + |
| 145 | + |
0 commit comments