Skip to content

Commit

Permalink
Update gganimate
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkeyes committed Nov 4, 2024
1 parent 9d5ccba commit e93c50b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
Binary file added gganimate/countries-small-multiples.pdf
Binary file not shown.
115 changes: 115 additions & 0 deletions gganimate/gganimate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
library(refugees)

population

library(tidyverse)

refugees_data <-
read_rds("https://github.com/rfortherestofus/blog/raw/refs/heads/main/gganimate/refugees_data.rds")

refugees_data

library(scales)
library(hrbrthemes)
library(gganimate)

refugees_data |>
filter(country_abbreviation == "SYR") |>
ggplot(
aes(
x = year,
y = refugees_as_pct
)
) +
geom_line() +
scale_y_continuous(
labels = percent_format()
) +
labs(
x = NULL,
y = NULL,
title = "The refugee population in Syria increased\ndramatically as a result of its civil war"
) +
theme_ipsum_inter(base_size = 12) +
theme(panel.grid.minor = element_blank())


syria_refugees_plot <- last_plot()

syria_refugees_plot +
transition_reveal(year)

library(sf)



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



countries_data



middle_east_refugees_data <-
countries_data |>
left_join(
refugees_data,
join_by(country_abbreviation)
) |>
filter(subregion == "Western Asia") |>
select(year, country_abbreviation, refugees_as_pct)

middle_east_refugees_data

middle_east_refugees_data |>
ggplot() +
geom_sf(
aes(fill = refugees_as_pct),
linewidth = 0.1,
color = "white"
) +
scale_fill_viridis_c(
labels = percent_format(),
limits = c(0, .4),
option = "C",
guide = guide_colorsteps(show.limits = TRUE)
) +
labs(
title = "Refugees as a percentage of total population",
fill = NULL
) +
theme_ipsum_inter(
grid = FALSE
) +
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
)
)



middle_east_refugees_map <- last_plot()


middle_east_refugees_map +
facet_wrap(vars(year))

middle_east_refugees_map +
transition_manual(year)


middle_east_refugees_map +
labs(title = "Refugees as a percentage of total population in {current_frame}") +
transition_manual(year)

4 changes: 2 additions & 2 deletions gganimate/gganimate.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Refugees animation
David Keyes
2024-01-17
2024-11-27

## Data

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}. Animated plots are a great way to capture attention, especially online where the next shiny object is just a scroll away. If you want to get attention on your data, using animation with a package like {gganimate} is a great option.

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](https://youtu.be/CN6FVID5ydU) and information management officer Cédric Vidonne. TODO: Add link
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](https://rfortherestofus.com/2024/03/podcast-episode-13) and information management officer Cédric Vidonne. TODO: Add link

## Import Data

Expand Down
4 changes: 2 additions & 2 deletions gganimate/gganimate.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Refugees animation"
title: "An introduction to the {gganimate} package"
author: David Keyes
date: 11/27/2024
toc: false
Expand Down Expand Up @@ -82,7 +82,7 @@ countries_data |>

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}. Animated plots are a great way to capture attention, especially online where the next shiny object is just a scroll away. If you want to get attention on your data, using animation with a package like {gganimate} is a great option.

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](https://youtu.be/CN6FVID5ydU) and information management officer Cédric Vidonne. TODO: Add link
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](https://rfortherestofus.com/2024/03/podcast-episode-13) and information management officer Cédric Vidonne. TODO: Add link

## Import Data

Expand Down

0 comments on commit e93c50b

Please sign in to comment.