-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNRSASDxSIMD.R
More file actions
73 lines (63 loc) · 2.72 KB
/
NRSASDxSIMD.R
File metadata and controls
73 lines (63 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
rm(list=ls())
library(tidyverse)
library(curl)
library(scales)
library(readxl)
library(extrafont)
library(ragg)
library(paletteer)
library(lubridate)
library(mgcv)
#Set common font for all plots
font <- "Lato"
theme_custom <- function() {
theme_classic() %+replace%
theme(plot.title.position="plot", plot.caption.position="plot",
strip.background=element_blank(), strip.text=element_text(face="bold", size=rel(1)),
strip.clip="off",
plot.title=element_text(face="bold", size=rel(1.5), hjust=0,
margin=margin(0,0,5.5,0)),
text=element_text(family="Lato"),
plot.subtitle=element_text(colour="Grey40", hjust=0, vjust=1),
plot.caption=element_text(colour="Grey40", hjust=1, vjust=1, size=rel(0.8)),
axis.text=element_text(colour="Grey40"),
axis.title=element_text(colour="Grey20"),
legend.text=element_text(colour="Grey40"),
legend.title=element_text(colour="Grey20"),
axis.line.x=element_blank(),
panel.grid.major.y=element_line(colour="grey95"))
}
#Read in data from ONS website
temp <- tempfile()
source <- "https://www.nrscotland.gov.uk/files//statistics/alcohol-deaths/2022/alcohol-specific-deaths-22-all-tabs.xlsx"
temp <- curl_download(url=source, destfile=temp, quiet=FALSE, mode="wb")
rawdata <- read_excel(temp, sheet="Table_5", range="A5:H335") %>%
mutate(SIMD=paste(6-`SIMD quintile`, `Quintile description`),
SIMD=gsub(" NA", "", SIMD))
data <- rawdata %>%
group_by(Year, Sex) %>%
mutate(Total=sum(Deaths)) %>%
ungroup() %>%
mutate(Prop=Deaths/Total)
ggplot(data %>% filter(Sex=="Persons"), aes(x=Year, y=Prop, colour=SIMD))+
geom_hline(yintercept=0, colour="grey30")+
geom_line()+
scale_x_continuous(name="")+
scale_y_continuous(name="Proportion of alcohol-specific deaths",
labels=label_percent(accuracy=1))+
scale_colour_manual(values=c("#ffc5c0", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177"))+
theme_custom()
ggplot(data %>% filter(Sex=="Persons"), aes(x=Year, y=Deaths, fill=SIMD))+
geom_hline(yintercept=0, colour="grey30")+
geom_area(position="stack")+
scale_x_continuous(name="")+
scale_y_continuous(name="Alcohol-specific deaths")+
scale_fill_manual(values=c("#ffc5c0", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177"))+
theme_custom()
ggplot(data %>% filter(Sex=="Persons"), aes(x=Year, y=Deaths, colour=SIMD))+
geom_hline(yintercept=0, colour="grey30")+
geom_line()+
scale_x_continuous(name="")+
scale_y_continuous(name="Alcohol-specific deaths")+
scale_colour_manual(values=c("#ffc5c0", "#fa9fb5", "#f768a1", "#c51b8a", "#7a0177"))+
theme_custom()