-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23 CCG OIS Indicators maps facet_wrap.R
More file actions
137 lines (106 loc) · 4.69 KB
/
23 CCG OIS Indicators maps facet_wrap.R
File metadata and controls
137 lines (106 loc) · 4.69 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
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
# CCG OIS Indicators ggplot2 maps
# R Script: 23 CCG OIS Indicators maps facet_wrap.R
library(sf)
library(here)
library(dplyr)
library(ggplot2)
library(readxl)
library(janitor)
# 1. Download CCG Indicators
CCGdata <- function() {
if(!dir.exists("data")){dir.create("data")}
# Download master.zip file
download.file(
url = "https://files.digital.nhs.uk/48/4DB2CA/CCG_OIS_MAR_2022_Excels_Files.zip",
destfile = "data/CCGoutcomes.zip")
unzip(zipfile = "data/CCGoutcomes.zip",
exdir = "data",junkpaths = T)}
CCGdata()
# 2. Load Shapefile
CCG_boundaries <- st_read(here("data","Clinical_Commissioning_Groups_April_2021","CCG_APR_2021_EN_BFC.shp"))
# Check Shapefiles output
CCG_map <- ggplot() +
geom_sf(data = CCG_boundaries, size = 0.5, color = "black", fill ="coral") +
ggtitle("CCG Boundaries plot. April 2021") +
coord_sf()
CCG_map
# Save maps
ggsave("plots/01_Map_CCG_Boundaries_April2021.png", width = 6, height = 4)
# 3. Obtain NHS Indicators
CCGdata <- function() {if(!dir.exists("data")){dir.create("data")}
# Download master.zip file
download.file(
url = "https://files.digital.nhs.uk/48/4DB2CA/CCG_OIS_MAR_2022_Excels_Files.zip",
destfile = "data/CCGoutcomes.zip")
unzip(zipfile = "data/CCGoutcomes.zip",
exdir = "data",
junkpaths = T) }
CCGdata()
# List excel files on Data sub-directory
list.files (path = "./data" ,pattern = "xlsx$")
# 4. Data wrangling
# Select CCG OIS - Indicator 1.17
# Name: Record of stage of cancer at diagnosis
# File: CCG_1.17_I01968_D
# Description: The file contains indicator values for CCG OIS Indicator 1.17 - Percentage of new cases of cancer
# for which a valid stage is recorded at the time of diagnosis, 95% CI
# Reporting period: 2013,2014,2015,2016,2017,2018,2019
# Soure: NHS Digital National Disease Registration Service (NDRS)
cancer_data <- read_excel(here("data", "CCG_1.17_I01968_D.xlsx"),
sheet = 3, skip =13) %>%
clean_names() %>%
select("reporting_period","breakdown","ons_code",
"level","level_description","indicator_value")%>%
filter(level_description !="England")
cancer_data
cancer_data_sel <- cancer_data %>%
select("reporting_period","breakdown","ons_code",
"level","level_description","indicator_value") %>%
filter(level_description !="England")
cancer_data_sel
# 5. Split cancer data by years
# 5.1 obtain number of records by year
cancer_data_ren <- cancer_data_sel %>% select(date = reporting_period , breakdown, ons_code,
level, level_description,indicator_value )
cancer_data_ren
dist_years <- cancer_data_ren %>% select(date) %>% group_by(date) %>% count()
dist_years
# 2013 and 2019 data
cancer_20131419 <- cancer_data_ren %>% filter(date =="2013"|date =="2019")
cancer_20131419
# 6. PLOT Maps using facet_wrap
# Remove previous data sets from environment
# Just keep CCG_boundaries and indicator data sets for each year (2013,2019)
# 6.1 Load shapefile for all set of maps
CCG_boundaries_MAP <- CCG_boundaries
# 6.2 Draw cancer indicator map for each year
# 6.2.1 Prepare data for plots (save it in a workspace)
cancer131419 <- cancer_20131419 %>% select( date,breakdown, CCG21CD = ons_code,level,level_description,indicator_value)
cancer131419
# we only need the data_evolution dataset
rm(list=ls()[! ls() %in% c("CCG_boundaries_MAP","cancer131419")])
# We merge both shape file and metric data set using DPLYR
mapdata_1319 <- left_join(CCG_boundaries_MAP, cancer131419, by = "CCG21CD")
# 6.2.1 APPLY SPECIFIC PROJECTION TO MERGED DATA SET
# Apply specific projection (epsg:4326) to the map
# and indicator merged data set
mapdata_coord <- st_transform(mapdata_1319, "+init=epsg:4326")
# 7. Create MAPS using facet_wrap
# Map displaying New cases of cancer for 2013 and 2014.
# facet_wrap(~date) to display two maps on the same graphical output file
cancer_map<- mapdata_coord %>%
ggplot(color=qsec)+
aes(fill = indicator_value) +
geom_sf() +
facet_wrap(~date)+
labs(title = "Percent of new cases of cancer 2013 and 2019",
subtitle = "Valid stage recorded at diagnosis,(95% CI)",
caption = "Data source: NHS Digital (NDRS). CCG OIS Indicator 1.17") +
theme(legend.title=element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
rect = element_blank())
cancer_map
rm(list=ls()[! ls() %in% c("mapdata_coord","cancer_map")])
ggsave("plots/08_facet_wrap_ggplot2_maps_year_cases_of_cancer.png", width = 6, height = 4)