Advertisement
brianhaas19

Historical Phone Usage

Nov 22nd, 2020
1,653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.10 KB | None | 0 0
  1. ###################################################################################################
  2. # Historical Phone Usage - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017
  3. # Created by: reddit.com/user/brianhaas19
  4.  
  5. # Link to data
  6. # https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-11-10
  7.  
  8. # Setup
  9. library(tidyverse)
  10. library(tidytuesdayR)
  11. library(gganimate)
  12. theme_set(theme_bw())
  13.  
  14. # Load data:
  15. tt <- tt_load("2020-11-10")
  16. mobile <- tt$mobile
  17. landline <- tt$landline
  18.  
  19. # Combine and clean data
  20. subs <- rbind(
  21.   mutate(mobile, type = "mobile") %>% rename(subs = mobile_subs),
  22.   mutate(landline, type = "landline") %>% rename(subs = landline_subs)
  23. ) %>%
  24.   rename(country = entity)
  25.  
  26. # Shorten the name of Bosnia and Herzegovina:
  27. subs$country[subs$country == "Bosnia and Herzegovina"] <- "Bosnia and Herz."
  28.  
  29. # List of European countries:
  30. europe <- subs %>%
  31.   filter(continent == "Europe", year <= 2017) %>% # data is mostly missing for year > 2017
  32.   group_by(country) %>%
  33.   mutate(has_data = !is.na(subs)) %>%
  34.   filter(has_data) %>%
  35.   count(has_data) %>%
  36.   filter(!(country %in% c("Liechtenstein", "Gibraltar", "San Marino"))) # missing some data
  37.  
  38. # Visualize
  39.  
  40. ## Colors:
  41. orange <- "#F5793A"
  42. blue <- "#0F2080"
  43. # Ref: Color blind friendly palettes: https://venngage.com/blog/color-blind-friendly-palette/
  44.  
  45. ## Static
  46. ### Recommended parameters for R notebook: fig.height=9, fig.width=9
  47. p <- subs %>%
  48.   filter(!is.na(subs)) %>%
  49.   filter(continent == "Europe", country %in% europe$country) %>%
  50.   ggplot(aes(year, subs, group = type, color = type)) +
  51.   geom_line() +
  52.   facet_wrap(~country, scales = "free_x") +
  53.   labs(x = "Year", y = "Subscriptions (per 100 people)",
  54.        title = expression(paste(bold("Historical Phone Usage"), " - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017")),
  55.        subtitle = "Measured as the number of subscriptions per 100 people.",
  56.        caption = expression(paste("Created by: ", italic("reddit.com/user/brianhaas19"), "\tData source: ", italic("https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-11-10")))) +
  57.   scale_x_continuous(breaks = c(1990, 2000, 2010),
  58.                      limits = c(1990, 2017)) +
  59.   scale_color_manual("Type:",
  60.                      breaks = c("landline", "mobile"),
  61.                      labels = c("Landline", "Mobile"),
  62.                      values = c(blue, orange)) +
  63.   theme(legend.position = "top",
  64.         plot.caption = element_text(hjust = 0))
  65. p
  66.  
  67. ### Uncomment this line to save to disk:
  68. # ggsave(str_c(getwd(), "/mobile_landline_subs.png"),
  69. #        plot = p, height = 9, width = 9)
  70.  
  71. ## Animation
  72. anim <- subs %>%
  73.   filter(!is.na(subs)) %>%
  74.   filter(continent == "Europe", country %in% europe$country) %>%
  75.   ggplot(aes(year, subs, group = type, color = type)) +
  76.   geom_line() +
  77.   geom_point() +
  78.   facet_wrap(~country, scales = "free_x") +
  79.   labs(x = "Year", y = "Subscriptions (per 100 people)",
  80.        title = expression(paste(bold("Historical Phone Usage"), " - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017")),
  81.        subtitle = "Measured as the number of subscriptions per 100 people.",
  82.        caption = expression(paste("Created by: ", italic("reddit.com/user/brianhaas19"), "\tData source: ", italic("https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-11-10")))) +
  83.   scale_x_continuous(breaks = c(1990, 2000, 2010),
  84.                      limits = c(1990, 2017)) +
  85.   scale_color_manual("Type:",
  86.                      breaks = c("landline", "mobile"),
  87.                      labels = c("Landline", "Mobile"),
  88.                      values = c(blue, orange)) +
  89.   theme(legend.position = "top",
  90.         plot.caption = element_text(hjust = 0)) +
  91.   transition_reveal(year)
  92.  
  93. animate(anim,
  94.         nframes = 120, end_pause = 20,
  95.         height = 9, width = 9, units = "in", res = 150)
  96.  
  97. ### Uncomment this line to save to disk:
  98. # anim_save(str_c(getwd(), "/mobile_landline_subs.gif"), animation = anim)
  99. ###################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement