data(temp_carbon)
temp_anomaly = temp_carbon %>%
select(Year = year, Global = temp_anomaly, Land = land_anomaly, Ocean = ocean_anomaly) %>%
pivot_longer(Global:Ocean, names_to = "Region", values_to = "Anomaly") %>%
filter(!is.na(Anomaly))
anim_1 <- temp_anomaly %>%
ggplot(aes(x = Year, y = Anomaly, col = Region)) +
geom_line(size = 1) +
geom_hline(aes(yintercept = 0), lty = 2) +
annotate("text", x = 2005, y = -.08,
label = "20th century mean", size = 3) +
scale_color_viridis_d(option = "C", end = .75) +
labs(
title = "Temperature anomaly relative to 20th century mean",
x = "Year",
y = "Temperature anomaly (degrees C)"
) +
lab11_theme +
transition_reveal(Year)
animate(anim_1, nframes = 10)
anim_2 = temp_anomaly %>%
ggplot(aes(x = Year, y = Anomaly, col = Region)) +
geom_line(size = 1) +
geom_segment(aes(xend = 2030, yend = Anomaly), linetype = 2) +
geom_text(aes(x = 2030, label = Region), hjust = 0) +
geom_hline(aes(yintercept = 0), lty = 2) +
annotate("text", x = 2005, y = -.08,
label = "20th century mean", size = 3) +
scale_color_viridis_d(option = "C", end = .75) +
labs(
title = "Temperature anomaly relative to 20th century mean",
x = "Year",
y = "Temperature anomaly (degrees C)"
) +
xlim(1880, 2040) +
coord_cartesian(clip = 'off') +
lab11_theme +
guides(col = FALSE) +
transition_reveal(Year)
animate(anim_2, nframes = 150)
covid_wide = covid %>%
pivot_wider(., id_cols = c(geo_value, time_value), names_from = signal, values_from = value)
covid_wide %>%
ggplot(aes(x = smoothed_wearing_mask, y = smoothed_restaurant_1d, size = smoothed_cli)) +
geom_point()
anim_3 = covid_wide %>%
ggplot(aes(x = smoothed_wearing_mask, y = smoothed_restaurant_1d, size = smoothed_cli, col = smoothed_cli)) +
geom_point(alpha = .5) +
scale_color_viridis_c(option = "C", end = .75) +
lab11_theme +
theme(legend.position = "bottom") +
transition_time(time_value)
animate(anim_3, nframes = 10)
anim_4 = covid_wide %>%
ggplot(aes(x = smoothed_wearing_mask, y = smoothed_restaurant_1d, size = smoothed_cli, col = smoothed_cli)) +
geom_point(alpha = .5) +
scale_color_viridis_c(option = "C", end = .75, guide = "legend") +
lab11_theme +
theme(legend.position = "bottom") +
transition_time(time_value) +
shadow_mark(size = .1, alpha = .1) +
labs(
title = "Restaurant visits, mask-wearing, and COVID-like illness over time",
subtitle = 'Date: {frame_time}',
x = "% Wearing Mask in Public",
y = "% Visited Restaurant",
col = "% with COVID-like Illness",
size = "% with COVID-like Illness",
caption = "Source: CMU Delphi Symptom Survey"
)
animate(anim_4, nframes = 100)
# Part 3: Your turn!
library(extrafont)
## Registering fonts with R
library(ggflags)
library(ggtext)
library(countrycode)
library(knitr)
library(formatR)
windowsFonts(Helvetica = windowsFont("Helvetica")) #install font
windowsFonts()
## $serif
## [1] "TT Times New Roman"
##
## $sans
## [1] "TT Arial"
##
## $mono
## [1] "TT Courier New"
##
## $Helvetica
## [1] "Helvetica"
g8_2 <- tibble(country = toupper(c("US", "Italy", "Canada", "UK",
"Japan", "Germany", "France", "Russia")),
count = c(3.2, 0.71, 0.5, 0.1, 0, 0.2, 0.1, 0),
label = c(as.character(c(3.2, 0.71, 0.5, 0.1, 0, 0.2, 0.1)), "N/A"),
code = c("us", "it", "ca", "gb", "jp", "de", "fr", "ru"))
a <- g8_2 %>% mutate(country = reorder(country, -count)) %>%
#Presentation of bars from greatest to smallest is more aes
ggplot(aes(country, count, label = label)) +
geom_bar(stat = "identity", fill = "navy", alpha = g8_2$count) +
geom_text(nudge_y = 0.2, color = "navy", size = 5) +
geom_flag(y = -.45, aes(country = code), size = 13) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4), limits = c(0,4)) +
geom_text(aes(6.9, 3.8,
label = "Source: UNODC Homicide Statistics"),
hjust = 0.6, color = "grey35", fontface = 3) +
ggtitle("NUMBER OF <span style='color:#000080'>GUN-RELATED HOMICIDES
</span> PER 100,000 PEOPLE IN G-8 COUNTRIES") +
xlab("") +
ylab("") +
ggthemes::theme_hc() +
theme(plot.background = element_rect(fill = "lightcyan",
colour = "grey",
size = 0.5, linetype = "solid")) +
theme(plot.title = element_markdown(size = 9.5, hjust = 0.75,
vjust = 6, face = "bold", fill = "slategray"),
axis.text.x = element_text(size = 8, vjust = -18.5, face = "bold"),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.line.x = element_blank(),
text = element_text(family = "Helvetica"),
plot.margin = unit(c(1,1,1,1), "cm"))
a + transition_states(country, wrap = FALSE) +
shadow_mark() +
enter_grow() +
enter_fade()
Using any data besides gapminder, create any animation you want (besides the ones I’ve included here). Your animations can be similar to Examples 1 and 2, but should include changes beyond plugging in different variables to the aes calls.
Everybody should submit an .rmd file with your animation. You should also turn in an HTML document (via RPubs) or use anim_save() to save your animation and turn it in alongside your .rmd file:
anim_save(FILENAME, animation = last_animation())
(for more info, use ?anim_save)