## ----setup, include=FALSE--------------------------- library(knitr) knitr::opts_chunk$set( fig.retina = 3, message = FALSE, warning = FALSE, out.width = "60%", echo=TRUE ) ## --------------------------------------------------- #install.packages("wpp2019") library(wpp2019) data(migration) data(pop) ## --------------------------------------------------- library(tidyverse) A=migration[,1:17] %>% filter(name=="Italy") %>% pivot_longer(cols=-c(country_code, name), names_to="period", values_to="net_mig") %>% ggplot(aes(x=period, y=net_mig, group=name))+ geom_line()+ theme_bw()+ labs(title="ITALIA", x="Anno", y="Net Migration" ) ## --------------------------------------------------- A ## --------------------------------------------------- library(knitr) kable(migration$name[migration$country_code>900 & migration$country_code<1000 ], type="html") ## --------------------------------------------------- names(migration)[1:17]<-names(pop)[1:17] migration_long<- migration[,1:17] %>% pivot_longer(cols=-c(country_code, name), names_to="year", values_to="mig") pop_long<- pop %>% pivot_longer(cols=-c(country_code, name), names_to="year", values_to="pop") %>% filter(pop>10000) ### seleziono paesi con pop> 10 mil mig_pop<-pop_long %>% left_join(migration_long, by=c("country_code", "name", "year")) %>% mutate(net.mig=1000*mig/pop) ## --------------------------------------------------- B<- mig_pop %>% ggplot(aes(x=year, y=net.mig, group=name, label=name))+ geom_line(col="grey", alpha=0.2)+ theme_bw()+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))+ labs(title="Net migration", x="Year", y="Net Migration", caption="Source: World Population Prospect" ) ## --------------------------------------------------- B+ geom_line(data=mig_pop %>% filter(name=="Italy"), aes(col="Italy"), alpha=0.8, size=1.1)+ geom_line(data=mig_pop %>% filter(name=="Argentina"), aes(col="Argentina"), alpha=0.8, size=1.1) ## --------------------------------------------------- library(plotly) ggplotly(B) ## --------------------------------------------------- library(readr) IMMprovince <- read_delim("IMMprovince.csv", ";", escape_double = FALSE, trim_ws = TRUE, skip = 114, col_names=c("cod.prov","prov", "cod.citt", "citt", "maschi", "femmine", "totale")) ## --------------------------------------------------- IMMprovince.tot<- IMMprovince %>% group_by(cod.prov, prov) %>% summarize(tot_stranieri=sum(totale)) %>% ungroup() %>% arrange(-tot_stranieri) %>% mutate(rank_abs=row_number(), cod.prov=as.numeric(cod.prov)) ## --------------------------------------------------- kable(IMMprovince.tot %>% filter(rank_abs<=10) %>% select(prov,tot_stranieri )) ## --------------------------------------------------- ISTAT.prov <- read_csv("province.csv", col_types = cols(`Codice provincia` = col_integer()), skip = 1) Pop.prov <-ISTAT.prov %>% filter(Età=="Totale") %>% mutate(Tot.Pop=`Totale Maschi`+`Totale Femmine`) %>% select(cod.prov="Codice provincia" , "Tot.Pop" ) IMMprovince.pop<- IMMprovince.tot %>% full_join(Pop.prov, by="cod.prov") %>% mutate(prop.str= 100*tot_stranieri/Tot.Pop) ## --------------------------------------------------- IMMprovince.pop<- IMMprovince.pop %>% arrange(-prop.str) %>% mutate(rank_rel=row_number()) kable(IMMprovince.pop %>% filter(rank_rel<=10) %>% select(prov,prop.str )) ## --------------------------------------------------- IMMprovince.citt<- IMMprovince %>% group_by(citt) %>% summarize(tot_stranieri=sum(totale)) %>% ungroup() %>% arrange(-tot_stranieri) %>% mutate(rank_citt=row_number()) kable(IMMprovince.citt %>% filter(rank_citt<=10) %>% select(citt,tot_stranieri )) ## --------------------------------------------------- IMMprovince_wide<- IMMprovince %>% filter(citt %in% c("Romania", "Albania", "Marocco", "Cinese, Repubblica Popolare", "Ucraina")) %>% select(cod.prov, prov, totale, citt) %>% pivot_wider( names_from=citt, values_from=totale) %>% mutate(cod.prov=as.numeric(cod.prov)) ## --------------------------------------------------- library(sf) library(tmap) geo_prov<-st_read("shapefileProvince/ProvCM01012018_g_WGS84.shp") geo_prov<- geo_prov %>% mutate(cod.prov=COD_PROV) %>% left_join(IMMprovince_wide , by="cod.prov") ## --------------------------------------------------- tm_shape(geo_prov) + tm_polygons(col="Romania", palette="Greens", style="cont") ## --------------------------------------------------- tm_shape(geo_prov) + tm_polygons(col="Albania", palette="Reds", style="cont") ## --------------------------------------------------- tm_shape(geo_prov) + tm_polygons(col="Marocco", palette="Blues", style="cont") ## --------------------------------------------------- tm_shape(geo_prov) + tm_polygons(col="Cinese, Repubblica Popolare", palette="Purples", style="cont") ## --------------------------------------------------- tm_shape(geo_prov) + tm_polygons(col="Ucraina", palette="Oranges", style="cont") ## --------------------------------------------------- library(migest) d0 <- read_csv(system.file("imr", "reg_flow.csv", package = "migest")) d0 ## --------------------------------------------------- d1 <- read_csv(system.file("vidwp", "reg_plot.csv", package = "migest")) d1 ## ---- out.width='40%'------------------------------- library(circlize) chordDiagram(x = d0 %>% filter(year0==1960) %>% select(orig_reg,dest_reg,flow), ) ## --------------------------------------------------- dev.off() chordDiagram(x = d0 %>% filter(year0==1960) %>% select(orig_reg,dest_reg,flow), directional = 1, order = d1$region, grid.col = d1$col1, annotationTrack = "grid", transparency = 0.25, annotationTrackHeight = c(0.05, 0.1), direction.type = c("diffHeight", "arrows"), link.arr.type = "big.arrow", diffHeight = -0.04, link.sort = TRUE, link.largest.ontop = TRUE) ## ----eval=FALSE, include=TRUE----------------------- ## circos.track(track.index = 1, bg.border = NA, panel.fun = function(x, y) { ## xlim = get.cell.meta.data("xlim") ## sector.index = get.cell.meta.data("sector.index") ## reg1 = d1 %>% filter(region == sector.index) %>% pull(reg1) ## reg2 = d1 %>% filter(region == sector.index) %>% pull(reg2) ## ## circos.text(x = mean(xlim), y = ifelse(is.na(reg2), 3, 4), ## labels = reg1, facing = "bending", cex = 1.1) ## circos.text(x = mean(xlim), y = 2.75, labels = reg2, facing = "bending", cex = 1.1) ## circos.axis(h = "top", labels.cex = 0.8, ## labels.niceFacing = FALSE, labels.pos.adjust = FALSE) ## }) ## --------------------------------------------------- library(tweenr) d2 <- d0 %>% mutate(corridor = paste(orig_reg, dest_reg, sep = " -> ")) %>% select(corridor, year0, flow) %>% mutate(ease = "linear") %>% tween_elements(time = "year0", group = "corridor", ease = "ease", nframes = 100) %>% tbl_df() ## --------------------------------------------------- d2 <- d2 %>% separate(col = .group, into = c("orig_reg", "dest_reg"), sep = " -> ") %>% select(orig_reg, dest_reg, flow, everything()) %>% mutate(flow = flow/1e06) d2 ## ----eval=FALSE, include=TRUE----------------------- ## dev.off() ## dir.create("./plot-gif/") ## ## library(circlize) ## for(f in unique(d2$.frame)){ ## # open a PNG plotting device ## png(file = paste0("./plot-gif/globalchord", f, ".png"), height = 7, width = 7, ## units = "in", res = 500) ## ## # initialise the circos plot ## circos.clear() ## par(mar = rep(0, 4), cex=1) ## circos.par(start.degree = 90, track.margin=c(-0.1, 0.1), ## gap.degree = 4, points.overflow.warning = FALSE) ## ## # plot the chord diagram ## chordDiagram(d2 %>% ## filter( .frame == f) %>% ## select(orig_reg,dest_reg,flow ), directional = 1, order = d1$region, ## grid.col = d1$col1, annotationTrack = "grid", ## transparency = 0.25, annotationTrackHeight = c(0.05, 0.1), ## direction.type = c("diffHeight", "arrows"), link.arr.type = "big.arrow", ## diffHeight = -0.04, link.sort = TRUE, link.largest.ontop = TRUE) ## ## # add labels and axis ## circos.track(track.index = 1, bg.border = NA, panel.fun = function(x, y) { ## xlim = get.cell.meta.data("xlim") ## sector.index = get.cell.meta.data("sector.index") ## reg1 = d1 %>% filter(region == sector.index) %>% pull(reg1) ## reg2 = d1 %>% filter(region == sector.index) %>% pull(reg2) ## ## circos.text(x = mean(xlim), y = ifelse(is.na(reg2), 3, 4), ## labels = reg1, facing = "bending", cex = 1.1) ## circos.text(x = mean(xlim), y = 2.75, labels = reg2, facing = "bending", cex = 1.1) ## circos.axis(h = "top", labels.cex = 0.8, ## labels.niceFacing = FALSE, labels.pos.adjust = FALSE) ## }) ## ## text(-.7,1,glue('Migrant Population: {d2 %>% filter(.frame==f) %>% select(year0) %>% summarize(mean(year0)) %>% round(0)} ')) ## text(.8,1,glue('{d2 %>% filter(.frame==f) %>% select(flow) %>% summarize(sum(flow)) %>% round(1)} million')) ## # close plotting device ## dev.off() ## } ## ## ----eval=FALSE, include=TRUE----------------------- ## library(magick) ## ## img <- image_read(path = "./plot-gif/globalchord0.png") ## for(f in unique(d2$.frame)[-1]){ ## img0 <- image_read(path = paste0("./plot-gif/globalchord",f,".png")) ## img <- c(img, img0) ## message(f) ## } ## ## img1 <- image_scale(image = img, geometry = "720x720") ## ## ## ani0 <- image_animate(image = img1, fps = 10) ## image_write(image = ani0, path = "./plot-gif/globalchord.gif") ##