Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################################################################
- # Historical Phone Usage - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017
- # Created by: reddit.com/user/brianhaas19
- # Link to data
- # https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-11-10
- # Setup
- library(tidyverse)
- library(tidytuesdayR)
- library(gganimate)
- theme_set(theme_bw())
- # Load data:
- tt <- tt_load("2020-11-10")
- mobile <- tt$mobile
- landline <- tt$landline
- # Combine and clean data
- subs <- rbind(
- mutate(mobile, type = "mobile") %>% rename(subs = mobile_subs),
- mutate(landline, type = "landline") %>% rename(subs = landline_subs)
- ) %>%
- rename(country = entity)
- # Shorten the name of Bosnia and Herzegovina:
- subs$country[subs$country == "Bosnia and Herzegovina"] <- "Bosnia and Herz."
- # List of European countries:
- europe <- subs %>%
- filter(continent == "Europe", year <= 2017) %>% # data is mostly missing for year > 2017
- group_by(country) %>%
- mutate(has_data = !is.na(subs)) %>%
- filter(has_data) %>%
- count(has_data) %>%
- filter(!(country %in% c("Liechtenstein", "Gibraltar", "San Marino"))) # missing some data
- # Visualize
- ## Colors:
- orange <- "#F5793A"
- blue <- "#0F2080"
- # Ref: Color blind friendly palettes: https://venngage.com/blog/color-blind-friendly-palette/
- ## Static
- ### Recommended parameters for R notebook: fig.height=9, fig.width=9
- p <- subs %>%
- filter(!is.na(subs)) %>%
- filter(continent == "Europe", country %in% europe$country) %>%
- ggplot(aes(year, subs, group = type, color = type)) +
- geom_line() +
- facet_wrap(~country, scales = "free_x") +
- labs(x = "Year", y = "Subscriptions (per 100 people)",
- title = expression(paste(bold("Historical Phone Usage"), " - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017")),
- subtitle = "Measured as the number of subscriptions per 100 people.",
- 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")))) +
- scale_x_continuous(breaks = c(1990, 2000, 2010),
- limits = c(1990, 2017)) +
- scale_color_manual("Type:",
- breaks = c("landline", "mobile"),
- labels = c("Landline", "Mobile"),
- values = c(blue, orange)) +
- theme(legend.position = "top",
- plot.caption = element_text(hjust = 0))
- p
- ### Uncomment this line to save to disk:
- # ggsave(str_c(getwd(), "/mobile_landline_subs.png"),
- # plot = p, height = 9, width = 9)
- ## Animation
- anim <- subs %>%
- filter(!is.na(subs)) %>%
- filter(continent == "Europe", country %in% europe$country) %>%
- ggplot(aes(year, subs, group = type, color = type)) +
- geom_line() +
- geom_point() +
- facet_wrap(~country, scales = "free_x") +
- labs(x = "Year", y = "Subscriptions (per 100 people)",
- title = expression(paste(bold("Historical Phone Usage"), " - Landline and Mobile Phone Subscriptions in Europe from 1990 to 2017")),
- subtitle = "Measured as the number of subscriptions per 100 people.",
- 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")))) +
- scale_x_continuous(breaks = c(1990, 2000, 2010),
- limits = c(1990, 2017)) +
- scale_color_manual("Type:",
- breaks = c("landline", "mobile"),
- labels = c("Landline", "Mobile"),
- values = c(blue, orange)) +
- theme(legend.position = "top",
- plot.caption = element_text(hjust = 0)) +
- transition_reveal(year)
- animate(anim,
- nframes = 120, end_pause = 20,
- height = 9, width = 9, units = "in", res = 150)
- ### Uncomment this line to save to disk:
- # anim_save(str_c(getwd(), "/mobile_landline_subs.gif"), animation = anim)
- ###################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement