Commit 6034adbc authored by Nicolas Lambert's avatar Nicolas Lambert
Browse files

debug & design

parent 6c65d6e4
File added
This diff is collapsed.
......@@ -3,7 +3,7 @@
source("helpers.R")
ui <- fluidPage(
titlePanel("MigrexploreR 3 - International migrant stock in 2019 (Beta)"),
titlePanel("INTERNATIONAL MIGRANT STOCK IN 2019"),
useShinyjs(),
sidebarLayout(
sidebarPanel(width = 2,
......@@ -16,18 +16,21 @@ ui <- fluidPage(
choices = isolist,
selected = "FRA"),
sliderInput("threshold",
label = "Threshold",
min = 0, max = 100000, value = 10000),
label = "Threshold (number of people in the smallest flow represented)",
min = 0, max = 200000, value = 10000, step = 1000, ticks = FALSE),
sliderInput("k",
label = "Flow thickness",
min = 0, max = 1, value = 0.3),
checkboxInput("auto", "Auto Size", TRUE),
label = "Manuel flow thickness (to ensure maps comparability)",
min = 0, max = 10, value = 2, step = 0.1, ticks = FALSE),
checkboxInput("auto", "Auto Size (non-comparable maps)", TRUE),
helpText('This application is part of a set of tools called "MigrExplorer" that allow to visualize international migrations in various aspects.'),
helpText("Data sources: United Nations, Department of Economic and Social Affairs, Population Division, 2019 - Natural Earth, 2020."),
helpText("By Françoise Bahoken & Nicolas Lambert, 2020."),
tags$br(),
tags$a(img(src='gitlab.png', align = "center", height="30px"), href = "https://gitlab.huma-num.fr/nlambert/migrexplorer3",target="_blank"),
tags$a(img(src='hypotheses.png', align = "center", height="30px"), href = "https://neocarto.hypotheses.org",target="_blank"),
tags$a(img(src='ccbysanc.png', align = "center", height="30px"), href = "https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode",target="_blank")
tags$a(img(src='ccbysanc.png', align = "center", height="30px"), href = "https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode",target="_blank"),
tags$hr(),
tags$div(class = "total")
),
......@@ -46,7 +49,7 @@ server <- function(input, output, session) {
disable("k")
output$d3 <- renderD3({
output$d3 <- renderD3({
json <- getflows(input$iso, input$type, input$threshold, NULL)
r2d3(
data = json,
......@@ -61,7 +64,7 @@ server <- function(input, output, session) {
} else {
enable("k")
enable("k")
output$d3 <- renderD3({
json <- getflows(input$iso, input$type, input$threshold, input$k)
......
......@@ -18,8 +18,7 @@
}
.flows {
fill: #ff6200;
fill-opacity: 0.9;
fill-opacity: 0.8;
stroke: #8a4d74;
stroke-width:0.2;
}
......@@ -46,6 +45,14 @@
alignment-baseline: middle;
}
h2{
font-weight: bold;
color: #63b0af;
}
.well{ background-color: #63b0af;
}
.maptitle2 {
font-family: 'Open Sans', sans-serif;
fill: #90a0a3;
......@@ -57,6 +64,13 @@
line-height: 1;
}
.help-block{
color:white;
font-size: 12px;
}
.note {
font-family: 'Open Sans', sans-serif;
fill: #90a0a3;
......@@ -66,20 +80,48 @@
.txthelp {
font-family: 'Open Sans', sans-serif;
fill: white;
font-size: 10x;
}
.stats {
font-family: 'Open Sans', sans-serif;
fill: white;
font-size: 40x;
font-size: 40px;
}
.enhance{
font-size: 22px;
font-weight: bold;
}
.minimize{
font-size: 11px;
font-style: italic;
}
div.tooltip {
width: 120px;
background-color: black;
.tooltip {
width: 200px;
background-color: #63b0af;
color: #fff;
text-align: center;
padding: 5px 0;
margin: 5px;
border-radius: 6px;
font-size: 14px;
}
.total {
width: 200px;
background-color: #63b0af;
position: relative;
top: 0px;
left: 0px;
color: #fff;
text-align: center;
padding: 5px 0;
margin: 5px;
border-radius: 6px;
}
......@@ -36,20 +36,59 @@ library(r2d3)
getflows <- function(iso, type, threshold, k) {
center <- st_centroid(ctr[ctr$id==iso,])
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,] }
if (type == "to"){ m <- migr[migr$j == iso,]}
m <- m[m$fij >= threshold,]
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)){
if (nrow(m) == 1){
m <- rbind(m,m)
}
......@@ -68,21 +107,24 @@ library(r2d3)
ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
ctr <- st_wrap_dateline(ctr, options = "WRAPDATELINE=YES", quiet = TRUE)
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
flows <- st_wrap_dateline(flows, options = "WRAPDATELINE=YES", quiet = TRUE)
flows <- st_wrap_dateline(flows, c("WRAPDATELINE=YES","DATELINEOFFSET=180"), quiet = TRUE)
flows <- st_buffer(flows,0.01)
if (type == "from"){ flows <- merge(flows, dfctr, by.x = "j", by.y = "id")}
if (type == "to"){ flows <- merge(flows, dfctr, by.x = "i", by.y = "id")}
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)
return(list(flows.json,countries.json, center.json))
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))
}
shiny::runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
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
center <- st_centroid(ctr[ctr$id==iso,], of_largest_polygon = TRUE)
coords <- st_coordinates(center)
coords
runApp('~/Documents/R/migrexplorer3')
coords[1]
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
runApp('~/Documents/R/migrexplorer3')
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 = "USA"
type = "from" # or to
threshold <- 100000
k = 0.3
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 = "USA"
type = "from" # or to
threshold <- 100000
k = 0.3
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"
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
}
m <- m[m$fij >= threshold,]
m$i <- as.character(m$i)
m$j <- as.character(m$j)
m
if(is.null(k)){
k <- 700000 / max(m$fi)
}
if (nrow(m == 1)){
m <- rbind(m,m)
}
flows <- plotflows(
x = countries,
xid = "id",
df = m,
dfid = c("i","j"),
dfvar = "fij",
k = k,
plot = FALSE
)[[3]]
plot(st_geometry(flows))
runApp('~/Documents/R/migrexplorer3')
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 = "USA"
type = "from" # or to
threshold <- 20000
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"
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
}
m <- m[m$fij >= threshold,]
m$i <- as.character(m$i)
m$j <- as.character(m$j)
m
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]]
plot(st_geometry(flows))
plot(st_geometry(flows), col="red")
ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
ctr <- st_wrap_dateline(ctr, options = "WRAPDATELINE=YES", quiet = TRUE)
plot(st_geometry(ctr), col="red")
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
flows <- st_wrap_dateline(flows, options = "WRAPDATELINE=YES", quiet = TRUE)
plot(st_geometry(flows), col="red")
flows <- st_buffer(flows,0.01)
plot(st_geometry(flows), col="red")
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 = "USA"
type = "from" # or to
threshold <- 20000
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"
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
}
m <- m[m$fij >= threshold,]
m$i <- as.character(m$i)
m$j <- as.character(m$j)
m
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]]
plot(st_geometry(flows), col="red")
ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
ctr <- st_wrap_dateline(ctr, options = "WRAPDATELINE=YES", quiet = TRUE)
plot(st_geometry(ctr), col="red")
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
flows <- st_buffer(flows,0.01)
flows <- st_wrap_dateline(flows, options = "WRAPDATELINE=YES", quiet = TRUE)
plot(st_geometry(flows), col="red")
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 = "USA"
type = "from" # or to
threshold <- 20000
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"
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
}
m <- m[m$fij >= threshold,]
m$i <- as.character(m$i)
m$j <- as.character(m$j)
m
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]]
plot(st_geometry(flows), col="red")
ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
ctr <- st_wrap_dateline(ctr, options = "WRAPDATELINE=YES", quiet = TRUE)
plot(st_geometry(ctr), col="red")
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
flows <- st_wrap_dateline(flows, options = "WRAPDATELINE=YES", quiet = TRUE)
#flows <- st_buffer(flows,0.01)
plot(st_geometry(flows), col="red")
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 = "USA"
type = "from" # or to
threshold <- 20000
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"
}
if (type == "to"){