Created
January 29, 2019 11:58
-
-
Save thomasp85/e73e249f339963304d3c2b084942d513 to your computer and use it in GitHub Desktop.
Code for gganimate talk RStudio::conf 2019
This file contains 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
--- | |
title: "gganimate cookbook code" | |
output: | |
html_document: | |
df_print: tibble | |
highlight: kate | |
css: gganimate_cookbook.css | |
--- | |
```{r setup} | |
library(gganimate) | |
dpi <- 144 | |
knitr::opts_chunk$set( | |
dpi = dpi, | |
fig.width = 687 / dpi, | |
fig.height = 633 / dpi, | |
cache = TRUE, | |
fig.retina = 1 | |
) | |
``` | |
## Example 1 | |
```{r gapminder data} | |
library(gapminder) | |
gapminder | |
p <- ggplot( | |
gapminder, | |
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() + | |
labs( | |
x = 'GDP per capita', | |
y = 'life expectancy' | |
) | |
p | |
``` | |
```{r first anim} | |
p + | |
transition_time(year) | |
``` | |
```{r labs} | |
p + | |
transition_time(year) + | |
labs(title = 'Year: {frame_time}') | |
``` | |
```{r view_follow} | |
p + | |
transition_time(year) + | |
labs(title = 'Year: {frame_time}') + | |
view_follow(fixed_y = TRUE) | |
``` | |
```{r shadow_wake, gganimate=list(detail = 10)} | |
p + | |
transition_time(year) + | |
labs(title = 'Year: {frame_time}') + | |
shadow_wake(wake_length = 0.1, | |
alpha = FALSE) | |
``` | |
```{r shadow_mark} | |
p + | |
transition_time(year) + | |
labs(title = 'Year: {frame_time}') + | |
shadow_mark(alpha = 0.3, size = 0.5) | |
``` | |
## Example 2 | |
```{r, message=FALSE} | |
library(GSODR) | |
library(dplyr) | |
copenhagen <- nearest_stations(LAT = 55.676098, | |
LON = 12.568337, | |
distance = 50) | |
austin <- nearest_stations(LAT = 30.267153, | |
LON = -97.7430608, | |
distance = 50) | |
copenhagen_temp <- get_GSOD(2018, station = copenhagen) | |
austin_temp <- get_GSOD(2018, station = austin) | |
january <- bind_rows(copenhagen_temp, austin_temp) %>% | |
filter(MONTH == '01') %>% | |
distinct(YDAY, STNID, .keep_all = TRUE) %>% | |
mutate(location = if_else(STNID %in% copenhagen, 'Copenhagen', 'Austin')) | |
select(january, YDAY, TEMP, location, STNID) | |
``` | |
```{r} | |
p <- ggplot(january) + | |
geom_line(aes(x = YDAY, y = TEMP, | |
colour = location, | |
group = STNID)) + | |
scale_colour_brewer(type = 'qual') + | |
theme(legend.position = 'bottom') + | |
labs(x = 'Day of Month', | |
y = 'Temperature (celcius)', | |
colour = NULL) | |
p | |
``` | |
```{r temp_trans_time} | |
p + | |
transition_time(YDAY) | |
``` | |
```{r temp_shadow_mark} | |
p + | |
transition_time(YDAY) + | |
shadow_mark() | |
``` | |
```{r trans_reveal} | |
p + | |
transition_reveal(YDAY) | |
``` | |
```{r point_group} | |
p + | |
transition_reveal(YDAY) + | |
geom_point(aes(x = YDAY, y = TEMP, | |
colour = location, | |
group = STNID)) | |
``` | |
```{r point_ungroup} | |
p + transition_reveal(YDAY) + | |
geom_point(aes(x = YDAY, y = TEMP, | |
colour = location, | |
group = seq_along(STNID))) | |
``` | |
```{r} | |
p <- ggplot(january) + | |
geom_smooth(aes(x = YDAY, y = TEMP, | |
colour = location)) + | |
scale_colour_brewer(type = 'qual') + | |
theme(legend.position = 'bottom') + | |
labs(x = 'Day of Month', | |
y = 'Temperature (celcius)', | |
colour = NULL) | |
p | |
``` | |
```{r reveal_stat} | |
p + | |
transition_reveal(stat(x)) | |
``` | |
# Example 3 | |
```{r eq data, message=FALSE} | |
library(lubridate) | |
# download.file('http://service.iris.edu/fdsnws/event/1/query?minmag=0&maxmag=10&orderby=time&format=geocsv&nodata=404', '~/Desktop/earthquakes.txt') | |
earthquakes <- readr::read_table( | |
'~/Desktop/earthquakes.txt', | |
sep = '|', header = TRUE, skip = 4) %>% | |
filter(Magnitude > 5) %>% | |
mutate(Time = as_date(Time)) %>% | |
filter(Time >= ymd('2018-01-01') & Time <= ymd('2018-12-31')) %>% | |
mutate(Month = factor(format(Time, '%B'), month.name)) | |
earthquakes | |
``` | |
```{r} | |
p <- ggplot(earthquakes) + | |
geom_bar(aes(x = Month, fill = stat(count))) + | |
scale_fill_distiller(palette = 'Reds', direction = 1) + | |
scale_y_continuous(expand = c(0, 0, 0.05, 0)) + | |
labs(x = NULL, y = 'Count') + | |
theme_minimal() + | |
theme(panel.grid = element_blank(), | |
panel.grid.major.y = element_line(colour = 'white'), | |
panel.ontop = TRUE, | |
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1), | |
legend.position = 'none') | |
p | |
``` | |
```{r eq_state} | |
p + | |
transition_states(Month, wrap = FALSE) + | |
shadow_mark() | |
``` | |
```{r eq_enters_grow} | |
p + | |
transition_states(Month, wrap = FALSE) + | |
shadow_mark() + | |
enter_grow() | |
``` | |
```{r eq_enters_growfade} | |
p + | |
transition_states(Month, wrap = FALSE) + | |
shadow_mark() + | |
enter_grow() + | |
enter_fade() | |
``` | |
```{r} | |
library(sf) | |
eq <- st_multipoint(cbind(earthquakes$Longitude, | |
earthquakes$Latitude)) %>% | |
st_sfc(crs = '+proj=longlat +datum=WGS84 +no_defs') %>% | |
st_sf(geometry = .) %>% | |
st_transform('+proj=robin +datum=WGS84 +no_defs +over') -> eq | |
earthquakes <- earthquakes %>% | |
mutate(x = eq$geometry[[1]][, 1], y = eq$geometry[[1]][, 2]) | |
earth <- rnaturalearth::ne_countries(returnclass = 'sf') %>% | |
sf::st_union() | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment