Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ofchurches/8f8d93d1480c6d8506848c79b2c89f1e to your computer and use it in GitHub Desktop.
Save ofchurches/8f8d93d1480c6d8506848c79b2c89f1e to your computer and use it in GitHub Desktop.
tidy_tuesday_20203103
library(tidyverse)
library(scales)
#First, come up with an idea in static ggplot...
beer_states <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-31/beer_states.csv')
top_states <- beer_states %>%
filter(state != "total") %>%
filter(type == "On Premises") %>%
group_by(state) %>%
summarise(sum = sum(barrels)) %>%
ungroup() %>%
top_n(10, sum) %>%
pull(state)
plot_beer <- beer_states %>%
filter(state %in% top_states) %>%
filter(type == "On Premises") %>%
group_by(year, state) %>%
summarise(sum = sum(barrels)) %>%
ungroup()
plot_beer %>%
ggplot(mapping = aes(x = year, y = sum, colour = state, goup = state)) +
geom_line(size = 2) +
labs(y = "Barrels of beer",
x = "Year",
title = "Top 10 States for on premises beer drinking in breweries by barrels",
colour = "State") +
scale_x_continuous(breaks = seq(from = 2008, to = 2019, by = 3)) +
scale_y_continuous(labels = comma) +
theme_bw() +
facet_wrap(~state, nrow = 2)
ggsave("beer_line.png")
#Turn this into a bar race with gganimate
#Uses the tutorial here: https://towardsdatascience.com/create-animated-bar-charts-using-r-31d09e5841da
library(gganimate)
library(gifski)
library(scales)
formatted_beer <- plot_beer %>%
group_by(year) %>%
mutate(rank = rank(-sum),
Value_rel = sum/sum[rank==1],
Value_lbl = round(sum, 0))
staticplot = ggplot(formatted_beer, aes(rank, group = state,
fill = as.factor(state), color = as.factor(state))) +
geom_tile(aes(y = sum/2,
height = sum,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(state, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y=sum,label = Value_lbl, hjust=0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
panel.grid.minor.x = element_line( size=.1, color="grey" ),
plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
plot.background=element_blank(),
plot.margin = margin(2,2, 2, 4, "cm"))
anim = staticplot + transition_states(year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'Year : {closest_state}',
subtitle = "Top 10 States for on premises beer drinking in breweries by barrels",
caption = "Data from TidyTuesday 2020-03-31")
animate(anim, 200, fps = 20, width = 1200, height = 1000,
renderer = gifski_renderer("beer_bar_race.gif"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment