brianhaas19

Big Mac Index (Tidy Tuesday 2020-12-22)

Dec 31st, 2020
1,282
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###################################################################################################
  2. # Big Mac Index - Change in price of the McDonald's Big Mac burger from 2000 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-12-22
  7.  
  8. # Setup
  9. library(tidyverse)
  10. library(scales)
  11. library(patchwork)
  12. theme_set(theme_bw())
  13.  
  14. # Load data
  15. big_mac <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-12-22/big-mac.csv')
  16. df <- big_mac # generic name
  17.  
  18. # Clean data
  19. ### Filter for countries with mostly complete data:
  20. complete_data <- df %>%
  21.   count(name) %>%
  22.   filter(n >= 19) %>% # this leaves 42 countries
  23.   .$name
  24.  
  25. df <- df %>%
  26.   filter(name %in% complete_data)
  27.  
  28. # Visualize
  29. # Suggested parameters for R notebook: fig.height=12, fig.width=9
  30.  
  31. ### Colors
  32. colors <- c("bun" = "#F7C064",
  33.             "burger" = "#7A4E3D",
  34.             "lettuce" = "#A0C03A",
  35.             "sauce" = "#F5DE92",
  36.             "cheese" = "#F9E239",
  37.             "sesame" = "#F9F3D9",
  38.             "red" = "#DF4458")
  39. # Ref: https://images.app.goo.gl/3iAvxZfQ9r75CCVz6
  40.  
  41. ### Plot
  42. p1 <- df %>%
  43.   mutate(name2 = name) %>% # use a duplicate column to allow 'highlighting' within each facet
  44.   ggplot(aes(date, dollar_price)) +
  45.   geom_line(data = df,
  46.             aes(group = name),
  47.             color = colors["sauce"],
  48.             alpha = 0.7) +
  49.   geom_line(color = colors["burger"]) +
  50.   facet_wrap(~name2, nrow = 7) +
  51.   labs(x = "Date", y = "Price in USD",
  52.        title = expression(paste(bold("Big Mac Index"), " - Change in price of the McDonald's Big Mac burger from 2000 to 2020")),
  53.        subtitle = "The price in USD is calculated by converting from local currency to US dollars using the appropriate exchange rate.",
  54.        caption = expression(paste("Created by: ", italic("reddit.com/user/brianhaas19"), "\tData source: ", italic("https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-12-22")))) +
  55.   scale_x_continuous(breaks = seq(min(df$date), max(df$date), length.out = 5),
  56.                      labels = date_format("'%y")) +
  57.   scale_y_continuous(breaks = seq(2.5, 7.5, 2.5), labels = dollar) +
  58.   theme(plot.title = element_text(color = colors["burger"]),
  59.         plot.subtitle = element_text(color = colors["burger"]),
  60.         plot.caption = element_text(color = colors["burger"], hjust = 0),
  61.         plot.background = element_rect(fill = colors["bun"]),
  62.         panel.background = element_rect(fill = colors["bun"]),
  63.         panel.grid.major = element_line(color = colors["lettuce"], size = 0.25, linetype = "dashed"),
  64.         panel.grid.minor = element_blank(),
  65.         axis.title = element_text(color = colors["burger"]),
  66.         axis.text = element_text(color = colors["burger"]),
  67.         strip.background = element_rect(fill = colors["red"]),
  68.         strip.text = element_text(color = colors["cheese"]))
  69. p1
  70.  
  71. ### Uncomment to save to disk:
  72. # ggsave(plot = p1,
  73. #        filename = str_c(getwd(), "/big_mac_index.png"),
  74. #        height = 12, width = 9)
  75. ###################################################################################################
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×