Skip to content

Commit 8920da5

Browse files
committed
Adding interactivity so some plots
1 parent a3ae47c commit 8920da5

File tree

2 files changed

+1023
-39
lines changed

2 files changed

+1023
-39
lines changed

demographics/population/populationEstimates.Rmd

+39-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ output:
1919

2020
<main>
2121
```{r setup, include=FALSE}
22-
knitr::opts_chunk$set(echo=FALSE, message=FALSE, warning=FALSE, results=FALSE)
22+
knitr::opts_chunk$set(echo=FALSE, message=FALSE, warning=FALSE)
2323
library(tidyverse) ;
2424
library(httr) ;
2525
library(readxl) ;
@@ -29,7 +29,7 @@ library(ggiraph)
2929
3030
```
3131

32-
```{r}
32+
```{r, results=FALSE}
3333
3434
source("https://github.com/traffordDataLab/assets/raw/master/theme/ggplot2/theme_lab.R")
3535
@@ -45,6 +45,12 @@ theme_labP <- function () {
4545
)
4646
}
4747
48+
girafe_options <- list(opts_tooltip(use_fill = FALSE, opacity = 1, css = "background-color: #e7e6e1; color: #212121; padding: 0.1em; border-radius: 0.25em;"),
49+
opts_toolbar(saveaspng = FALSE),
50+
opts_hover(css = "fill-opacity: 1; stroke:white; stroke-opacity: 1; r: 2.5pt;")
51+
)
52+
53+
4854
tmp <- tempfile(fileext = ".xlsx")
4955
5056
GET(url = "https://www.ons.gov.uk/file?uri=/peoplepopulationandcommunity/populationandmigration/populationestimates/datasets/estimatesofthepopulationforenglandandwales/mid2011tomid2023detailedtimeserieseditionofthisdataset/myebtablesenglandwales20112023.xlsx",
@@ -119,7 +125,7 @@ mid2022_23_5y <- mid2022_23 %>%
119125

120126
<p>In Mid-year 2023, the Trafford population was 237,480, which is 829 more than the population in Mid-year 2022.</p>
121127

122-
<p>From Mid-year 2022 to Mid-year 2023, the population has grown by 0.4%, this is 0.9% less than GM at 1.3% growth and 0.6% less than England at 1%. Trafford has the lowest growth compared to the rest of GM authorities and the second lowest growth of 15 similar local Authorities.</p>
128+
<p>From Mid-year 2022 to Mid-year 2023, the population has grown by 0.4%, this is 0.9% less than Greater Manchester (GM) at 1.3% growth and 0.6% less than England at 1%. Trafford has the lowest growth compared to the rest of GM authorities and the second lowest growth of the average of 15 similar local authorities (Similar Authorities).</p>
123129

124130
<p>The median age in Trafford was 40.9 years, 0.1 years less than the median age in Mid-year 2022. Trafford had the third highest median age among GM authorities. GM's median age is 37.2 years, and England’s median age is 40.4 years.</p>
125131

@@ -155,11 +161,11 @@ ggplot(gmCompP, aes(x = `% Change`, y = area_name)) +
155161
</div>
156162
</div>
157163

158-
### Females aged 15 to 19 and Males aged 75 to 79 had the largest increase in growth
164+
### Females 15 to 19 and Males 75 to 79 age bands had the largest increase in growth
159165

160-
<p>Comparing the change from Mid-2022 to Mid-2023, females aged 15 to 19 had the largest increase in growth and number of residents at 5.7% and 372, respectively. Males aged 75 to 79 had the largest increase in growth at 5.2%; however, the largest increase in number of residents for Males was age band 15 to 19 at 199.</p>
166+
<p>Comparing the change from Mid-2022 to Mid-2023, females 15 to 19 age band had the largest increase in growth and number of residents at 5.7% and 372, respectively. Males 75 to 79 age band had the largest increase in growth at 5.2%; however, the largest increase in number of residents for Males was age band 15 to 19 at 199.</p>
161167

162-
<p>Three age bands had the largest decrease, 20 to 24 years with -3.2 (170 fewer females), 70 to 74 years with -3 (162 fewer females), and under 5 years with -2.9 (184 fewer females). Males aged 70 to 74 had the largest decrease in growth and number of residents with -4.3% and -211, respectively.</p>
168+
<p>Three age bands had the largest decrease, 20 to 24 years with -3.2% (170 fewer females), 70 to 74 years with -3% (162 fewer females), and under 5 years with -2.9% (184 fewer females). Males aged 70 to 74 had the largest decrease in growth and number of residents with -4.3% and -211, respectively.</p>
163169

164170
<p>Female population grew 0.27% while male population grew 0.44%</p>
165171

@@ -171,13 +177,18 @@ ggplot(gmCompP, aes(x = `% Change`, y = area_name)) +
171177
172178
temp <- mid2022_23_5y %>%
173179
mutate(value = case_when(sex == "Female" ~ value * -1, TRUE ~ value)) %>%
174-
mutate(sex = factor(sex, levels = c("Female", "Male")))
180+
mutate(sex = factor(sex, levels = c("Female", "Male"))) %>%
181+
pivot_wider(names_from = year,values_from = value) %>%
182+
mutate(tooltip = paste0("<strong>", sex, " ", age, "</strong><br/>",
183+
"<strong>2022: </strong> ", format(abs(`2022`),big.mark = ","), "<br/>",
184+
"<strong>2023: </strong> ", format(abs(`2023`),big.mark = ",")))
185+
175186
176-
ggplot() +
177-
geom_col(data = filter(temp, year == "2023"),
178-
aes(x = age, y = value, fill = sex), alpha = 0.8, width = 0.9)+
179-
geom_step(data = filter(temp, year == "2022"),
180-
aes(x = age, y = value, group = sex, colour = sex), stat = "identity", size = 1, direction = "mid") +
187+
gg <- ggplot(temp) +
188+
geom_col_interactive(
189+
aes(x = age, y = `2023`, fill = sex, tooltip = tooltip), alpha = 0.8, width = 0.9)+
190+
geom_step(
191+
aes(x = age, y = `2022`, group = sex, colour = sex), stat = "identity", size = 1, direction = "mid") +
181192
scale_fill_manual(values = c("#1d85a5", "#194a81")) +
182193
scale_colour_manual(values = c("#1d85a5", "#194a81")) +
183194
facet_share(~sex, dir = "h", scales = "free",reverse_num = TRUE) +
@@ -204,6 +215,9 @@ ggplot() +
204215
strip.background = element_rect(fill = "transparent", colour = NA),
205216
strip.text = element_text(size = 9, face = "bold", hjust = .5))
206217
218+
girafe(ggobj = gg, height_svg = 5,
219+
options = girafe_options)
220+
207221
#ggsave("pop_pyramid_2022.png", dpi = 300, scale = 1)
208222
209223
```
@@ -217,7 +231,11 @@ change <- mid2022_23_5y %>%
217231
pivot_wider(names_from = year, values_from = value) %>%
218232
mutate(Difference = `2023` - `2022`) %>%
219233
mutate(`% Change` = round(Difference*100/`2022`,1)) %>%
220-
mutate(sex = factor(sex, levels = c("Female", "Male")))
234+
mutate(sex = factor(sex, levels = c("Female", "Male"))) %>%
235+
mutate(tooltip = paste0("<strong>", sex, " ", age, "</strong><br/>",
236+
"<strong>% Change: </strong> ", `% Change`, "%<br/>",
237+
"<strong>Difference: </strong> ", Difference))
238+
221239
222240
stats <- change %>%
223241
group_by(sex) %>%
@@ -237,10 +255,10 @@ stats2 <- stats %>%
237255
<div class = "col-md-10">
238256

239257
```{r fig.height=6}
240-
#gg <-
258+
gg <-
241259
ggplot(change) +
242260
geom_hline(yintercept = 0, size = 1, colour = "black") +
243-
geom_col(aes(age,`% Change`, fill = sex), position = "dodge", width = 0.7) +
261+
geom_col_interactive(aes(age,`% Change`, fill = sex, tooltip = tooltip), position = "dodge", width = 0.7) +
244262
scale_fill_manual(values = c("#1d85a5", "#194a81"),
245263
guide = guide_legend(keyheight = unit(3, units = "mm"),
246264
keywidth=unit(3, units = "mm"))) +
@@ -259,6 +277,10 @@ stats2 <- stats %>%
259277
axis.text.y = element_text(size = 8, hjust = 0),
260278
legend.position = "top",
261279
legend.text = element_text(size = 8))
280+
281+
girafe(ggobj = gg, height_svg = 5,
282+
options = girafe_options)
283+
262284
```
263285
</div>
264286
<div class = "col-md-1">
@@ -267,9 +289,9 @@ stats2 <- stats %>%
267289

268290
### Population under 5 is decreasing in Trafford but increasing in GM
269291

270-
<p>In Trafford, from Mid-2022 to Mid-2023, the population under 5 decreased 2.2%, while in Greater Manchester (GM) it increased 0.4%, England remained unchanged at 0% change, and in Similar authorities decreased 0.3%. Trafford's decrease of 1.4% for the age band 20 to 24 showed the largest difference with GM's 3% increase. 35 to 39 year olds decreased 0.7% for Trafford while England (2.4%), GM (2.8%), and Similar authorities (2.8%) all increased. Similarly, 85 to 89 year olds decreased 0.1% for Trafford, while England (2.3%), GM (2.9%), and Similar authorities (2.6%) all increased.</p>
292+
<p>In Trafford, from Mid-2022 to Mid-2023, the population under 5 decreased 2.2%, while in Greater Manchester (GM) it increased 0.4%, England remained unchanged at 0% change, and in Similar Authorities decreased 0.3%. Trafford's decrease of 1.4% for the age band 20 to 24 showed the largest difference with GM's 3% increase. 35 to 39 year olds decreased 0.7% for Trafford while England (2.4%), GM (2.8%), and Similar Authorities (2.8%) all increased. Similarly, 85 to 89 year olds decreased 0.1% for Trafford, while England (2.3%), GM (2.9%), and Similar Authorities (2.6%) all increased.</p>
271293

272-
<p>Trafford's population increased for all broad age bands but at different rates than England, GM, and Similar authorities. The 18 to 64 year olds slightly increased for Trafford at 0.1% while in GM the increase was 1.5%. The 65+ year olds increased 0.6% while in England and Similar authorities increased 1.4% and 1.5%, respectively.</p>
294+
<p>Trafford's population increased for all broad age bands but at different rates than England, GM, and Similar Authorities. The 18 to 64 year olds slightly increased for Trafford at 0.1% while in GM the increase was 1.5%. The 65+ year olds increased 0.6% while in England and Similar Authorities increased 1.4% and 1.5%, respectively.</p>
273295

274296
<div class = "row">
275297

0 commit comments

Comments
 (0)