Commit 53a0125e authored by Nicolas Lambert's avatar Nicolas Lambert
Browse files

update

parent 4232f5aa
library(sf)
library(geojsonsf)
library(flowmapper)
library(shiny)
library(shinyjs)
library(r2d3)
# IMPORT
migr <- read.csv("data/migr2019_T.csv")
ctr <- st_read("data/countries.gpkg")
ctr <- ctr[,c("adm0_a3_is" ,"label","geom")]
colnames(ctr) <- c("id","name","geom")
# List countries
isolist <- as.character(ctr$id)
names(isolist) <- ctr$name
dfctr <- data.frame(isolist,names(isolist))
colnames(dfctr) <- c("id","label")
# VARS
# #
iso = "FIN"
type = "from" # or to
threshold <- 100000
k = 0.3
# PROJ
# crs <- "+proj=aeqd +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
# GET FLOWS
getflows <- function(iso, type, threshold, k) {
center <- st_centroid(ctr[ctr$id==iso,], of_largest_polygon = TRUE)
coords <- st_coordinates(center)
crs <- paste0("+proj=aeqd +lat_0=",coords[2]," +lon_0=",coords[1]," +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
countries <- st_transform(ctr,crs)
if (type == "from"){
m <- migr[migr$i == iso,]
flowcol <- "#ff6200"
sentence <- paste0("people from ",dfctr[dfctr == iso,"label"]," are living abroad in 2019.")
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
sentence <- paste0("migrants are living in ",dfctr[dfctr == iso,"label"] , " in 2019.")
}
getunit <- function(t){
if(t >= 0){
total <- t
unit <- ""
}
if(t >= 1000){
total <- round(t/1000,2)
if(total >= 2){unit <- "thousands"} else {unit <- "thousand"}
}
if(t >= 1000000 ){
total <- round(t/1000000,2)
if(total >= 2){unit <- "millions"} else {unit <- "million"}
}
return(paste0(total," ",unit))
}
total <- getunit(sum(m$fij))
total <- paste0("<span class='enhance'>",total,"</span><br/>",sentence)
m <- m[m$fij >= threshold,]
total <- paste0(total," <span class='minimize'><br/>(",getunit(sum(m$fij))," on the map)</span>")
m$i <- as.character(m$i)
m$j <- as.character(m$j)
if(is.null(k)){
k <- 700000 / max(m$fi)
}
if (nrow(m) == 1){
m <- rbind(m,m)
}
if (nrow(m > 0)){
flows <- plotflows(
x = countries,
xid = "id",
df = m,
dfid = c("i","j"),
dfvar = "fij",
k = k,
plot = FALSE
)[[3]]
ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
flows <- st_wrap_dateline(flows, c("WRAPDATELINE=YES","DATELINEOFFSET=180"), quiet = TRUE)
flows <- st_buffer(flows,0.01)
flows <- merge(flows, dfctr, by.x = "i", by.y = "id")
flows <- merge(flows, dfctr, by.x = "j", by.y = "id")
flows <- flows[order(flows$height, decreasing = TRUE),]
colnames(flows)[14:15] <- c("label1","label2")
flows.json <- sf_geojson(flows, simplify = TRUE)
} else {flows.json <- 0}
countries.json <- sf_geojson(ctr, simplify = FALSE)
center.json <- sf_geojson(center, simplify = FALSE)
if(coords[1] >= 0){coords[1] <- coords[1] - 20} else {coords[1] <- coords[1] + 20}
if(coords[2] >= 0){coords[2] <- coords[2] - 20} else {coords[2] <- coords[2] + 20}
return(list(flows.json, countries.json, center.json, flowcol, coords[1], coords[2],total))
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment