Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkeyes committed Nov 4, 2024
1 parent 1449d96 commit b2a1a2f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
47 changes: 38 additions & 9 deletions gganimate/gganimate.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,55 @@ David Keyes

## 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). [Their package](https://www.unhcr.org/refugee-statistics/insights/explainers/refugees-r-package.html), called {refugees}, has data on a number of different datasets related to refugee populations. I’ll use one of these datasets, called `population`, which has “data on forcibly displaced and stateless persons by year, including refugees, asylum-seekers, internally displaced people (IDPs) and stateless people.” I begin by loading the {refugees} package:

``` r
library(refugees)
```

And then showing the `population` data:

``` r
population
#> # A tibble: 126,402 × 16
#> year coo_name coo coo_iso coa_name coa coa_iso refugees asylum_seekers
#> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 1951 Unknown UKN UNK Australia AUL AUS 180000 0
#> 2 1951 Unknown UKN UNK Austria AUS AUT 282000 0
#> 3 1951 Unknown UKN UNK Belgium BEL BEL 55000 0
#> 4 1951 Unknown UKN UNK Canada CAN CAN 168511 0
#> 5 1951 Unknown UKN UNK Denmark DEN DNK 2000 0
#> 6 1951 Unknown UKN UNK France FRA FRA 290000 0
#> 7 1951 Unknown UKN UNK United Ki… GBR GBR 208000 0
#> 8 1951 Unknown UKN UNK Germany GFR DEU 265000 0
#> 9 1951 Unknown UKN UNK Greece GRE GRC 18000 0
#> 10 1951 Unknown UKN UNK China, Ho… HKG HKG 30000 0
#> # ℹ 126,392 more rows
#> # ℹ 7 more variables: returned_refugees <dbl>, idps <dbl>, returned_idps <dbl>,
#> # stateless <dbl>, ooc <dbl>, oip <dbl>, hst <dbl>
```

``` r

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

## Data

``` r
refugees_data <-
read_rds("refugees_data.rds") # TODO: Switch to web URL
read_rds("https://github.com/rfortherestofus/blog/raw/refs/heads/main/gganimate/refugees_data.rds")
```

``` r
refugees_data_geospatial <-
read_sf("refugees_data_geospatial.geojson")
read_sf("https://github.com/rfortherestofus/blog/raw/refs/heads/main/gganimate/refugees_data_geospatial.geojson")
```

## Plot
Expand All @@ -47,11 +78,10 @@ refugees_data |>
) +
theme_ipsum_inter() +
theme(panel.grid.minor = element_blank()) +
transition_reveal(year) +
ease_aes("linear")
transition_reveal(year)
```

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

## Map

Expand Down Expand Up @@ -96,8 +126,7 @@ refugees_data_geospatial |>
size = 12
)
) +
transition_manual(year) +
ease_aes("linear")
transition_manual(year)
```

<img src="gganimate_files/figure-commonmark/unnamed-chunk-6-1.gif" style="width:100.0%" />
<img src="gganimate_files/figure-commonmark/unnamed-chunk-8-1.gif" style="width:100.0%" />
29 changes: 25 additions & 4 deletions gganimate/gganimate.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ editor_options:
```{r}
#| echo: false
#| eval: false
# This code chunk is where I import the data, but I'm not showing it in the blog post
library(tidyverse)
library(refugees)
library(wbstats)
Expand Down Expand Up @@ -77,26 +80,44 @@ refugees_data_geospatial |>

## 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
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}. In this blog post, I'll show how to use the {gganimate} package by animating data on refugees. As part of our [one percent for people, one percent for the planet giving program](https://rfortherestofus.com/1percent), we regularly support the work of the United Nations High Commissioner for Refugees (UNHCR). If you want to learn more about how the UNHCR uses R, please see our interviews with UNHCR statistician Ahmadou Dicko and information management officer Cédric Vidonne. TODO: Add links

To show how {gganimate} works, let's use data from UNHCR. [Their package](https://www.unhcr.org/refugee-statistics/insights/explainers/refugees-r-package.html), called {refugees}, has data on a number of different datasets related to refugee populations. I'll use one of these datasets, called `population`, which has "data on forcibly displaced and stateless persons by year, including refugees, asylum-seekers, internally displaced people (IDPs) and stateless people."

I begin by loading the {refugees} package:

```{r}
library(refugees)
```

And then showing the `population` data:

```{r}
population
```




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

## Data

```{r}
refugees_data <-
read_rds("refugees_data.rds") # TODO: Switch to web URL
read_rds("https://github.com/rfortherestofus/blog/raw/refs/heads/main/gganimate/refugees_data.rds")
```

```{r}
refugees_data_geospatial <-
read_sf("refugees_data_geospatial.geojson")
read_sf("https://github.com/rfortherestofus/blog/raw/refs/heads/main/gganimate/refugees_data_geospatial.geojson")
```

## Plot
Expand Down
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.

0 comments on commit b2a1a2f

Please sign in to comment.