-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
1400 lines (1066 loc) · 25.4 KB
/
index.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Getting familiar with ggplot2 extensions"
subtitle: "Some of my most frequently used extensions"
author: "<br><br>Ashirwad Barnwal "
date: "<br>18-Feb-2021<br><br>"
output:
xaringan::moon_reader:
css: ["default", "kunoichi", "ninjutsu", "assets/custom.css"]
nature:
ratio: "16:9"
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
lib_dir: libs
---
```{r before-slides, include = FALSE}
# Prettify slide appearance
knitr::opts_chunk$set(
warning = FALSE,
message = FALSE,
fig.height = 6,
out.width = "100%",
comment = " ",
dpi = 300
)
xaringanExtra::use_panelset()
xaringanExtra::use_share_again()
# Add metadata
library(metathis)
meta() %>%
meta_name("github-repo" = "ashirwad/isu-graphics-ggext") %>%
meta_social(
title = "Getting familiar with ggplot2 extensions",
description = paste(
"Some of my most frequently used ggplot2 extensions.",
"Presented at ISU Graphics Group meeting in Spring 2021."
),
url = "https://isugg21-ggext.netlify.app",
image = "https://isugg21-ggext.netlify.app/social-media-card.png",
image_alt = paste(
"Title slide of Getting familiar with ggplot2 extensions:",
"Some of my most frequently used ggplot2 extensions,",
"presented at ISU Graphics Group meeting in Spring 2021 by Ashirwad Barnwal"
),
og_type = "website",
og_author = "Ashirwad Barnwal",
twitter_card_type = "summary_large_image"
)
```
```{r setup, include = FALSE}
## Load packages
# Path and data manipulation
library(conflicted)
library(here)
library(glue)
library(tidyverse)
conflict_prefer("filter", "dplyr")
# ggplot2 extensions and helpers
library(gghighlight)
library(ggrepel)
library(ggpubr)
library(patchwork)
library(ggalt)
library(ggridges)
library(countrycode)
library(ggimage)
library(ggthemes)
library(ggdark)
library(viridisLite)
# Miscellaneous
library(flipbookr)
library(emo)
library(default)
library(knitr)
default(include_url) <- list(height = "600px")
## Import & process data
midwest <- c(
"Illinois", "Indiana", "Iowa", "Kansas", "Michigan", "Minnesota", "Missouri",
"Nebraska", "North Dakota", "Ohio", "South Dakota", "Wisconsin"
)
fatal_crash_smry_by_state <- read_csv(
here("data", "usa-fatal-crash-totals-2018.csv")
) %>%
filter(state %in% midwest) %>%
rename(
death_rate_vmt = deaths_per_100m_vmt, death_rate_pop = deaths_per_100k_pop
) %>%
mutate(death_rate_vmt_above_mean = if_else(
death_rate_vmt > mean(death_rate_vmt), "Yes", "No"),
death_rate_vmt_z = death_rate_vmt
) %>%
mutate(across(death_rate_vmt_z, ~ round(as.vector(scale(.x)), 2)))
fatal_crash_monthly_counts <- read_csv(
here("data", "usa-fatal-crash-count-by-month.csv")
) %>%
rename(crash_year = Crash_Year) %>%
pivot_longer(
-crash_year, names_to = "crash_month", values_to = "fatal_crash_freq"
)
states_with_alcohol_problem <- read_csv(
here("data", "states-with-alcohol-problem-in-2018.csv")
)
```
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[Welcome!]
]
]]
---
class: split-40 white
.column.bg-main1[.content.vmiddle.center[
# Overview
<br><br>
### ggplot2 alone = `r ji("flexed_biceps")`
### ggplot2 + extensions = `r ji("flexed_biceps")` `r ji("flexed_biceps")`
<br>
### In this talk, I will introduce some of my .yellow[most frequently used] ggplot2 extensions.
<br>
### Let's get started!
]]
.column[.content.vmiddle.center[
```{r ggext-gallery, echo = FALSE}
include_url("https://exts.ggplot2.tidyverse.org/gallery/")
```
]]
---
# .purple[Data description]
.panelset[
.panel[.panel-name[Fatal crash summary by state, 2018]
```{r fatal-crash-smry-by-state}
# Only midwest
glimpse(fatal_crash_smry_by_state)
```
.center[Data source: [Insurance Institute for Highway Safety (IIHS)](https://www.iihs.org/topics/fatality-statistics/detail/state-by-state#fatal-crash-totals)]
]
.panel[.panel-name[Fatal crash monthly counts, 2010-19]
```{r fatal-crash-monthly-counts}
# Across the United States
glimpse(fatal_crash_monthly_counts)
```
.center[Data source: [Fatality Analysis Reporting System (FARS)](https://cdan.dot.gov/query)]
]
.panel[.panel-name[States with alcohol problem, 2017-18]
```{r states-with-alcohol-problem}
# Across the United States
glimpse(states_with_alcohol_problem)
```
.center[Data source: [Traffic safety facts (Table 7)](https://crashstats.nhtsa.dot.gov/Api/Public/ViewPublication/812826)]
]
]
---
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[gghighlight]
]
]]
---
class: middle center bg-main1
.font5[Example 1]
---
```{r gghigh-demo1, include = FALSE}
ggplot(fatal_crash_smry_by_state) +
aes(x = death_rate_vmt, y = state) +
geom_point() +
aes(
y = fct_reorder(
state, death_rate_vmt
)
) +
geom_segment(
aes(
x = 0, y = state,
xend = death_rate_vmt,
yend = state
)
) +
labs(
x = paste(
"Deaths per 100 million",
"vehicle miles traveled"
)
) +
labs(y = "Midwestern state") +
labs(
title = paste(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
)
) +
labs(caption = "Data source: IIHS") +
gghighlight(state == "Iowa")
```
`r chunk_reveal("gghigh-demo1")`
---
class: middle center bg-main1
.font5[Example 2]
---
```{r gghigh-demo2, include = FALSE}
ggplot(fatal_crash_monthly_counts) +
aes(
x = crash_month,
y = fatal_crash_freq
) +
aes(group = crash_year) +
geom_line() +
scale_x_discrete(limits = month.abb) +
aes(color = factor(crash_year)) +
labs(x = "Crash month") +
labs(y = "Fatal crash frequency") +
labs(color = "Crash year") +
labs(
title = paste(
"Motor vehicle fatal crash trend",
"in the U.S. by month, 2010–2019"
)
) +
labs(caption = "Data source: FARS") +
gghighlight(
max(fatal_crash_freq) > 3000
)
```
`r chunk_reveal("gghigh-demo2")`
---
class: split-40 white
.column.bg-main1[.content.vmiddle.center[
## Check [.yellow[gghighlight website]](https://yutannihilation.github.io/gghighlight/index.html) to learn more
## `r ji("point_right")`
]]
.column[.content.vmiddle[
```{r gghigh-website, echo = FALSE}
include_url("https://yutannihilation.github.io/gghighlight/index.html")
```
]]
---
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[ggrepel]
]
]]
---
class: middle center bg-main1
.font5[Example 1]
---
```{r ggrepel-demo1, include = FALSE}
ggplot(fatal_crash_smry_by_state) +
aes(
x = death_rate_pop,
y = death_rate_vmt
) +
geom_point() ->
ggrepel_baseplot
ggrepel_baseplot +
geom_text(aes(label = state)) ->
ggtext_ugly
ggrepel_baseplot +
geom_text_repel(
aes(label = state), seed = 123
) ->
ggtext_pretty
ggrepel_baseplot +
geom_label(aes(label = state)) ->
gglabel_ugly
ggrepel_baseplot +
geom_label_repel(
aes(label = state), seed = 123
) ->
gglabel_pretty
```
`r chunk_reveal("ggrepel-demo1")`
---
class: middle center bg-main1
.font5[Example 2]
---
```{r ggrepel-demo2, include = FALSE}
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2, #BREAK2
box.padding = 1, #BREAK3
segment.curvature = -0.2, #BREAK4
segment.ncp = 5, #BREAK5
segment.angle = 30 #BREAK6
) +
geom_smooth(
method = "lm", se = FALSE
) +
labs(
x = "Deaths per 100k population"
) +
labs(
y = "Deaths per 100 million VMT"
) +
labs(
title = paste(
"Relationship between motor",
"vehicle crash death rates",
"for midwest, 2018"
)
) +
labs(caption = "Data source: IIHS")
```
`r chunk_reveal("ggrepel-demo2", break_type = "non_seq")`
---
class: split-40 white
.column.bg-main1[.content.vmiddle.center[
## Check [.yellow[ggrepel website]](https://ggrepel.slowkow.com/index.html) to learn more
## `r ji("point_right")`
]]
.column[.content.vmiddle[
```{r ggrepel-website, echo = FALSE}
include_url("https://ggrepel.slowkow.com/index.html")
```
]]
---
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[ggpubr]
]
]]
---
class: middle center bg-main1
.font5[Example 1a]
---
```{r ggpubr-demo1, include = FALSE}
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending", #BREAK2
add = "segment", #BREAK3
add.params = list(size = 1), #BREAK4
dot.size = 9, #BREAK5
rotate = TRUE, #BREAK6
color = "death_rate_vmt_above_mean", #BREAK7
label = "death_rate_vmt", #BREAK8
font.label = list( #BREAK8
color = "white", size = 9, #BREAK8
vjust = 0.5 #BREAK8
), #BREAK8
legend.title = "Value above mean", #BREAK9
title = paste( #BREAK9
"Motor vehicle crash death rate", #BREAK9
"per midwestern state, 2018" #BREAK9
), #BREAK9
xlab = "Midwestern state", #BREAK9
ylab = "Deaths per 100 million VMT", #BREAK9
caption = "Data source: IIHS" #BREAK9
)
```
`r chunk_reveal("ggpubr-demo1", break_type = "non_seq", left_assign = TRUE)`
---
class: middle center bg-main1
.font5[Example 1b]
---
```{r ggpubr-demo2, include = FALSE}
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending", #BREAK2
add = "segment", #BREAK3
add.params = list(size = 2), #BREAK4
dot.size = 9, rotate = TRUE, #BREAK5
color = "death_rate_vmt_above_mean", #BREAK6
label = "death_rate_vmt_z", #BREAK7
font.label = list( #BREAK8
color = "white", size = 9, #BREAK8
vjust = 0.5 #BREAK8
), #BREAK8
legend.title = "Value above mean", #BREAK9
title = paste( #BREAK9
"Motor vehicle crash death", #BREAK9
"rate per midwestern state, 2018" #BREAK9
), #BREAK9
xlab = "Midwestern state", #BREAK9
ylab = paste( #BREAK9
"Deaths per 100 million VMT", #BREAK9
"(normalized)" #BREAK9
) #BREAK9
) +
geom_hline(
yintercept = 0,
linetype = 2, color = "lightgrey" #BREAK9
)
```
`r chunk_reveal("ggpubr-demo2", break_type = "non_seq", left_assign = TRUE)`
---
class: middle center bg-main1
.font5[Example 2]
---
```{r ggpubr-demo3, include = FALSE}
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year", #BREAK2
palette = plasma( #BREAK3
5, begin = 0.4, end = 0.8 #BREAK3
), #BREAK3
add = "boxplot", #BREAK4
add.params = list(fill = "white"), #BREAK5
title = paste( #BREAK6
"U.S. fatal traffic crash", #BREAK6
"distribution by month, 2015–19" #BREAK6
), #BREAK6
legend.title = "Crash year", #BREAK7
xlab = "", ylab = "" #BREAK8
) +
theme_economist() #BREAK9
```
`r chunk_reveal("ggpubr-demo3", break_type = "non_seq")`
---
# .purple[Case for violin plots]
> The data in each category is shifting overtime, as can clearly be seen in the "Raw" data view, yet the boxplots remain static. Violin plots are a good method for presenting the distribution of a dataset with more detail than is available with a traditional boxplot. This is not to say that using a boxplot is never appropriate, but if you are going to use a boxplot, it is important to make sure the underlying data is distrubuted in a way that important information is not hidden. – **Justin Matejka & George Fitzmaurice**
```{r violin-vs-boxplot, echo = FALSE}
include_graphics("https://d2f99xq7vri1nk.cloudfront.net/BoxViolinSmaller.gif")
```
.center[
Source: [.blue[Autodesk research]](https://www.autodesk.com/research/publications/same-stats-different-graphs)
]
---
class: split-40 white
.column.bg-main1[.content.vmiddle.center[
## Check [.yellow[ggpubr website]](https://rpkgs.datanovia.com/ggpubr/index.html) to learn more
## `r ji("point_right")`
]]
.column[.content.vmiddle[
```{r ggpubr-website, echo = FALSE}
include_url("https://rpkgs.datanovia.com/ggpubr/index.html")
```
]]
---
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[patchwork]
]
]]
---
class: middle center bg-main1
.font5[Example 1]
---
```{r ggpatch-demo0, include = FALSE}
# Create plots for demo
ggplot() ->
p1
ggplot() +
theme_bw() ->
p2
```
`r chunk_reveal("ggpatch-demo0")`
---
```{r ggpatch-demo1, include = FALSE}
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1 |
p2 |
p1 |
p2 ->
ggpatch_juxta
# Place on top of each other
p1 /
p2 /
p1 /
p2 ->
ggpatch_updown
# Hybrids
(p1 / p2) |
(p1 / p2) ->
ggpatch_hybrid1
(p1 | p2) /
(p1 | p2) ->
ggpatch_hybrid2
```
`r chunk_reveal("ggpatch-demo1")`
---
```{r ggpatch-demo2, include = FALSE}
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2 ->
ggpatch_hybrid4
# Asterisk operator
ggpatch_hybrid3 * theme_dark() ->
ggpatch_star1
ggpatch_hybrid4 * theme_dark() ->
ggpatch_star2
# Ampersand operator
ggpatch_hybrid3 & theme_dark() ->
ggpatch_amp1
ggpatch_hybrid4 & theme_dark() ->
ggpatch_amp2
# Annotation
ggpatch_amp2 +
plot_annotation(tag_levels = "A") ->
ggpatch_annot1
ggpatch_amp2[[3]] <- ggpatch_amp2[[3]] +
plot_layout(tag_level = "new")
ggpatch_amp2 +
plot_annotation(
tag_levels = c("A", "1")
)
```
`r chunk_reveal("ggpatch-demo2")`
---
class: middle center bg-main1
.font5[Example 2]
---
```{r ggpatch-demo3, include = FALSE}
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]] +
labs(title = NULL, caption = NULL) ->
ggpatch_final[[1]]
ggpatch_final[[2]] +
labs(title = NULL) ->
ggpatch_final[[2]]
ggpatch_final +
plot_layout(guides = "collect") +
plot_annotation(
title = paste( #BREAK2
"Motor vehicle crash death rate", #BREAK2
"per midwestern state, 2018" #BREAK2
), #BREAK2
caption = "Data source: IIHS", #BREAK3
theme = theme_pubr(), #BREAK4
tag_levels = "A", #BREAK5
tag_prefix = "Fig. " #BREAK6
) &
theme( #BREAK7
plot.tag = element_text(size = 10) #BREAK7
) #BREAK7
```
`r chunk_reveal("ggpatch-demo3")`
---
`r chunk_reveal("ggpatch-demo3", chunk_options = "fig.height = 7", break_type = "non_seq")`
---
class: split-40 white
.column.bg-main1[.content.vmiddle.center[
## Check [.yellow[patchwork website]](https://patchwork.data-imaginist.com/index.html) to learn more
## `r ji("point_right")`
]]
.column[.content.vmiddle[
```{r patchwork-website, echo = FALSE}
include_url("https://patchwork.data-imaginist.com/index.html")
```
]]
---
layout: false
class: bg-main3 split-30 hide-slide-number
.column[
]
.column.slide-in-right[.content.vmiddle[
.sliderbox.shade_main.pad1[
.font5[ggalt]
]
]]
---
class: middle center bg-main1
.font5[Example 1]
.font2[Heavily based on [.yellow[this blog post]](https://rud.is/b/2016/04/17/ggplot2-exercising-with-ggalt-dumbbells/)]
---
```{r ggalt-demo1a, include = FALSE}
rename(
states_with_alcohol_problem,
prop_2017 = alcohol_deaths_prop_2017,
prop_2018 = alcohol_deaths_prop_2018
) %>%
mutate(
diff = round(
prop_2018 - prop_2017, 2
),
diff_pretty = paste0(
"+", diff * 100
),
lab_2017 = ifelse(
state == "Montana",
paste0(
round(prop_2017 * 100), "%"
),
paste0(round(prop_2017 * 100))
),
lab_2018 = ifelse(
state == "Montana",
paste0(
round(prop_2018 * 100), "%"
),
paste0(round(prop_2018 * 100))
)
) ->
ggdumb_df
```
`r chunk_reveal("ggalt-demo1a")`
---
```{r ggalt-demo1b, include = FALSE}
ggdumb_df %>%
ggplot() +
aes(
y = state, x = prop_2017,
xend = prop_2018
) +
geom_dumbbell(
size = 1.5,
color = "#b2b2b2",
size_x = 3,
colour_x = "#9fb059",
size_xend = 3,
colour_xend = "#edae52"
) +
aes(y = fct_reorder(state, diff)) +
geom_text(
data = filter(
ggdumb_df, state == "Montana"
),
aes(
x = prop_2017, y = state,
label = "Year 2017"
),
color = "#9fb059", size = 3,
vjust = -2, fontface = "bold"
) ->
ggdumb_part1
```
`r chunk_reveal("ggalt-demo1b")`
---
```{r ggalt-demo1c, include = FALSE}
ggdumb_part1 +
geom_text(
data = filter(
ggdumb_df, state == "Montana"
),
aes(
x = prop_2018, y = state,
label = "Year 2018"
),
color = "#edae52", size = 3,
vjust = -2, fontface = "bold"
) +
scale_y_discrete(
expand = expansion(0, 1.2)
) +
geom_text(
aes(x = prop_2017, y = state,
label = lab_2017),
color = "#9fb059", size = 2.75,
vjust = 2.5
) +
geom_text(
aes(x = prop_2018, y = state,
label = lab_2018),
color = "#edae52", size = 2.75,
vjust = 2.5
) ->
ggdumb_part2
```
`r chunk_reveal("ggalt-demo1c")`
---
```{r ggalt-demo1d, include = FALSE}
ggdumb_part2 +
geom_rect(
aes(
xmin = 0.55, xmax = 0.675,
ymin = -Inf, ymax = Inf
),
fill = "#efefe3"
) +
geom_text(
aes(
y = state, x = 0.6125,
label = diff_pretty,
),
fontface = "bold", size = 3
) +
geom_text(
data = filter(
ggdumb_df, state == "Montana"
),
aes(
x = 0.6125, y = state,
label = "DIFF"
),
color = "#7a7d7e", size = 3.1,
vjust = -2, fontface = "bold"
) ->
ggdumb_part3
```
`r chunk_reveal("ggalt-demo1d")`
---
```{r ggalt-demo1e, include = FALSE}
ggdumb_part3 +
scale_x_continuous(
expand = c(0, 0),
limits = c(0, 0.675)
) +
labs(
x = NULL, y = NULL,
title = paste(
"States with alcohol problem",
"based on 2017-18 comparison"
),
subtitle = paste(
"Alcohol-impaired-driving",
"fatalities (% of total)"
),
caption = "Data source: FARS"
) +
theme_bw() +
theme(
panel.grid.major = element_blank()
) +
theme(
panel.grid.minor = element_blank()
) +
theme(
panel.border = element_blank()
) ->
ggdumb_part4
```
`r chunk_reveal("ggalt-demo1e")`
---
```{r ggalt-demo1f, include = FALSE}
ggdumb_part4 +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(
plot.title = element_text(
face = "bold"
)
) +
theme(
plot.subtitle = element_text(
face = "italic", size = 9,
margin = margin(b = 12)
)
) +
theme(
plot.caption = element_text(
size = 7, margin = margin(t = 12),
color = "#7a7d7e"
)
) +
geom_segment(
aes(
y = state, yend = state,
x = 0, xend = 0.5
),
color = "#b2b2b2", size = 0.15
)
```