Skip to content

Instantly share code, notes, and snippets.

@szechno
Last active December 2, 2025 16:58
Show Gist options
  • Select an option

  • Save szechno/84b4c5cbacb387adba1ae3fcd018ea97 to your computer and use it in GitHub Desktop.

Select an option

Save szechno/84b4c5cbacb387adba1ae3fcd018ea97 to your computer and use it in GitHub Desktop.
library(mapgl)
library(sf)
library(stringr)
library(dplyr)
library(readr)
library(lubridate)
library(tidyr)
# read in data and tidy up
locations <- read_csv("./data/afghanistan.csv") |>
st_as_sf(coords = c("lon", "lat"), crs = 4326)
locations <- bind_cols(
unite(data = locations, # data source
col = "FlowMonitoringPoint", # new col name
sep = " - ", # joiner
c(name, other), # vector of cols to unite
na.rm = TRUE, # deal with NAs
remove = FALSE)) # perserve other cols
locations <- locations |>
mutate(FlowMonitoringPoint = ifelse(
FlowMonitoringPoint == "Dak - DAK", "DAK", FlowMonitoringPoint
))
people <- read_csv(paste0("./data/",
"afghanistan-flow-monitoring-counting-data-",
"10jan2024to22nov2025.csv"))
people <- people |> mutate(
ReportingDate = dmy(ReportingDate),
ReportingMonth = month(ReportingDate),
ReportingYear = year(ReportingDate),
FlowMonitoringPoint = str_squish(FlowMonitoringPoint)
) |>
rename_all(\(x) str_replace_all(x, " ", ""))
# try making a map
p_all1 <- p_all |> filter(ReportingMonth == 10,
ReportingYear == 2025)
p1 <- locations |> left_join(p_all1)
# if NA
p1 <- p1 |> filter(!is.na(Direction))
# combine same
p1 <- p1 |> group_by(FlowMonitoringPoint, Direction, rotate) |> summarise(n = sum(TotalHeadcount), .groups = "drop")
maplibre(style = carto_style("positron")) |>
fit_bounds(locations) |>
add_image("direction",
url = "./images/gx_arrow.png") |>
add_symbol_layer(
id = "direction_in",
source = p1,
icon_image = "direction",
icon_offset = c(0, 20), # rotation affects offset!
icon_size = 0.5,
icon_allow_overlap = TRUE,
icon_ignore_placement = TRUE,
icon_rotate = match_expr(
column = "rotate",
values = c("ew", "we", "sn"),
stops = c(90, 270, 180),
default = 0
),
filter = list(
"==", "Direction", "Inflow" # ie Direction == "Inflow"
),
text_field = concat("in:",get_column("n")),
text_allow_overlap = TRUE,
text_offset = c(0, -1)
) |>
add_symbol_layer(
id = "direction_out",
source = p1,
icon_image = "direction",
icon_offset = c(0, 20), # rotation affects offset!
icon_size = 0.5,
icon_allow_overlap = TRUE,
icon_ignore_placement = TRUE,
icon_rotate = match_expr(
column = "rotate",
values = c("ew", "we", "sn"),
stops = c(270, 90, 0),
default = 0
),
filter = list(
"==", "Direction", "Outflow"
),
text_field = concat("out:",get_column("n")),
text_allow_overlap = TRUE,
text_offset = c(0, 1)
) |>
add_control("<div style='padding:5px'>
<b>Cross-border mobility</b><br />
between Afghanistan and its neighbours<br />
aggregated for October 2025<br />
Data: <a href='https://data.humdata.org/'>The Humanitarian Data Exchange</a><br />
macka szechno<br />26 November 2025
</div>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment