Created
February 26, 2019 08:14
-
-
Save djnavarro/738b7ccef613011876f7c49e46df14b4 to your computer and use it in GitHub Desktop.
draws the gganimate gapminder example, using bbc style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # packages | |
| library(tidyverse) | |
| library(gganimate) | |
| library(gapminder) | |
| library(scales) | |
| # modified version of the bbc_style function | |
| # adapted from: https://github.com/bbc/bbplot | |
| bbc_style2 <- function(font = "Helvetica") { | |
| theme( | |
| plot.title = element_text(family = font, size = 28, face = "bold", color = "#222222"), | |
| plot.subtitle = element_text(family = font, size = 22, margin = margin(9, 0, 9, 0)), | |
| plot.caption = element_blank(), | |
| legend.position = "top", | |
| legend.text.align = 0, | |
| legend.background = element_blank(), | |
| legend.title = element_blank(), | |
| legend.key = element_blank(), | |
| legend.text = element_text(family = font, size = 18, color = "#222222"), | |
| axis.title = element_text(family = font, size = 18, color = "#222222"), | |
| axis.text = element_text(family = font, size = 18, color = "#222222"), | |
| axis.text.x = element_text(margin = margin(5, b = 10)), | |
| axis.ticks = element_blank(), | |
| axis.line = element_blank(), | |
| panel.grid.minor = element_blank(), | |
| panel.grid.major.y = element_line(color = "#cbcbcb"), | |
| panel.grid.major.x = element_blank(), | |
| panel.background = element_blank(), | |
| strip.background = element_rect(fill = "white"), | |
| strip.text = element_text(size = 22, hjust = 0)) | |
| } | |
| # gapminder animation | |
| p <- gapminder %>% | |
| ggplot(aes( | |
| x = gdpPercap, | |
| y = lifeExp, | |
| size = pop, | |
| colour = country)) + | |
| geom_point(alpha = 0.7, show.legend = FALSE) + | |
| scale_colour_manual(values = country_colors) + | |
| scale_size(range = c(2, 12)) + | |
| scale_x_log10(labels = comma) + | |
| facet_wrap(~continent, nrow = 1) + | |
| bbc_style2() + | |
| theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
| ggtitle("Year: {frame_time}") + | |
| xlab("GDP per capita") + | |
| ylab("Life expectancy") + | |
| transition_time(year) + | |
| ease_aes('linear') | |
| # animate | |
| animate(p, type="cairo", detail = 5, width = 1000, height = 400) | |
| anim_save("~/../Desktop/bb_gapminder.gif") |
Author
djnavarro
commented
Feb 26, 2019

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment