Created
May 7, 2021 10:22
-
-
Save Lextuga007/ac58cea85a43bfba5975ebcf899c832f to your computer and use it in GitHub Desktop.
Chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Suicides # | |
| # code based mostly on https://www.trafforddatalab.io/charticles/2020-11-16-suicides/ | |
| # Source: Office for National Statistics | |
| # URL: https://www.ons.gov.uk/peoplepopulationandcommunity/birthsdeathsandmarriages/deaths/datasets/suicidesbylocalauthority | |
| # Licence: OGL 3.0 | |
| # load libraries --------------------------- | |
| library(tidyverse) ; library(httr) ; library(readxl) ; library(ggtext) ; library(stringr) ; library(janitor) | |
| # set area ---------------------------------------------------------------- | |
| lookup <- c("Nottingham", | |
| "Nottinghamshire", | |
| "East Midlands", | |
| "England") | |
| # load data --------------------------- | |
| tmp <- tempfile(fileext = ".xls") | |
| GET(url = "https://www.ons.gov.uk/file?uri=%2fpeoplepopulationandcommunity%2fbirthsdeathsandmarriages%2fdeaths%2fdatasets%2fsuicidesbylocalauthority%2fcurrent/2019localauthorityfinal.xls", | |
| write_disk(tmp)) | |
| # tidy data --------------------------- | |
| df <- read_xls(tmp, sheet = 4, skip = 3) %>% | |
| select(region = ...3, | |
| `2019`:`2002`) %>% | |
| filter(!is.na(region)) %>% | |
| pivot_longer(-region, | |
| names_to = "period", | |
| values_to = "number") %>% | |
| mutate(period = as.integer(period)) | |
| nottingham <- df %>% | |
| filter(region == "Nottingham") | |
| nottinghamshire <- df %>% | |
| filter(region == "Nottinghamshire") | |
| df_exclude <- df %>% | |
| filter(region != str_detect("Nott", region)) | |
| # plot data --------------------------- | |
| ggplot(df_exclude, aes(x = period, | |
| y = number, | |
| group = region)) + | |
| geom_line(colour = "lightgrey") + | |
| geom_line(data = nottingham, | |
| aes(x = period, | |
| y = number, | |
| colour = "green")) + ## DEFAULTS TO PINK COLOUR | |
| geom_line(data = nottinghamshire, | |
| aes(x = period, | |
| y = number, | |
| colour = "darkgreen")) + ## DEFAULTS TO BLUE | |
| labs( | |
| title = "Suicide registrations", | |
| subtitle = paste0("<span style = 'color:#757575;'>Number of suicides by local authority</span>"), | |
| caption = "Source: ONS", | |
| x = NULL, | |
| y = NULL, | |
| colour = NULL, | |
| labels = c("Nottingham", "Nottinghamshire") | |
| ) + | |
| theme_minimal() + | |
| theme(plot.margin = unit(rep(0.5, 4), "cm"), | |
| panel.grid.major.x = element_blank(), | |
| panel.grid.minor = element_blank(), | |
| plot.title.position = "plot", | |
| plot.title = element_text(size = 14, face = "bold"), | |
| plot.subtitle = element_markdown(size = 12, margin = margin(b = 20)), | |
| plot.caption = element_text(colour = "grey60", margin = margin(t = 20, b = -10)), | |
| legend.position = "none", | |
| axis.text.x = element_text(angle = 90, vjust = 0.5), | |
| axis.title.y.right = element_text(size = 12, angle = 0, vjust = 1, margin = margin(l = 5)), | |
| axis.ticks.x = element_line(colour = "#333333", size = 0.5)) + | |
| # Nottingam | |
| geom_richtext(aes(x = 2016, y = 75, label = "<span style = 'color:#814047;'</span> Nottingham"), | |
| size = 3, color = "#212121", lineheight = .9, | |
| fill = NA, label.color = NA, stat = "unique") + | |
| geom_curve(aes(x = 2016, y = 75, xend = 2015, yend = 55), | |
| colour = "#212121", curvature = -0.3, size = 0.3, | |
| arrow = arrow(length = unit(0.01, "npc"))) + | |
| # Nottinghamshire | |
| geom_richtext(aes(x = 2016, y = 40, label = "<span style = 'color:darkgreen;'</span> Nottinghamshire"), | |
| size = 3, color = "#212121", lineheight = .9, | |
| fill = NA, label.color = NA, stat = "unique") + | |
| geom_curve(aes(x = 2016, y = 30, xend = 2015, yend = 25), | |
| colour = "#212121", curvature = 0.3, size = 0.3, | |
| arrow = arrow(length = unit(0.01, "npc"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment