Skip to content

Commit

Permalink
Merge pull request #41 from JosephBARBIERDARNAL/refugees
Browse files Browse the repository at this point in the history
draft animate refugees
  • Loading branch information
dgkeyes authored Nov 4, 2024
2 parents 9491d91 + 9ed587e commit c16f5b8
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 0 deletions.
68 changes: 68 additions & 0 deletions gganimate/anim.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
library(tidyverse)
library(gganimate)
library(ggplot2)
library(scales)
library(refugees)
library(sf)

geodata <- read_sf("refugees-animation/countries.geojson") %>%
filter(ADMIN != "Antarctica")

world_pop <- read_csv("refugees-animation/population-and-demography.csv", show_col_types = FALSE)

all_countries_years <- expand.grid(
year = unique(population$year),
coa_iso = unique(geodata$ISO_A3)
)

refugee_data <- population %>%
select(year, coa_name, coa_iso, refugees) %>%
group_by(year, coa_name, coa_iso) %>%
summarise(refugees = sum(refugees, na.rm = TRUE)) %>%
ungroup() %>%
right_join(all_countries_years, by = c("year", "coa_iso")) %>%
mutate(refugees = ifelse(is.na(refugees), NA, refugees))

map_data <- geodata %>%
left_join(refugee_data, by = c("ISO_A3" = "coa_iso"), relationship = "many-to-many") %>%
select(geometry, year, coa_name, refugees, ISO_A3) %>%
left_join(world_pop, by = c("year" = "Year", "ISO_A3" = "Code")) %>%
mutate(refugee_per_cap = refugees / Population * 100)

p <- ggplot(map_data, aes(fill = refugee_per_cap)) +
geom_sf(linewidth = 0.2, color = "black") +
scale_fill_gradientn(
colours = c("blue", "red"),
na.value = "lightgrey",
limits = range(map_data$refugee_per_cap, na.rm = TRUE),
name = "Refugees per capita",
labels = comma
) +
theme_void() +
labs(
title = "Evolution of Refugees/capita Over Time",
subtitle = "Year: {current_frame}",
fill = "Refugees"
) +
theme(
legend.position = "bottom",
legend.key.width = unit(2, "cm"),
legend.title = element_text(size = 12, face = "bold"),
legend.text = element_text(size = 10),
plot.title = element_text(hjust = 0.5, size = 20),
plot.subtitle = element_text(hjust = 0.5, size = 16)
) +
coord_sf(crs = st_crs(3857))



animated_map <- p +
transition_manual(year)

anim_save(
"refugees-animation/animated_refugee_map.gif",
animated_map,
fps = 5,
width = 1200, height = 800, res = 150, # Higher resolution
renderer = gifski_renderer(loop = TRUE)
)
Binary file added gganimate/animated_refugee_map.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions gganimate/gganimate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Refugees animation
David Keyes
2024-01-17

## Data

## Intro

``` r
library(tidyverse)
library(gganimate)
library(sf)
library(scales)
library(hrbrthemes)
```

## Data

``` r
refugees_data <-
read_rds("refugees_data.rds") # TODO: Switch to web URL
```

``` r
refugees_data_geospatial <-
read_sf("refugees_data_geospatial.geojson")
```

## Plot

``` r
refugees_data |>
filter(country_abbreviation == "SYR") |>
ggplot(
aes(
x = year,
y = refugees_as_pct
)
) +
scale_y_continuous(
labels = percent_format()
) +
geom_line() +
labs(
x = NULL,
y = NULL
) +
theme_ipsum_inter() +
theme(panel.grid.minor = element_blank()) +
transition_reveal(year) +
ease_aes("linear")
```

<img src="gganimate_files/figure-commonmark/unnamed-chunk-5-1.gif" style="width:100.0%" />

## Map

``` r
refugees_data_geospatial |>
left_join(
refugees_data,
join_by(country_abbreviation)
) |>
filter(continent == "Asia") |>
filter(subregion == "Western Asia") |>
ggplot() +
geom_sf(
aes(fill = refugees_as_pct),
linewidth = 0.1,
color = "white"
) +
theme_ipsum_inter(
grid = FALSE
) +
labs(
title = "Refugees as a percentage of total population in {current_frame}",
# caption = "{current_frame}",
fill = NULL
) +
scale_fill_viridis_c(
labels = scales::percent_format(),
limits = c(0, .4),
option = "C",
guide = guide_colorsteps(show.limits = TRUE)
) +
theme(
legend.position = "top",
plot.title = element_text(
face = "bold",
hjust = 0.5
),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.key.width = unit(2, "cm"),
legend.text = element_text(
size = 12
)
) +
transition_manual(year) +
ease_aes("linear")
```

<img src="gganimate_files/figure-commonmark/unnamed-chunk-6-1.gif" style="width:100.0%" />
170 changes: 170 additions & 0 deletions gganimate/gganimate.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
title: "Refugees animation"
author: David Keyes
date: 1/17/2024
toc: false
format: gfm
wrap: none
execute:
warning: false
message: false
knitr:
opts_chunk:
out.width: 100%
fig.height: 8
dpi: 300
collapse: true
comment: "#>"
warning: false
message: false
editor_options:
chunk_output_type: console
---

## Data {.unnumbered .hidden}

```{r}
#| echo: false
#| eval: false
library(tidyverse)
library(refugees)
library(wbstats)
library(rnaturalearth)
library(here)
library(sf)
refugees <-
population |>
rename(country_abbreviation = coo_iso) |>
group_by(year, country_abbreviation) |>
summarise(number_of_refugees = sum(refugees, na.rm = TRUE)) |>
ungroup() |>
filter(year >= 2000) |>
arrange(country_abbreviation)
country_population <-
wb_data("SP.POP.TOTL", start_date = 2000, end_date = 2023) |>
select(date, iso3c, SP.POP.TOTL) |>
set_names("year", "country_abbreviation", "total_population") |>
labelled::remove_var_label()
refugees_data <-
refugees |>
left_join(
country_population,
join_by(year, country_abbreviation)
) |>
mutate(refugees_as_pct = number_of_refugees / total_population) |>
select(year, country_abbreviation, total_population, number_of_refugees, refugees_as_pct) |>
complete(year, country_abbreviation) |>
arrange(desc(refugees_as_pct))
refugees_data |>
write_rds(here("gganimate/refugees_data.rds"))
refugees_data_geospatial <-
ne_countries() |>
select(iso_a3, continent, subregion) |>
rename(country_abbreviation = iso_a3)
refugees_data_geospatial |>
st_write(
here("gganimate/refugees_data_geospatial.geojson"),
driver = "GeoJSON",
delete_dsn = TRUE # This overwrites existing file if present
)
```

## Intro

The {gganimate} package is the easiest way to make animated plots in R. If you know how to make plots in ggplot, you can animate them with {gganimate}. To show how {gganimate} works, let's use data from the United Nations High Commissioner for Refugees (UNHCR). Th

```{r}
library(tidyverse)
library(gganimate)
library(sf)
library(scales)
library(hrbrthemes)
```

## Data

```{r}
refugees_data <-
read_rds("refugees_data.rds") # TODO: Switch to web URL
```

```{r}
refugees_data_geospatial <-
read_sf("refugees_data_geospatial.geojson")
```

## Plot

```{r}
refugees_data |>
filter(country_abbreviation == "SYR") |>
ggplot(
aes(
x = year,
y = refugees_as_pct
)
) +
scale_y_continuous(
labels = percent_format()
) +
geom_line() +
labs(
x = NULL,
y = NULL
) +
theme_ipsum_inter() +
theme(panel.grid.minor = element_blank()) +
transition_reveal(year)
```

## Map

```{r}
refugees_data_geospatial |>
left_join(
refugees_data,
join_by(country_abbreviation)
) |>
filter(continent == "Asia") |>
filter(subregion == "Western Asia") |>
ggplot() +
geom_sf(
aes(fill = refugees_as_pct),
linewidth = 0.1,
color = "white"
) +
theme_ipsum_inter(
grid = FALSE
) +
labs(
title = "Refugees as a percentage of total population in {current_frame}",
# caption = "{current_frame}",
fill = NULL
) +
scale_fill_viridis_c(
labels = scales::percent_format(),
limits = c(0, .4),
option = "C",
guide = guide_colorsteps(show.limits = TRUE)
) +
theme(
legend.position = "top",
plot.title = element_text(
face = "bold",
hjust = 0.5
),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
legend.key.width = unit(2, "cm"),
legend.text = element_text(
size = 12
)
) +
transition_manual(year)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gganimate/refugees_data.rds
Binary file not shown.
184 changes: 184 additions & 0 deletions gganimate/refugees_data_geospatial.geojson

Large diffs are not rendered by default.

Binary file added gganimate/refugees_data_geospatial.rds
Binary file not shown.

0 comments on commit c16f5b8

Please sign in to comment.