-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveBirthsScotland2022.Rmd
285 lines (207 loc) · 10.3 KB
/
LiveBirthsScotland2022.Rmd
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
---
title: "Live births in Scotland 2022"
subtitle: "Health boards ranking"
author: "Inmaculada Ruiz-Morales "
date: "`r format(Sys.time(), '%d-%m-%Y')`"
output: html_document
urlcolor: blue
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
```{r libraries, message=FALSE, warning=FALSE, tidy.opts=list(width.cutoff=80), tidy = TRUE}
# I load my favorite packages (I don´t always use all of them but I keep all in my template).
library(tidyverse)
library(janitor)
library(lubridate)
library(kableExtra)
library(formatR)
library(scales)
library(sp)
library(sf)
library(gridExtra)
library(latticeExtra)
library(cowplot)
```
# Data used
To perfom this analysis, two datasets has been retrieved from [The Scottish Health and Social Care Open Data platform](https://www.opendata.nhs.scot/) for their analysis. And one from the [Spatial Data Metadata Portal](https://spatialdata.gov.scot/geonetwork/srv/eng/catalog.search#/home), Scotland’s catalogue of spatial data.
1. [Births by hospital ](https://www.opendata.nhs.scot/dataset/df10dbd4-81b3-4bfa-83ac-b14a5ec62296/resource/d534ae02-7890-4fbc-8cc7-f223d53fb11b), Containing 8266 observations with information for 5 variables The number of live and stillbirths by hospital of birth sourced from the Scottish Morbidity Record 02 (SMR02).
2. [Hospitals in Scotland](https://www.opendata.nhs.scot/dataset/cbd1802e-0e04-4282-88eb-d7bdcfb120f0/resource/c698f450-eeed-41a0-88f7-c1e40a568acc). 277 observations of 16 variables. with a listing of all NHS hospitals across Scotland.
3. [geographical spatial data for the Scottish Health Boards](https://spatialdata.gov.scot/geonetwork/srv/eng/catalog.search#/metadata/f12c3826-4b4b-40e6-bf4f-77b9ed01dc14), a ESRI Shape file spatial data defining the boundaries of NHS Health Boards in Scotland,
Reading the data and cleaning variable names:
```{r read in the data}
#read in .csv files with the data and clea_names
births <- read_csv("https://www.opendata.nhs.scot/dataset/df10dbd4-81b3-4bfa-83ac-b14a5ec62296/resource/d534ae02-7890-4fbc-8cc7-f223d53fb11b/download/10.3_birthsbyhospital.csv") %>%
clean_names() %>%
separate(financial_year, into = c("year", NA), sep = "/")
hospitals <- read_csv("https://www.opendata.nhs.scot/dataset/cbd1802e-0e04-4282-88eb-d7bdcfb120f0/resource/c698f450-eeed-41a0-88f7-c1e40a568acc/download/current-hospital_flagged20211216.csv") %>%
clean_names()
#read in .shp file
## you need to download all the files in your computer and change the path in the code
path = "D:/SpatiaDataFiles/SG_NHS_HealthBoards_2019.shp"
hb_spatial <- st_read(path)
```
We can see that the category 'outcome' could be 'Live', 'Still' or 'Unknown'. We are going to represent live births
```{r}
head(births)
```
```{r}
table(births$outcome)
```
## Total live births in Scotland in the financial year 2022-23
```{r}
births %>%
filter(year==2022 & outcome=="Live") %>%
summarize(new_babies_2022 = sum(smr02births))
```
## Babies born at home in 2022
```{r}
#number of babies born at home
births %>%
filter(year==2022 & outcome=="Live" & hospital=="D201N") %>%
summarize("Babies born at home in 2022"= sum(smr02births))
```
# Evolution of total number of live births a long time since 1997 to 2022
```{r}
baby_year <- births %>% filter (outcome=="Live") %>%
group_by(year) %>%
summarise(number_of_babies = sum(smr02births))
kable(baby_year)
```
```{r, fig.width=11, fig.height=4}
ggplot(baby_year, aes(year, number_of_babies)) + geom_col(fill="#0097a7", alpha=0.3)+
geom_text(aes(label = number_of_babies), vjust=-0.3, size =2.8, color='#005B70') +
labs(
title = "Number of live births in Scottish hospitals",
subtitle = "(by financial year)",
caption="Data from: Public health Scotland") +
ylab("number of births")
```
# Evolution of still births outcomes since 1997 to 2022
```{r}
still_year <- births %>%
filter (outcome=="Still") %>%
group_by(year) %>%
summarise(still_births = sum(smr02births))
kable(still_year)
```
```{r, fig.width=10, fig.height=4}
ggplot(still_year, aes(year, still_births)) + geom_col(fill="brown", alpha=0.4) +
ylim(0, 1000)
```
Live births at home. (Maybe not all home births were recorded in this dataset).
```{r, fig.width=10, fig.height=4}
home <- births %>%
# D201N is the code for home births
filter(hospital== "D201N") %>%
group_by(year) %>%
summarize(home_delivered = sum(smr02births))
ggplot(home, aes(year, home_delivered)) + geom_col(fill="#0097a7", alpha=0.3) +
ylab("Number of babies") +
geom_text(aes(label = home_delivered), vjust=-0.1, size =3, color='#0097a7') +
labs(
title = "Trends in home delivery births in Scotland",
subtitle = "(by financial year)",
caption="Data from: Public health Scotland")
```
admissions_deaths %>%
ggplot(aes(x = reorder(injury_type, death_ratio), y = death_ratio)) +
geom_col(color="red", fill='pink') +
coord_flip() +
labs(
title = "Death ratio by Injury type",
subtitle = "Scotland 2013-2022",
caption = "Data source: Public Health Scotland",
y = "Deaths/Admissions ratio",
x = "",
fill = "total_deaths" ) +
geom_text(aes(label = round(death_ratio, 3)), hjust = -0.1, size = 3, color='red')
## Number of births in each hospital - table
```{r}
#subseting live births in 2022 grouped by hospital
newborns22 <- births %>%
# D201N is the code for home births (52 births in 2021)
filter(year==2022 & outcome=="Live" & hospital!= "D201N") %>%
group_by(hospital) %>%
summarize(babies_2022 = sum(smr02births)) %>%
arrange(desc(babies_2022))
```
```{r}
head(hospitals)
```
Finding column´s names in the hospitals dataset
```{r}
names(hospitals)
```
Joining births dataset with hospital dataset:
```{r}
births_2022 <- newborns22 %>%
left_join(hospitals, by=c("hospital" = "hospital_code")) %>%
select(hospital, hospital_name, health_board, babies_2022)
kable(births_2022,
caption = "Live births in Scottish hospitals in 2022") %>%
kable_styling(latex_options = "striped", font_size = 12)
```
## Number of live births in each NHS Health board - table
```{r}
#calculate births for each health board
births_hb<- births_2022 %>%
group_by(health_board) %>%
summarise(Newborns = sum(babies_2022)) %>%
arrange(desc(Newborns))
kable(births_hb,
caption = "Live births by Health Boards in 2022") %>%
kable_styling(latex_options = "striped", font_size = 12)
```
Joining our births & hospital data with the spatial data for the NHS Health boards boundaries:
```{r }
#join the spatial data with
births_spatial <- hb_spatial %>%
left_join(births_hb, by = c("HBCode" = "health_board"))
```
# Plotting the map
```{r}
baby2022_map <- ggplot(births_spatial, aes(fill = Newborns)) +
geom_sf(size = 0.1, color = "#0097a7") +
scale_fill_viridis_c(option = "mako", direction = -1) +
labs(
title = "Live births in Scotland 2022",
subtitle = "by Health Boards",
caption="Data from: Public health Scotland & Scottish Goverment spatial data") +
coord_sf() +
theme_void()
baby2022_map
```
See more data fun and drawings in the author´s website [www.inmaruiz.com](https://inmaruiz.com/index.php/machine-learning/)
### Software and packages used (or not used, but mentioned alt least)
**R:** R Core Team (2022). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria.
URLcitatiohttps://www.R-project.org/.
**janitor:** Firke S (2021). _janitor: Simple Tools for Examining and Cleaning Dirty Data_. R package version 2.1.0,
<https://CRAN.R-project.org/package=janitor>..
**Tidyverse:** Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R, Grolemund G, Hayes A, Henry L, Hester J,
Kuhn M, Pedersen TL, Miller E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V, Takahashi K,
Vaughan D, Wilke C, Woo K, Yutani H (2019). “Welcome to the tidyverse.” _Journal of Open Source
Software_, *4*(43), 1686. doi:10.21105/joss.01686 <https://doi.org/10.21105/joss.01686>.
**Knitr:** Yihui Xie (2022). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.40.
H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016
**kableExtra:** Zhu H (2021). _kableExtra: Construct Complex Table with 'kable' and Pipe Syntax_. R package version 1.3.4, https://CRAN.R-project.org/package=kableExtra.
**ggplot:** H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016
**formatR** Xie Y (2023). _formatR: Format R Code Automatically_. R package version 1.14,
<https://CRAN.R-project.org/package=formatR>.
**lubridate** Garrett Grolemund, Hadley Wickham (2011). Dates and Times Made Easy with lubridate. Journal of
Statistical Software, 40(3), 1-25. URL https://www.jstatsoft.org/v40/i03/.
**rgdal** Bivand R, Keitt T, Rowlingson B (2023). _rgdal: Bindings for the 'Geospatial' Data Abstraction Library_.
R package version 1.6-4, <https://CRAN.R-project.org/package=rgdal>.
**sp** Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial data in R. R News 5 (2),
https://cran.r-project.org/doc/Rnews/.
Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio, 2013. Applied spatial data analysis with R, Second
edition. Springer, NY. https://asdar-book.org/
**sf** Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10
(1), 439-446, https://doi.org/10.32614/RJ-2018-00.
**gridExtra** Auguie B (2017). _gridExtra: Miscellaneous Functions for "Grid" Graphics_. R package version 2.3,
<https://CRAN.R-project.org/package=gridExtra>.
**laticeExtra** Sarkar D, Andrews F (2022). _latticeExtra: Extra Graphical Utilities Based on Lattice_. R package
version 0.6-30, <https://CRAN.R-project.org/package=latticeExtra>.
**cowplot** Wilke C (2020). _cowplot: Streamlined Plot Theme and Plot Annotations for 'ggplot2'_. R package version 1.1.1,
<https://CRAN.R-project.org/package=cowplot>.
[Spatial Data Metadata Portal](https://spatialdata.gov.scot/geonetwork/srv/eng/catalog.search#/home), Scotland’s catalogue of spatial data.