Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################################################################
- # Toronto Shelters - Average nightly number of people served in Toronto's shelter system.
- # Created by: reddit.com/user/brianhaas19
- # Link to data
- # https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-12-01
- # Setup
- library(tidyverse)
- library(scales)
- library(patchwork)
- library(ggrepel)
- library(ggsci)
- theme_set(theme_bw())
- # Load data
- shelters <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-12-01/shelters.csv')
- # Clean data
- shelters <- shelters %>%
- rename(date = occupancy_date)
- # Visualize
- ### Recommended parameters for R notebook: fig.height=7, fig.width=9
- line_size = 0.5
- p1 <- shelters %>%
- filter(is.finite(capacity)) %>% # to be consistent with the second plot
- mutate(month = lubridate::floor_date(date, "month")) %>%
- group_by(month, sector) %>%
- mutate(num_days = n_distinct(date)) %>% # number of days in the month for calculating nightly average
- summarise(occupancy = sum(occupancy)/num_days) %>%
- distinct() %>%
- ungroup() %>% # ungroup() here so that the next line to create the labels works
- mutate(label = if_else(month == max(month), as.character(sector), NA_character_)) %>%
- ggplot(aes(month, occupancy, group = sector, color = sector)) +
- geom_line(size = line_size, show.legend = FALSE) +
- geom_label_repel(aes(label = label), seed = 1, show.legend = FALSE, size = 4, label.padding = 0.15,
- nudge_x = 0, na.rm = TRUE) + # *1
- scale_y_continuous(labels = comma) +
- ggsci::scale_color_jama() +
- labs(x = NULL, y = "Number of People", title = expression(paste("Average nightly ", bold(occupancy), " by ", bold(sector), ":"))) +
- theme(plot.title = element_text(size = 12))
- p2 <- shelters %>%
- filter(is.finite(capacity)) %>%
- mutate(month = lubridate::floor_date(date, "month")) %>%
- group_by(month) %>%
- mutate(num_days = n_distinct(date)) %>%
- summarise(occupancy = sum(occupancy)/num_days,
- capacity = sum(capacity)/num_days) %>%
- distinct() %>%
- pivot_longer(cols = c(occupancy, capacity), names_to = "type") %>%
- ungroup() %>% # ungroup() here so that the next line to create the labels works
- mutate(label = if_else(month == max(month), if_else(type == "occupancy", "Occupancy", "Capacity"), NA_character_)) %>%
- ggplot(aes(month, value, color = type)) +
- geom_line(size = line_size, show.legend = FALSE) +
- geom_label_repel(aes(label = label), seed = 1, show.legend = FALSE, size = 4, label.padding = 0.15,
- nudge_x = 0, na.rm = TRUE) + # *1
- scale_y_continuous(labels = comma) +
- scale_color_manual(values = c("#80796B", "#6A6599")) + # these are the remaining two colors from ggsci::scale_color_jama() not used above
- labs(x = NULL, y = "Number of People", title = expression(paste("Average nightly ", bold(occupancy), " and ", bold(capacity), " - all sectors:"))) +
- theme(plot.title = element_text(size = 12))
- # Use patchwork package to combine the plots:
- P <- p1/p2 + plot_annotation(title = expression(paste(bold("Toronto Shelters"), " - Average nightly number of people served in Toronto's shelter system.")),
- caption = expression(paste("Created by: ", italic("reddit.com/user/brianhaas19"), "\tData source: ", italic("https://github.com/rfordatascience/tidytuesday/blob/master/data/2020/2020-12-01"))))
- P
- # Uncomment this line to save to disk:
- # ggsave(str_c(getwd(), "/toronto_shelters.png"),
- # plot = P, height = 7, width = 9)
- ###################################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement