brianhaas19

GABF Gold Medal Distribution From CSV

Nov 9th, 2020 (edited)
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.30 KB | None | 0 0
  1. ###################################################################################################
  2. # Great American Beer Festival - Distribution of gold medal winning breweries from 1987 to 2020
  3. # Created by: reddit.com/user/brianhaas19
  4.  
  5. # Link to data
  6. # https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-10-20/readme.md
  7.  
  8. # Setup
  9. library(tidyverse)
  10. library(tidytuesdayR)
  11. library(scales)
  12. library(gganimate)
  13. theme_set(theme_bw())
  14.  
  15. # Load clean data set of GABF gold medal winners from CSV:
  16. gold_medal_map <- read_csv("https://pastebin.com/raw/TLpKkMnG")
  17.  
  18. # Load additional data:
  19. ### State ID's:
  20. state_ids <- structure(list(state = c("alabama", "alaska", "arizona", "arkansas",
  21. "california", "colorado", "connecticut", "delaware", "district of columbia",
  22. "florida", "georgia", "hawaii", "idaho", "illinois", "indiana",
  23. "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland",
  24. "massachusetts", "michigan", "minnesota", "mississippi", "missouri",
  25. "montana", "nebraska", "nevada", "new hampshire", "new jersey",
  26. "new mexico", "new york", "north carolina", "north dakota", "ohio",
  27. "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina",
  28. "south dakota", "tennessee", "texas", "utah", "vermont", "virginia",
  29. "washington", "west virginia", "wisconsin", "wyoming"), id = c("AL",
  30. "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI",
  31. "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI",
  32. "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC",
  33. "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT",
  34. "VT", "VA", "WA", "WV", "WI", "WY")), class = c("spec_tbl_df",
  35. "tbl_df", "tbl", "data.frame"), row.names = c(NA, -51L), spec = structure(list(
  36.     cols = list(state = structure(list(), class = c("collector_character",
  37.     "collector")), id = structure(list(), class = c("collector_character",
  38.     "collector"))), default = structure(list(), class = c("collector_guess",
  39.     "collector")), skip = 1L), class = "col_spec"))
  40.  
  41. ### Get map data from the `maps` package:
  42. states <- map_data("state")
  43. names(states) <- names(states) %>% str_replace("region", "state")
  44.  
  45.  
  46. # Colors and Themes
  47. colors <- c("GABF_blue" = "#005F85",
  48.             "GABF_gold" = "#C69F32",
  49.             "GABF_lightblue" ="#8ED1EE")
  50.  
  51. map_theme <- theme(
  52.   axis.text = element_blank(),
  53.   axis.line = element_blank(),
  54.   axis.ticks = element_blank(),
  55.   panel.border = element_blank(),
  56.   panel.grid = element_blank(),
  57.   axis.title = element_blank(),
  58.   plot.title = element_text(color = colors["GABF_blue"]),
  59.   plot.subtitle = element_text(color = colors["GABF_blue"], size = 18),
  60.   plot.caption = element_text(color = colors["GABF_blue"], hjust = 0)
  61. )
  62.  
  63. # Visualize
  64.  
  65. ## Animated Plot:
  66. num_years <- n_distinct(gold_medal_map$year)
  67. p <- ggplot(data = gold_medal_map) +
  68.   geom_polygon(data = states,
  69.                aes(x = long, y = lat, group = group),
  70.                size = 0.2,
  71.                fill = colors["GABF_lightblue"], color = colors["GABF_blue"]) +
  72.   geom_point(aes(long, lat, size = n),
  73.              color = colors["GABF_gold"],
  74.              alpha = 0.7,
  75.              show.legend = FALSE) +
  76.   ggrepel::geom_label_repel(data = filter(gold_medal_map, n > 1),
  77.                             label.size = 0,
  78.                             fill = alpha(c("white"), 0.6),
  79.                             aes(long, lat, label = label),
  80.                             color = colors["GABF_blue"],
  81.                             seed = 1) +
  82.   scale_size_continuous(range = c(3, 8)) +
  83.   coord_quickmap() +
  84.   map_theme +
  85.   labs(title = expression(paste(bold("Great American Beer Festival"), " - Distribution of gold medal winning breweries from 1987 to 2020")),
  86.        subtitle = "Year: {next_state}",
  87.        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")))) +
  88.   transition_states(year, transition_length = 1, state_length = 1)
  89.  
  90. animate(p,
  91.         nframes = num_years * 2, duration = num_years*2,
  92.         height = 6, width = 9, units = "in", res = 150)
  93.  
  94. #anim_save(str_c(getwd(), "/GABF.gif")) # Uncomment to save to disk
  95. ###################################################################################################
Add Comment
Please, Sign In to add comment