forked from jules32/rmarkdown-website-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemographics.Rmd
818 lines (693 loc) · 28.3 KB
/
demographics.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
---
output:
html_document:
css: demographics-style.css
---
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-2B31CSSSMK"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-2B31CSSSMK');
</script>
</head>
<!---- .tabset-pill ---->
<style>
.nav-pills>li>a {
position: relative;
display: block;
padding: 10px 15px;
color: #000000;
font-weight: bold;
border-left: 1px solid #ddd;
border-right: 5px solid rgba(26, 26, 26, 0.4); /* #1a1a1a, opacity 0.6 */
border-bottom: 5px solid rgba(26, 26, 26, 0.4);
border-top: 1px solid #ddd;
border-radius: 5px;
}
.nav-pills>li.active>a,
.nav-pills>li.active>a:hover,
.nav-pills>li.active>a:focus {
color: #ffffff;
background-color: #000000;
}
</style>
## {.tabset .tabset-pills}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, out.width="100%", out.height="100%")
pacman::p_load(extrafont, dplyr, glue, htmltools, viridisLite, highcharter, ggplot2, ggiraph, patchwork)
deep_rooted <- swatches::read_palette(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/palettes/Deep Rooted.ase"))
med_age_tbl <- readr::read_rds(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/data/median-age-bubble.rds"))
heat_dat <- readr::read_rds(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/data/age-cases-heat.rds"))
ind_age_clean <- readr::read_rds(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/data/age-death-line.rds"))
line_data_date <- readr::read_rds(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/data/age-death-line-data-date.rds"))
heat_bubble_data_date <- readr::read_rds(glue("{rprojroot::find_rstudio_root_file()}/Indiana-COVID-19-Tracker/data/heat-bubble-data-date.rds"))
```
### Median Age of Cases, Tests, Deaths
```{r age_bubble, echo=FALSE}
# tooltip function
fntltp <- JS("function(){
return '<b>' + this.series.xAxis.categories[this.point.x] + '</b>' + '<br>' +
'Median Age' + ': ' + Highcharts.numberFormat(this.point.median_age, 1) + '<br>' +
'Deaths' + ': ' + this.point.weekly_deaths + '<br>' +
'Tests' + ': ' + this.point.weekly_tests.toLocaleString(undefined, {maximumFractionDigits: 0});
}")
data_date_text <- glue("Data Date: {heat_bubble_data_date}")
# specifications for data date text
plotline_datadate <- list(
# position determined by number of bars or bubbles along x axis
value = length(unique(med_age_tbl$end_date))-1,
# just want text and not line
width = 0,
label = list(
text = data_date_text,
verticalAlign = "top",
style = list(color = "white"),
# neg y value, in pixels, moves text up
textAlign = "right",
y = -90,
rotation = 0
)
)
# merges/overrides parts of tuft2 theme with my theme elements
theme_med_age <- hc_theme_merge(
hc_theme_tufte2(),
hc_theme(
chart = list(
backgroundColor = "black"
),
title = list(
align = "left",
style = list(
color = "white",
fontSize = "18px"
)
),
subtitle = list(
align = "left",
style = list(
color = "white",
fontSize = "14px"
)
),
caption = list(
align = "left",
style = list(
color = "white"
)
),
xAxis = list(
labels = list(
style = list(color = "white",
fontSize = "14px")
),
title = list(
text = "Week ending date",
style = list(color = "white",
fontSize = "16px")
),
tickInterval = 2
),
yAxis = list(
labels = list(
style = list(color = "white",
fontSize = "14px")
),
tickLength = 0,
gridLineWidth = 0
),
legend = list(
layout = "horizontal",
align = "center",
verticalAlign = "top",
itemStyle = list(color = "white")
)
)
)
# using 2 y-axes; opposite = TRUE places y-axis on right (or top if x-axis)
med_age_axis = list(
title = list(
text = "Median Age",
style = list(color = "white")
),
opposite = FALSE
)
tests_axis = list(
title = list(
text = "Tests",
style = list(color = "white")
),
opposite = TRUE
)
# "school starts" annotation is above this bar
annot_y <- med_age_tbl %>%
filter(end_date == "Aug 22") %>%
pull(median_age)
# highchart() creates htmlwidget
age_bubble <- highchart() %>%
# creates 2 y-axes on the same level (i.e. 1 chart, 2 axes)
hc_yAxis_multiples(
med_age_axis,
tests_axis
) %>%
# theres "datetime" for type arg but doesn't work with just dates so need categories
# plotLine adds a vertical line and text normally, only used text to add data date
hc_xAxis(categories = levels(med_age_tbl$end_date),
plotLines = list(plotline_datadate),
labels = list(step = 4)) %>%
# phantom series, so I can get square legend symbols instead of circles
hc_add_series(name = 'Median Age', type = "scatter", color = "#99875c",
id = 'legend_median', marker = list(symbol = "square")) %>%
hc_add_series(name = 'Tests', type = "scatter", color = "#755c99",
id = 'legend_tests', marker = list(symbol = "square")) %>%
# Bubble with median age and size = deaths
hc_add_series(name = 'Median Age', data = med_age_tbl, type = "bubble",
# z var determins bubble size, value needed when multiple numerics in dataset (I think)
hcaes(x = end_date, y = median_age, z = weekly_deaths, value = median_age),
# maxSize = general param for bubble sizes
zMax = 500, maxSize = "12%", marker = list(fillColor = '#99875c'),
# yAxis is the axis id, zIndex determines which series layer is outermost (larger = more outer)
yAxis = 0, zIndex = 0, tooltip = list(stickOnContact = FALSE),
# states has various js-like effects type features
# turned off effect of series being highlighted on hover
states = list(inactive = list(enabled = FALSE)), opacity = 0.95,
# turned legend of this series off since using legend of phantom series
# linkedTo makes it so this series is affected by selection of phantom series in the legend
showInLegend = FALSE, linkedTo = 'legend_median') %>%
# column chart for weekly tests
# pretty much same settings as bubble chart
hc_add_series(name = 'Tests', data = med_age_tbl, type = "column",
# Using shared tooltip and only wnated bubble series to show it
hcaes(x = end_date, y = weekly_tests, value = median_age, tooltip = FALSE),
# border params are for stroke around bars
yAxis = 1, zIndex = 0, color = '#755c99', borderColor = '#99875c',
borderRadius = 2, borderWidth = 2, opacity = 0.75,
states = list(inactive = list(enabled = FALSE),
hover = list(enabled = FALSE)),
showInLegend = FALSE, linkedTo = 'legend_tests') %>%
hc_annotations(
list(
# x is the bar number, "Aug 08" doesn't work
# xAxis must be specified else x is in pixels
labels = list(
list(
point = list(x = 22, y = annot_y + 4, xAxis = 0, yAxis = 0),
text = "<b>School Starts</b>",
style = list(color = "black"),
backgroundColor = "white"
)
)
)
) %>%
hc_tooltip(formatter = fntltp, shared = TRUE) %>%
# download opions; view fullscreen
hc_exporting(enabled = TRUE,
sourceWidth = 1280,
sourceHeight = 720) %>%
hc_legend(bubbleLegend = list(enabled = TRUE,
color = "#99875c",
# min and max size of circles in legend
# too small a spread between minSize, maxSize can crowd-out 2nd bubble label
minSize = 21,
maxSize = 40,
labels = list(style = list(color = "white",
fontSize = 12),
# integer format
format = '{value:.0f} deaths'),
ranges = list(
list(value = 500),
list(value = 250),
list(value = 100)))) %>%
hc_title(text = "Median Age of Weekly COVID-19 Cases, Weekly Tests and Deaths") %>%
hc_caption(text = "Source: Indiana Data Hub") %>%
hc_add_theme(theme_med_age)
htmlwidgets::saveWidget(age_bubble, file = glue("{rprojroot::find_rstudio_root_file()}/images/demographics/age-bubble.html"))
age_bubble
```
```{r, echo=FALSE}
text1 <- "This chart shows the median age (left y-axis) of COVID-19 cases for a particular week as bubbles and the number of tests (right y-axis) for a particular week as columns. The number of deaths for that week is represented by the size of the bubble."
ui1 <- "The median age of cases decreases as testing increases. Testing facilities expand outside of hospitals and into communities where a greater number of younger people get tested."
ui2 <- HTML("If testing remains <i>high</i>, relatively <i>constant</i>, and the median age is <i>younger</i>, then <i>fewer or similar</i> numbers of deaths are expected.")
ui3 <- HTML("If testing remains <i>high</i>, relatively <i>constant</i>, and the median age <i>rises</i>, then deaths are expected to <i>increase</i>.")
ui4 <- HTML("If testing is substantially <i>reduced or inconsistent</i> and the median age <i>rises</i>, then the behavior of the amount of deaths is more *uncertain*. The rise might be due to a lack of testing or may indicate viral spread from younger people to older ones.")
chart_feats <- details::details(
"Hovering over the bubbles shows the final date of the weekly interval, the median age, number of deaths, and number of tests for that week. The bar series or bubble series can selected by clicking on the opposite series in the legend. The menu button on the top right provides image and data download options and the ability to view fullscreen.",
summary = 'Chart Features',
lang = 'none'
) %>%
stringr::str_remove("```none") %>%
stringr::str_remove("```") %>%
HTML()
withTags(
div(
class = "card",
div(
class = "card-body",
h3("ABOUT"),
p(class = "card-text",
text1),
ul(
li(ui1),
li(ui2),
li(ui3),
li(ui4),
),
chart_feats
)
)
)
```
\
\
\
### Cases by Age
```{r age_heat, echo=FALSE}
caption_text <- "Sources: Indiana Data Hub, U.S. Census Bureau"
# specifications for data date text
plotline_datadate <- list(
# position determined by number of cells along x axis
value = length(unique(heat_dat$end_date))-3, width = 2,
label = list(
text = data_date_text,
verticalAlign = "top",
style = list(color = "white"),
# neg y value, in pixels, moves text up
textAlign = "right",
y = -110,
rotation = 0
)
)
# specifications for "school_starts" annotation
plotline_school_cases <- list(
color = "#fde725", value = 22, width = 2, zIndex = 5,
label = list(
text = "School Starts",
verticalAlign = "top",
style = list(color = "#fde725",
fontSize = "14px"),
textAlign = "center",
rotation = 0,
y = -50
)
)
# only need the line for the bottom heatmap
plotline_school_cap <- list(
color = "#fde725", value = 22, width = 2, zIndex = 5
)
# tooltip functions: date and age group: cases or prop cases
# number format
fntltp_capita <- JS("function(){
return '<b>' + this.series.xAxis.categories[this.point.x] + '</b>' + '<br>' +
this.series.yAxis.categories[this.point.y] + ': ' + Highcharts.numberFormat(this.point.value, 2);
}")
# string format adds commas per 1000
fntltp_cases <- JS("function(){
return '<b>' + this.series.xAxis.categories[this.point.x] + '</b>' + '<br>' +
this.series.yAxis.categories[this.point.y] + ': ' + this.point.value.toLocaleString(undefined, {maximumFractionDigits: 0});
}")
# merges/overrides parts of tuft2 theme with my theme elements
theme_cases <- hc_theme_merge(
hc_theme_tufte2(),
hc_theme(
chart = list(
backgroundColor = "black"
),
title = list(
align = "left",
style = list(
color = "white",
fontSize = "18px"
)
),
subtitle = list(
align = "left",
style = list(
color = "white",
fontSize = "16px"
)
),
xAxis = list(
labels = list(
style = list(color = "white",
fontSize = "14px")
),
title = list(
text = "Week ending date",
style = list(color = "white",
fontSize = "16px"),
# measure in pixels, pos value moves label up
offset = 50,
y = -30),
tickInterval = 2,
# move x-axis to top
opposite = TRUE
),
yAxis = list(
labels = list(
style = list(color = "white",
fontSize = "14px")
),
# neg offset moves y-axis able left
offset = -5,
tickLength = 0,
gridLineWidth = 0,
minorGridLineWidth = 0
),
legend = list(
layout = "horizontal",
align = "left",
verticalAlign = "top",
# moves legend closer to plot area; pixels
margin = -20
)
)
)
theme_cap <- hc_theme_merge(
hc_theme_tufte2(),
hc_theme(
chart = list(
backgroundColor = "black"
),
subtitle = list(
align = "left",
style = list(
color = "white",
fontSize = "16px"
)
),
caption = list(
align = "left",
style = list(
color = "white"
)
),
# bottom chart doesn't need dates axis
xAxis = list(
labels = list(
enabled = FALSE
)
),
yAxis = list(
labels = list(
style = list(color = "white",
fontSize = "14px")
),
# neg offset moves y-axis able left
offset = -5,
tickLength = 0,
gridLineWidth = 0,
minorGridLineWidth = 0
),
legend = list(
layout = "horizontal",
align = "left",
verticalAlign = "top"
)
)
)
# Indiana cases exceeded the range I had for my color scale (Based it on the Florida outbreak but the Hoosiers outdid Florida ::shrugs::). Attempting to add on to the end of the color range without changing the colors for previous values. It's a mess.
extended_palette <- as.character()
last_color <- last(inferno(n = 1500, begin = 0.10))
for (i in (1:100)) {
new_color <- prismatic::clr_lighten(last_color, shift = 0.01)
extended_palette[[i]] <- new_color
last_color <- new_color
}
# cases
high_cases <- highchart() %>%
hc_add_series(data = heat_dat, type = "heatmap",
# value needed when multiple numerics in dataset (I think)
hcaes(x = end_date, y = age_grp, value = weekly_cases)) %>%
hc_yAxis(categories = heat_dat$age_grp) %>%
# theres "datetime" for type arg but doesn't work with just dates so need categories
hc_xAxis(categories = levels(heat_dat$end_date),
# data date and school start annotation + vertical line
plotLines = list(plotline_school_cases, plotline_datadate),
labels = list(step = 4)) %>%
hc_colorAxis(stops = color_stops(1500, c(inferno(1500, begin = 0.10), extended_palette)),
# endontick=T only allows integers for min and max
endOnTick = FALSE, max = 3000,
# legend axis labels
labels = list(
style = list(color = "#ffffff")
)) %>%
hc_tooltip(formatter = fntltp_cases) %>%
hc_exporting(enabled = TRUE,
sourceWidth = 1280,
sourceHeight = 720) %>%
hc_title(text = "Weekly COVID-19 Cases by Age") %>%
hc_subtitle(text = "cases") %>%
hc_add_theme(theme_cases)
# cases per 1000
high_cap <- highchart() %>%
hc_add_series(data = heat_dat, type = "heatmap",
# value needed when multiple numerics in dataset (I think)
hcaes(x = end_date, y = age_grp, value = prop_cases)) %>%
hc_yAxis(categories = heat_dat$age_grp) %>%
# theres "datetime" for type arg but doesn't work with just dates so need categories
hc_xAxis(categories = levels(heat_dat$end_date),
# adds vertical line marking when school started
plotLines = list(plotline_school_cap),
labels = list(step = 2)) %>%
# 1500 adds more gradations (more continuous), begin = .10 starts palette in a lighter area
hc_colorAxis(stops = color_stops(1500, c(inferno(1500, begin = 0.10), extended_palette)),
# endontick=T only allows integers for min and max
endOnTick = FALSE, max = 4.0,
# legend axis labels
labels = list(
style = list(color = "#ffffff")
)) %>%
hc_tooltip(formatter = fntltp_capita) %>%
hc_exporting(enabled = TRUE,
sourceWidth = 1280,
sourceHeight = 720) %>%
hc_subtitle(text = "cases per 1000 residents per age group") %>%
hc_caption(text = caption_text) %>%
hc_add_theme(theme_cap)
htmlwidgets::saveWidget(high_cap, file = glue("{rprojroot::find_rstudio_root_file()}/images/demographics/heat-cap.html"))
htmlwidgets::saveWidget(high_cases, file = glue("{rprojroot::find_rstudio_root_file()}/images/demographics/heat-cases.html"))
hw_grid(list(high_cases, high_cap),
ncol = 1)
```
```{r, echo=FALSE}
text1 <- "Both heatmaps show a breakdown of weekly COVID-19 cases by age group."
ui1 <- "In the top heatmap, the intensity of color in each cell represents number of cases for that age range in a particular week."
ui2 <- "In the bottom heatmap, the number of cases is scaled to per 1000 residents per age group. For example on the week that ended on May 5th, there were 2.26 cases for every 1000 residents that were 80 yrs or older."
text2 <- "While raw totals can give us an idea about the age distribution of cases, it can sometimes be misleading. As of 2019, there are 1,752,893 residents in the 0 to 19 age group and 815,370 residents in the 40 to 49 age group, so 2000 cases may be a substantial signal of something for one group but not the other."
text3 <- "Higher intensity cells in the scaled cases heatmap could indicate outbreaks in more age-consistent venues."
example <- details::details(
"On May 5th in the top heatmap, the cell for 80 and older is on the lower end of the intensity scale, and in the bottom heatmap, the intensity of that cell is in the upper third. An explanation for this difference could be that there were outbreaks at long term care facilities. A similar instance involving the 20-29 group might indicate bars or colleges as a hotspot.",
summary = 'Example',
lang = 'none'
) %>%
stringr::str_remove("```none") %>%
stringr::str_remove("```") %>%
HTML()
chart_feats <- details::details(
"Hovering over the cells shows the final date of the weekly interval, the age group, and weekly case total or weekly scaled case total. The menu button on the top right provides image and data download options and the ability to view fullscreen.",
summary = 'Chart Features',
lang = 'none'
) %>%
stringr::str_remove("```none") %>%
stringr::str_remove("```") %>%
HTML()
withTags(
div(
class = "card",
div(
class = "card-body",
h3("ABOUT"),
p(class = "card-text",
text1),
ul(
li(ui1),
li(ui2)
),
p(class = "card-text",
text2, br()),
p(class = "card-text",
text3),
example,
chart_feats
)
)
)
```
\
\
\
### Deaths by Age
```{r age-deaths, echo=FALSE, warning=FALSE}
line_data_date <- ind_age_clean %>%
filter(end_date == max(end_date)) %>%
slice_tail() %>%
pull(end_date)
# # A very large backlog of deaths got added to the data on Feb 6, 2021. It made patterns for certain age groups unrecognizable so I decided to single out that day and just give it a point marker and label.
# data_dump <- ind_age_clean %>%
# filter(end_date == as.Date("2021-02-06") &
# !agegrp %in% c("0 to 19", "20 to 29", "30 to 39")) %>%
# rename(actual_total = weekly_total) %>%
# mutate(weekly_total = c(20, 50, 100, 200, 400))
#
# ind_age_cleaner <- ind_age_clean %>%
# filter(end_date != as.Date("2021-02-06") |
# agegrp %in% c("0 to 19", "20 to 29", "30 to 39"))
gen_plots <- function(data, Name) {
# Only want integer breaks. With small totals, ggplot creates breaks with decimal values
num_breaks <- data %>%
filter(weekly_total == max(weekly_total)) %>%
mutate(num_breaks = case_when(
weekly_total == 1 ~ 1,
max(weekly_total) > 4 ~ 5,
TRUE ~ max(weekly_total) + 1
)) %>%
slice_head() %>%
pull(num_breaks)
# last point is only a complete week's worth of data for 1 out of 7 days. I don't want the trend line affected by it, so I'm removing it from the geom_smooth data.
# line_data <- data %>%
# slice(-n())
g <- ggplot(data, aes(x = end_date, y = weekly_total)) +
geom_point_interactive(aes(tooltip = tooltip, data_id = tooltip),
color = "#99775c", size = 3) +
geom_smooth(
# data = line_data,
color = "#CC3D24FF",
se = FALSE, method = "loess",
span = 0.50, formula = 'y ~ x') +
scale_y_continuous(breaks= scales::pretty_breaks(num_breaks)) +
scale_x_date(position = "top", date_labels = "%b") +
labs(x = NULL, y = NULL, title = Name)
if (Name == "0 to 19") {
g <- g + ggrepel::geom_label_repel(data = data %>%
filter(end_date == as.Date("2020-08-10")|
end_date == as.Date("2021-08-03")),
aes(label = c("School Starts", "School Starts")),
fill = "black", color = "#fde725",
point.padding = 32, label.size = 0.80,
seed = 125, direction = "y")
}
# seed 125
# top charts have x-axis
if (Name == "0 to 19" | Name == "20 to 29" | Name == "30 to 39"){
g + theme(plot.title = element_text(color = "white",
family = "sans",
face = "bold.italic"),
legend.position = "none",
axis.text.x = element_text(color = "white",
size = 16,
family = "sans"),
axis.text.y = element_text(color = "white",
size = 16,
family = "sans"),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "black",
color = NA),
plot.background = element_rect(fill = "black",
color = NA),
panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = deep_rooted[[7]]))
} else {
# bottom charts
# g + geom_point_interactive(data = data %>%
# filter(end_date == as.Date("2021-02-06")),
# aes(tooltip = tooltip, data_id = tooltip),
# color = "#99775c", size = 3) +
# # label and point indicating day backlog of deaths enters db
# ggrepel::geom_label_repel(data = data %>%
# filter(end_date == as.Date("2021-02-06")),
# aes(label = actual_total), size = 6,
# color = "white", fill = "black",
# # nudge_x = .02, nudge_y = .40,
# segment.color = NA, label.size = NA) +
g + theme(plot.title = element_text(color = "white",
family = "sans",
face = "bold.italic"),
legend.position = "none",
axis.text.x = element_blank(),
axis.text.y = element_text(color = "white",
size = 14,
family = "sans"),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "black",
color = NA),
plot.background = element_rect(fill = "black",
color = NA),
panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = deep_rooted[[7]]))
}
}
# create all the giraphs
age_plots <- ind_age_clean %>%
# mutate(actual_total = NA) %>%
# bind_rows(data_dump) %>%
arrange(end_date, agegrp) %>%
group_by(agegrp) %>%
tidyr::nest() %>%
mutate(plots = purrr::map2(data, agegrp, ~gen_plots(.x, .y)))
# use patchwork to facet them
all_charts <- age_plots$plots[[1]] + age_plots$plots[[2]] + age_plots$plots[[3]] + age_plots$plots[[4]] + age_plots$plots[[5]] + age_plots$plots[[6]] + age_plots$plots[[7]] + age_plots$plots[[8]] +
plot_annotation(
title = "Weekly COVID-19 Deaths by Age Group",
subtitle = glue("Data Date: {line_data_date}"),
caption = "Source: Indiana Data Hub") &
theme(plot.title = element_text(color = "white",
size = 22,
family = "sans"),
plot.subtitle = element_text(color = "white",
size = 18,
family = "sans"),
plot.caption = element_text(color = "white",
size = 16,
family = "sans"),
panel.background = element_rect(fill = "black",
color = NA),
panel.border = element_blank(),
plot.background = element_rect(fill = "black",
color = NA))
# ggiraph options
age_deaths <- girafe(code = print(all_charts),
fonts = list(sans = "Cardo"),
options = list(
opts_tooltip(css = "font-size:12px;color:black;background-color:white;font-family:'Cardo';padding:2px")
),
width_svg = 25,
height_svg = 17
)
htmlwidgets::saveWidget(age_deaths, file = glue("{rprojroot::find_rstudio_root_file()}/images/demographics/age-deaths.html"))
age_deaths
```
```{r, echo=FALSE}
text1 <- "This group of charts shows weekly death counts and trend lines for each age group."
text2 <- "Yellow labels in the '0 to 19' chart indicate the points when school starts."
chart_feats <- chart_feats <- details::details(
"Hovering over the points show the final date of the weekly interval and the deaths for that week.",
summary = 'Chart Features',
lang = 'none'
) %>%
stringr::str_remove("```none") %>%
stringr::str_remove("```") %>%
HTML()
withTags(
div(
class = "card",
div(
class = "card-body",
h3("ABOUT"),
p(class = "card-text",
text1, br()),
p(class = "card-text",
text2),
chart_feats
)
)
)
```
\
\