Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################################################################
- # Great American Beer Festival - Distribution of gold medal winning breweries from 1987 to 2020
- # Created by: reddit.com/user/brianhaas19
- # Link to data
- # https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-10-20/readme.md
- # Setup
- library(tidyverse)
- library(tidytuesdayR)
- library(scales)
- library(gganimate)
- theme_set(theme_bw())
- # Load clean data set of GABF gold medal winners from CSV:
- gold_medal_map <- read_csv("https://pastebin.com/raw/TLpKkMnG")
- # Load additional data:
- ### State ID's:
- state_ids <- structure(list(state = c("alabama", "alaska", "arizona", "arkansas",
- "california", "colorado", "connecticut", "delaware", "district of columbia",
- "florida", "georgia", "hawaii", "idaho", "illinois", "indiana",
- "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland",
- "massachusetts", "michigan", "minnesota", "mississippi", "missouri",
- "montana", "nebraska", "nevada", "new hampshire", "new jersey",
- "new mexico", "new york", "north carolina", "north dakota", "ohio",
- "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina",
- "south dakota", "tennessee", "texas", "utah", "vermont", "virginia",
- "washington", "west virginia", "wisconsin", "wyoming"), id = c("AL",
- "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI",
- "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI",
- "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC",
- "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT",
- "VT", "VA", "WA", "WV", "WI", "WY")), class = c("spec_tbl_df",
- "tbl_df", "tbl", "data.frame"), row.names = c(NA, -51L), spec = structure(list(
- cols = list(state = structure(list(), class = c("collector_character",
- "collector")), id = structure(list(), class = c("collector_character",
- "collector"))), default = structure(list(), class = c("collector_guess",
- "collector")), skip = 1L), class = "col_spec"))
- ### Get map data from the `maps` package:
- states <- map_data("state")
- names(states) <- names(states) %>% str_replace("region", "state")
- # Colors and Themes
- colors <- c("GABF_blue" = "#005F85",
- "GABF_gold" = "#C69F32",
- "GABF_lightblue" ="#8ED1EE")
- map_theme <- theme(
- axis.text = element_blank(),
- axis.line = element_blank(),
- axis.ticks = element_blank(),
- panel.border = element_blank(),
- panel.grid = element_blank(),
- axis.title = element_blank(),
- plot.title = element_text(color = colors["GABF_blue"]),
- plot.subtitle = element_text(color = colors["GABF_blue"], size = 18),
- plot.caption = element_text(color = colors["GABF_blue"], hjust = 0)
- )
- # Visualize
- ## Animated Plot:
- num_years <- n_distinct(gold_medal_map$year)
- p <- ggplot(data = gold_medal_map) +
- geom_polygon(data = states,
- aes(x = long, y = lat, group = group),
- size = 0.2,
- fill = colors["GABF_lightblue"], color = colors["GABF_blue"]) +
- geom_point(aes(long, lat, size = n),
- color = colors["GABF_gold"],
- alpha = 0.7,
- show.legend = FALSE) +
- ggrepel::geom_label_repel(data = filter(gold_medal_map, n > 1),
- label.size = 0,
- fill = alpha(c("white"), 0.6),
- aes(long, lat, label = label),
- color = colors["GABF_blue"],
- seed = 1) +
- scale_size_continuous(range = c(3, 8)) +
- coord_quickmap() +
- map_theme +
- labs(title = expression(paste(bold("Great American Beer Festival"), " - Distribution of gold medal winning breweries from 1987 to 2020")),
- subtitle = "Year: {next_state}",
- caption = expression(paste("Created by: ", italic("reddit.com/user/brianhaas19"), "\tData source: ", italic("https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-10-20/readme.md")))) +
- transition_states(year, transition_length = 1, state_length = 1)
- animate(p,
- nframes = num_years * 2, duration = num_years*2,
- height = 6, width = 9, units = "in", res = 150)
- #anim_save(str_c(getwd(), "/GABF.gif")) # Uncomment to save to disk
- ###################################################################################################
Add Comment
Please, Sign In to add comment