+ - 0:00:00
Notes for current slide
Notes for next slide

Getting familiar with ggplot2 extensions

Some of my most frequently used extensions



Ashirwad Barnwal   


18-Feb-2021

1 / 37

Welcome!

2 / 37

Overview



ggplot2 alone = 💪

ggplot2 + extensions = 💪 💪


In this talk, I will introduce some of my most frequently used ggplot2 extensions.


Let's get started!

3 / 37

Data description

# Only midwest
glimpse(fatal_crash_smry_by_state)
Rows: 12
Columns: 9
$ state <chr> "Illinois", "Indiana", "Iowa", "Kansas", ...
$ population <dbl> 12741080, 6691878, 3156145, 2911505, 9995...
$ vmt_millions <dbl> 107954, 81529, 33282, 32190, 102398, 6043...
$ fatal_crashes <dbl> 948, 774, 291, 366, 905, 349, 848, 201, 9...
$ deaths <dbl> 1031, 858, 318, 404, 974, 381, 921, 230, ...
$ death_rate_pop <dbl> 8.1, 12.8, 10.1, 13.9, 9.7, 6.8, 15.0, 11...
$ death_rate_vmt <dbl> 0.96, 1.05, 0.96, 1.26, 0.95, 0.63, 1.20,...
$ death_rate_vmt_above_mean <chr> "No", "Yes", "No", "Yes", "No", "No", "Ye...
$ death_rate_vmt_z <dbl> -0.36, 0.11, -0.36, 1.23, -0.42, -2.11, 0...

Data source: Insurance Institute for Highway Safety (IIHS)

# Across the United States
glimpse(fatal_crash_monthly_counts)
Rows: 120
Columns: 3
$ crash_year <dbl> 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 20...
$ crash_month <chr> "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "...
$ fatal_crash_freq <dbl> 2101, 1830, 2213, 2552, 2704, 2569, 2852, 2825, 27...

Data source: Fatality Analysis Reporting System (FARS)

# Across the United States
glimpse(states_with_alcohol_problem)
Rows: 16
Columns: 3
$ state <chr> "Alaska", "Arizona", "Colorado", "Georgia"...
$ alcohol_deaths_prop_2017 <dbl> 0.28, 0.27, 0.27, 0.23, 0.28, 0.23, 0.22, ...
$ alcohol_deaths_prop_2018 <dbl> 0.36, 0.28, 0.30, 0.25, 0.30, 0.28, 0.25, ...

Data source: Traffic safety facts (Table 7)

4 / 37

gghighlight

5 / 37

Example 1

6 / 37
ggplot(fatal_crash_smry_by_state)

6 / 37
ggplot(fatal_crash_smry_by_state) +
aes(x = death_rate_vmt, y = state)

6 / 37
ggplot(fatal_crash_smry_by_state) +
aes(x = death_rate_vmt, y = state) +
geom_point()

6 / 37
ggplot(fatal_crash_smry_by_state) +
aes(x = death_rate_vmt, y = state) +
geom_point() +
aes(
y = fct_reorder(
state, death_rate_vmt
)
)

6 / 37
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
)
)

6 / 37
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"
)
)

6 / 37
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")

6 / 37
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"
)
)

6 / 37
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")

6 / 37
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")

6 / 37

Example 2

7 / 37
ggplot(fatal_crash_monthly_counts)

7 / 37
ggplot(fatal_crash_monthly_counts) +
aes(
x = crash_month,
y = fatal_crash_freq
)

7 / 37
ggplot(fatal_crash_monthly_counts) +
aes(
x = crash_month,
y = fatal_crash_freq
) +
aes(group = crash_year)

7 / 37
ggplot(fatal_crash_monthly_counts) +
aes(
x = crash_month,
y = fatal_crash_freq
) +
aes(group = crash_year) +
geom_line()

7 / 37
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)

7 / 37
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))

7 / 37
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")

7 / 37
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")

7 / 37
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")

7 / 37
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"
)
)

7 / 37
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")

7 / 37
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
)

7 / 37

Check gghighlight website to learn more

👉

8 / 37

ggrepel

9 / 37

Example 1

10 / 37
ggplot(fatal_crash_smry_by_state)

10 / 37
ggplot(fatal_crash_smry_by_state) +
aes(
x = death_rate_pop,
y = death_rate_vmt
)

10 / 37
ggplot(fatal_crash_smry_by_state) +
aes(
x = death_rate_pop,
y = death_rate_vmt
) +
geom_point()

10 / 37
ggplot(fatal_crash_smry_by_state) +
aes(
x = death_rate_pop,
y = death_rate_vmt
) +
geom_point() ->
ggrepel_baseplot
10 / 37
ggplot(fatal_crash_smry_by_state) +
aes(
x = death_rate_pop,
y = death_rate_vmt
) +
geom_point() ->
ggrepel_baseplot
ggrepel_baseplot

10 / 37
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))

10 / 37
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
10 / 37
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

10 / 37
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
)

10 / 37
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
10 / 37
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

10 / 37
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))

10 / 37
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
10 / 37
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

10 / 37
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
)

10 / 37
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
10 / 37

Example 2

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
) +
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")

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2,
) +
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")

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2,
box.padding = 1,
) +
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")

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2,
box.padding = 1,
segment.curvature = -0.2,
) +
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")

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2,
box.padding = 1,
segment.curvature = -0.2,
segment.ncp = 5,
) +
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")

11 / 37
ggrepel_baseplot +
geom_label_repel(
aes(label = state),
seed = 123,
nudge_x = 0.2,
box.padding = 1,
segment.curvature = -0.2,
segment.ncp = 5,
segment.angle = 30
) +
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")

11 / 37

Check ggrepel website to learn more

👉

12 / 37

ggpubr

13 / 37

Example 1a

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
dot.size = 9,
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
dot.size = 9,
rotate = TRUE,
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
dot.size = 9,
rotate = TRUE,
color = "death_rate_vmt_above_mean",
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
dot.size = 9,
rotate = TRUE,
color = "death_rate_vmt_above_mean",
label = "death_rate_vmt",
font.label = list(
color = "white", size = 9,
vjust = 0.5
),
)
lol_chart

14 / 37
lol_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state",
y = "death_rate_vmt",
sorting = "descending",
add = "segment",
add.params = list(size = 1),
dot.size = 9,
rotate = TRUE,
color = "death_rate_vmt_above_mean",
label = "death_rate_vmt",
font.label = list(
color = "white", size = 9,
vjust = 0.5
),
legend.title = "Value above mean",
title = paste(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
xlab = "Midwestern state",
ylab = "Deaths per 100 million VMT",
caption = "Data source: IIHS"
)
lol_chart

14 / 37

Example 1b

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
dot.size = 9, rotate = TRUE,
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
dot.size = 9, rotate = TRUE,
color = "death_rate_vmt_above_mean",
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
dot.size = 9, rotate = TRUE,
color = "death_rate_vmt_above_mean",
label = "death_rate_vmt_z",
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
dot.size = 9, rotate = TRUE,
color = "death_rate_vmt_above_mean",
label = "death_rate_vmt_z",
font.label = list(
color = "white", size = 9,
vjust = 0.5
),
) +
geom_hline(
yintercept = 0,
)
dev_chart

15 / 37
dev_chart <- ggdotchart(
data = fatal_crash_smry_by_state,
x = "state", y = "death_rate_vmt_z",
sorting = "descending",
add = "segment",
add.params = list(size = 2),
dot.size = 9, rotate = TRUE,
color = "death_rate_vmt_above_mean",
label = "death_rate_vmt_z",
font.label = list(
color = "white", size = 9,
vjust = 0.5
),
legend.title = "Value above mean",
title = paste(
"Motor vehicle crash death",
"rate per midwestern state, 2018"
),
xlab = "Midwestern state",
ylab = paste(
"Deaths per 100 million VMT",
"(normalized)"
)
) +
geom_hline(
yintercept = 0,
linetype = 2, color = "lightgrey"
)
dev_chart

15 / 37

Example 2

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
add.params = list(fill = "white"),
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
add.params = list(fill = "white"),
title = paste(
"U.S. fatal traffic crash",
"distribution by month, 2015–19"
),
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
add.params = list(fill = "white"),
title = paste(
"U.S. fatal traffic crash",
"distribution by month, 2015–19"
),
legend.title = "Crash year",
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
add.params = list(fill = "white"),
title = paste(
"U.S. fatal traffic crash",
"distribution by month, 2015–19"
),
legend.title = "Crash year",
xlab = "", ylab = ""
)

16 / 37
fatal_crash_monthly_counts %>%
filter(
crash_year %in% c(2015:2019)
) %>%
ggviolin(
x = "crash_year",
y = "fatal_crash_freq",
fill = "crash_year",
palette = plasma(
5, begin = 0.4, end = 0.8
),
add = "boxplot",
add.params = list(fill = "white"),
title = paste(
"U.S. fatal traffic crash",
"distribution by month, 2015–19"
),
legend.title = "Crash year",
xlab = "", ylab = ""
) +
theme_economist()

16 / 37

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

17 / 37

Check ggpubr website to learn more

👉

18 / 37

patchwork

19 / 37

Example 1

20 / 37
# Create plots for demo
ggplot()

20 / 37
# Create plots for demo
ggplot() ->
p1
20 / 37
# Create plots for demo
ggplot() ->
p1
ggplot()

20 / 37
# Create plots for demo
ggplot() ->
p1
ggplot() +
theme_bw()

20 / 37
# Create plots for demo
ggplot() ->
p1
ggplot() +
theme_bw() ->
p2
20 / 37
# Fill a grid
p1

20 / 37
# Fill a grid
p1 +
p2

20 / 37
# Fill a grid
p1 +
p2 +
p1

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1 |
p2

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1 |
p2 |
p1

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1 |
p2 |
p1 |
p2

20 / 37
# Fill a grid
p1 +
p2 +
p1 +
p2 ->
ggpatch_grid
# Place side-by-side
p1 |
p2 |
p1 |
p2 ->
ggpatch_juxta
20 / 37
# 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

20 / 37
# 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

20 / 37
# 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

20 / 37
# 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

20 / 37
# 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
20 / 37
# 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)

20 / 37
# 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)

20 / 37
# 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
20 / 37
# 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)

20 / 37
# 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)

20 / 37
# 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
20 / 37
(p1 | p2) / p1

20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2

20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2

20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2 ->
ggpatch_hybrid4
20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2 ->
ggpatch_hybrid4
# Asterisk operator
ggpatch_hybrid3 * theme_dark()

20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2 ->
ggpatch_hybrid4
# Asterisk operator
ggpatch_hybrid3 * theme_dark() ->
ggpatch_star1
20 / 37
(p1 | p2) / p1 ->
ggpatch_hybrid3
p1 | p2 |
p1 / p2 ->
ggpatch_hybrid4
# Asterisk operator
ggpatch_hybrid3 * theme_dark() ->
ggpatch_star1
ggpatch_hybrid4 * theme_dark()

20 / 37
(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
20 / 37
(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()

20 / 37
(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
20 / 37
(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()

20 / 37
(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
20 / 37
(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

20 / 37
(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")

20 / 37
(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
20 / 37
(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]]
20 / 37
(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")
20 / 37
(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

20 / 37
(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")
)

20 / 37

Example 2

21 / 37
(lol_chart / dev_chart)

21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]]

21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]] +
labs(title = NULL, caption = NULL)

21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]] +
labs(title = NULL, caption = NULL) ->
ggpatch_final[[1]]
21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]] +
labs(title = NULL, caption = NULL) ->
ggpatch_final[[1]]
ggpatch_final[[2]]

21 / 37
(lol_chart / dev_chart) ->
ggpatch_final
ggpatch_final[[1]] +
labs(title = NULL, caption = NULL) ->
ggpatch_final[[1]]
ggpatch_final[[2]] +
labs(title = NULL)

21 / 37
(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]]
21 / 37
(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

21 / 37
(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")

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
tag_levels = "A",
tag_prefix = "Fig. "
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
tag_levels = "A",
tag_prefix = "Fig. "
) &
theme(
plot.tag = element_text(size = 10)
)

21 / 37
(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(
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
tag_levels = "A",
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
tag_levels = "A",
tag_prefix = "Fig. "
)

21 / 37
(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(
"Motor vehicle crash death rate",
"per midwestern state, 2018"
),
caption = "Data source: IIHS",
theme = theme_pubr(),
tag_levels = "A",
tag_prefix = "Fig. "
) &
theme(
plot.tag = element_text(size = 10)
)

21 / 37

Check patchwork website to learn more

👉

22 / 37

ggalt

23 / 37

Example 1

Heavily based on this blog post

24 / 37
rename(
states_with_alcohol_problem,
prop_2017 = alcohol_deaths_prop_2017,
prop_2018 = alcohol_deaths_prop_2018
)
# A tibble: 16 x 3
state prop_2017 prop_2018
<chr> <dbl> <dbl>
1 Alaska 0.28 0.36
2 Arizona 0.27 0.28
3 Colorado 0.27 0.3
4 Georgia 0.23 0.25
5 Maine 0.28 0.3
6 Minnesota 0.23 0.28
7 Mississippi 0.22 0.25
8 Montana 0.31 0.43
9 New Hampshire 0.26 0.33
10 New Jersey 0.19 0.22
11 New York 0.290 0.33
12 North Carolina 0.28 0.290
13 South Dakota 0.28 0.35
14 Utah 0.2 0.23
15 Wisconsin 0.31 0.34
16 Puerto Rico 0.31 0.4
24 / 37
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))
)
)
# A tibble: 16 x 7
state prop_2017 prop_2018 diff diff_pretty lab_2017 lab_2018
<chr> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 Alaska 0.28 0.36 0.08 +8 28 36
2 Arizona 0.27 0.28 0.01 +1 27 28
3 Colorado 0.27 0.3 0.03 +3 27 30
4 Georgia 0.23 0.25 0.02 +2 23 25
5 Maine 0.28 0.3 0.02 +2 28 30
6 Minnesota 0.23 0.28 0.05 +5 23 28
7 Mississippi 0.22 0.25 0.03 +3 22 25
8 Montana 0.31 0.43 0.12 +12 31% 43%
9 New Hampshire 0.26 0.33 0.07 +7 26 33
10 New Jersey 0.19 0.22 0.03 +3 19 22
11 New York 0.290 0.33 0.04 +4 29 33
12 North Carolina 0.28 0.290 0.01 +1 28 29
13 South Dakota 0.28 0.35 0.07 +7 28 35
14 Utah 0.2 0.23 0.03 +3 20 23
15 Wisconsin 0.31 0.34 0.03 +3 31 34
16 Puerto Rico 0.31 0.4 0.09 +9 31 40
24 / 37
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
24 / 37
ggdumb_df
# A tibble: 16 x 7
state prop_2017 prop_2018 diff diff_pretty lab_2017 lab_2018
<chr> <dbl> <dbl> <dbl> <chr> <chr> <chr>
1 Alaska 0.28 0.36 0.08 +8 28 36
2 Arizona 0.27 0.28 0.01 +1 27 28
3 Colorado 0.27 0.3 0.03 +3 27 30
4 Georgia 0.23 0.25 0.02 +2 23 25
5 Maine 0.28 0.3 0.02 +2 28 30
6 Minnesota 0.23 0.28 0.05 +5 23 28
7 Mississippi 0.22 0.25 0.03 +3 22 25
8 Montana 0.31 0.43 0.12 +12 31% 43%
9 New Hampshire 0.26 0.33 0.07 +7 26 33
10 New Jersey 0.19 0.22 0.03 +3 19 22
11 New York 0.290 0.33 0.04 +4 29 33
12 North Carolina 0.28 0.290 0.01 +1 28 29
13 South Dakota 0.28 0.35 0.07 +7 28 35
14 Utah 0.2 0.23 0.03 +3 20 23
15 Wisconsin 0.31 0.34 0.03 +3 31 34
16 Puerto Rico 0.31 0.4 0.09 +9 31 40
24 / 37
ggdumb_df %>%
ggplot()

24 / 37
ggdumb_df %>%
ggplot() +
aes(
y = state, x = prop_2017,
xend = prop_2018
)

24 / 37
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"
)

24 / 37
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))

24 / 37
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"
)

24 / 37
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
24 / 37
ggdumb_part1

24 / 37
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"
)

24 / 37
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)
)

24 / 37
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
)

24 / 37
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
)

24 / 37
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
24 / 37
ggdumb_part2

24 / 37
ggdumb_part2 +
geom_rect(
aes(
xmin = 0.55, xmax = 0.675,
ymin = -Inf, ymax = Inf
),
fill = "#efefe3"
)

24 / 37
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
)

24 / 37
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"
)

24 / 37
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
24 / 37
ggdumb_part3

24 / 37
ggdumb_part3 +
scale_x_continuous(
expand = c(0, 0),
limits = c(0, 0.675)
)

24 / 37
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"
)

24 / 37
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()

24 / 37
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()
)

24 / 37
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()
)

24 / 37
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()
)

24 / 37
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
24 / 37
ggdumb_part4

24 / 37
ggdumb_part4 +
theme(axis.ticks = element_blank())

24 / 37
ggdumb_part4 +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank())

24 / 37
ggdumb_part4 +
theme(axis.ticks = element_blank()) +
theme(axis.text.x = element_blank()) +
theme(
plot.title = element_text(
face = "bold"
)
)

24 / 37
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)
)
)

24 / 37
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"
)
)

24 / 37
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
)

24 / 37

Example 2

25 / 37
fatal_crash_smry_by_state
# A tibble: 12 x 9
state population vmt_millions fatal_crashes deaths death_rate_pop
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Illi~ 12741080 107954 948 1031 8.1
2 Indi~ 6691878 81529 774 858 12.8
3 Iowa 3156145 33282 291 318 10.1
4 Kans~ 2911505 32190 366 404 13.9
5 Mich~ 9995915 102398 905 974 9.7
6 Minn~ 5611179 60438 349 381 6.8
7 Miss~ 6126452 76595 848 921 15
8 Nebr~ 1929268 20975 201 230 11.9
9 Nort~ 760077 9856 95 105 13.8
10 Ohio 11689442 114474 996 1068 9.1
11 Sout~ 882235 9719 110 130 14.7
12 Wisc~ 5813568 65885 530 588 10.1
# ... with 3 more variables: death_rate_vmt <dbl>,
# death_rate_vmt_above_mean <chr>, death_rate_vmt_z <dbl>
25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2)
# A tibble: 2 x 9
state population vmt_millions fatal_crashes deaths death_rate_pop
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Sout~ 882235 9719 110 130 14.7
2 Kans~ 2911505 32190 366 404 13.9
# ... with 3 more variables: death_rate_vmt <dbl>,
# death_rate_vmt_above_mean <chr>, death_rate_vmt_z <dbl>
25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state
# A tibble: 12 x 9
state population vmt_millions fatal_crashes deaths death_rate_pop
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Illi~ 12741080 107954 948 1031 8.1
2 Indi~ 6691878 81529 774 858 12.8
3 Iowa 3156145 33282 291 318 10.1
4 Kans~ 2911505 32190 366 404 13.9
5 Mich~ 9995915 102398 905 974 9.7
6 Minn~ 5611179 60438 349 381 6.8
7 Miss~ 6126452 76595 848 921 15
8 Nebr~ 1929268 20975 201 230 11.9
9 Nort~ 760077 9856 95 105 13.8
10 Ohio 11689442 114474 996 1068 9.1
11 Sout~ 882235 9719 110 130 14.7
12 Wisc~ 5813568 65885 530 588 10.1
# ... with 3 more variables: death_rate_vmt <dbl>,
# death_rate_vmt_above_mean <chr>, death_rate_vmt_z <dbl>
25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
)

25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
) +
geom_point()

25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
) +
geom_point() +
geom_encircle(
data = ggspike_df,
color = "red",
s_shape = 0.3, expand = 0.03,
spread = 0.01
)

25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
) +
geom_point() +
geom_encircle(
data = ggspike_df,
color = "red",
s_shape = 0.3, expand = 0.03,
spread = 0.01
) +
geom_point(
data = ggspike_df,
color = "red"
)

25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
) +
geom_point() +
geom_encircle(
data = ggspike_df,
color = "red",
s_shape = 0.3, expand = 0.03,
spread = 0.01
) +
geom_point(
data = ggspike_df,
color = "red"
) +
geom_spikelines(
data = ggspike_df,
linetype = 2
)

25 / 37
fatal_crash_smry_by_state %>%
slice_max(death_rate_vmt, n = 2) ->
ggspike_df
fatal_crash_smry_by_state %>%
ggplot(
aes(
x = death_rate_vmt,
y = death_rate_pop
)
) +
geom_point() +
geom_encircle(
data = ggspike_df,
color = "red",
s_shape = 0.3, expand = 0.03,
spread = 0.01
) +
geom_point(
data = ggspike_df,
color = "red"
) +
geom_spikelines(
data = ggspike_df,
linetype = 2
) ->
ggspike_part1
25 / 37
ggspike_part1

25 / 37
ggspike_part1 +
geom_label_repel(
data = ggspike_df,
aes(
label = glue(
"({death_rate_vmt}, ",
"{death_rate_pop})"
)
),
box.padding = 1, color = "red"
)

25 / 37
ggspike_part1 +
geom_label_repel(
data = ggspike_df,
aes(
label = glue(
"({death_rate_vmt}, ",
"{death_rate_pop})"
)
),
box.padding = 1, color = "red"
) +
labs(
x = "Deaths per 100 million VMT",
y = "Deaths per 100k population",
title = paste(
"Relationship between motor",
"vehicle crash death rates",
"for midwest, 2018"
),
caption = "Data source: IIHS"
)

25 / 37

Check ggalt vignette to learn more

👉

26 / 37

ggridges

27 / 37

Example

28 / 37
fatal_crash_monthly_counts
# A tibble: 120 x 3
crash_year crash_month fatal_crash_freq
<dbl> <chr> <dbl>
1 2010 Jan 2101
2 2010 Feb 1830
3 2010 Mar 2213
4 2010 Apr 2552
5 2010 May 2704
6 2010 Jun 2569
7 2010 Jul 2852
8 2010 Aug 2825
9 2010 Sep 2799
10 2010 Oct 2827
# ... with 110 more rows
28 / 37
fatal_crash_monthly_counts %>%
ggplot(
aes(
x = fatal_crash_freq,
y = crash_month,
fill = stat(x)
)
)

28 / 37
fatal_crash_monthly_counts %>%
ggplot(
aes(
x = fatal_crash_freq,
y = crash_month,
fill = stat(x)
)
) +
geom_density_ridges_gradient(
scale = 3,
rel_min_height = 0.01
)

28 / 37
fatal_crash_monthly_counts %>%
ggplot(
aes(
x = fatal_crash_freq,
y = crash_month,
fill = stat(x)
)
) +
geom_density_ridges_gradient(
scale = 3,
rel_min_height = 0.01
) +
scale_y_discrete(
limits = rev(month.abb)
)

28 / 37
fatal_crash_monthly_counts %>%
ggplot(
aes(
x = fatal_crash_freq,
y = crash_month,
fill = stat(x)
)
) +
geom_density_ridges_gradient(
scale = 3,
rel_min_height = 0.01
) +
scale_y_discrete(
limits = rev(month.abb)
) +
scale_fill_viridis_c(
name = "Fatal Crash Freq.",
option = "C"
)

28 / 37
fatal_crash_monthly_counts %>%
ggplot(
aes(
x = fatal_crash_freq,
y = crash_month,
fill = stat(x)
)
) +
geom_density_ridges_gradient(
scale = 3,
rel_min_height = 0.01
) +
scale_y_discrete(
limits = rev(month.abb)
) +
scale_fill_viridis_c(
name = "Fatal Crash Freq.",
option = "C"
) +
labs(
x = "Fatal crash frequency",
y = "Crash month",
title = paste(
"Motor vehicle monthly fatal",
"crashes in the U.S., 2010–2019"
)
)

28 / 37

Check ggridges website to learn more

👉

29 / 37

ggimage

30 / 37

Example

Data source: WHO

31 / 37
tibble(
country = c(
"Dominican Republic", "Zimbabwe",
"Saudi Arabia", "Tonga",
"Thailand", "Armenia"
),
deaths_per_100k_pop = c(
193.86, 126.03,
102.19, 98.83,
97.67, 61.02
)
)
# A tibble: 6 x 2
country deaths_per_100k_pop
<chr> <dbl>
1 Dominican Republic 194.
2 Zimbabwe 126.
3 Saudi Arabia 102.
4 Tonga 98.8
5 Thailand 97.7
6 Armenia 61.0
31 / 37
tibble(
country = c(
"Dominican Republic", "Zimbabwe",
"Saudi Arabia", "Tonga",
"Thailand", "Armenia"
),
deaths_per_100k_pop = c(
193.86, 126.03,
102.19, 98.83,
97.67, 61.02
)
) %>%
mutate(
country_code = countrycode(
country,
"country.name",
"iso2c"
)
)
# A tibble: 6 x 3
country deaths_per_100k_pop country_code
<chr> <dbl> <chr>
1 Dominican Republic 194. DO
2 Zimbabwe 126. ZW
3 Saudi Arabia 102. SA
4 Tonga 98.8 TO
5 Thailand 97.7 TH
6 Armenia 61.0 AM
31 / 37
tibble(
country = c(
"Dominican Republic", "Zimbabwe",
"Saudi Arabia", "Tonga",
"Thailand", "Armenia"
),
deaths_per_100k_pop = c(
193.86, 126.03,
102.19, 98.83,
97.67, 61.02
)
) %>%
mutate(
country_code = countrycode(
country,
"country.name",
"iso2c"
)
) ->
ggimage_df
31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
)

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point()

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
)

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
)

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
) +
coord_flip()

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
) +
coord_flip() +
expand_limits(y = -8)

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
) +
coord_flip() +
expand_limits(y = -8) +
theme_minimal()

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
) +
coord_flip() +
expand_limits(y = -8) +
theme_minimal() +
theme(axis.title = element_blank())

31 / 37
ggplot(
ggimage_df,
aes(
x = fct_reorder(
country, deaths_per_100k_pop
),
y = deaths_per_100k_pop
)
) +
geom_point() +
geom_segment(
aes(
y = 0, yend = deaths_per_100k_pop,
x = country, xend = country
)
) +
geom_flag(
y = -8, aes(image = country_code)
) +
coord_flip() +
expand_limits(y = -8) +
theme_minimal() +
theme(axis.title = element_blank()) ->
ggimage_part1
31 / 37
ggimage_part1

31 / 37
ggimage_part1 +
labs(
title = paste(
"Countries with the highest road",
"traffic death rate (per 100k",
"population) \nwithin each",
"continent"
)
)

31 / 37
ggimage_part1 +
labs(
title = paste(
"Countries with the highest road",
"traffic death rate (per 100k",
"population) \nwithin each",
"continent"
)
) +
labs(caption = "Data source: WHO")

31 / 37

Check ggimage tutorial to learn more

👉

32 / 37

ggthemes + ggdark

33 / 37

Example

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
theme_bw()

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
theme_solarized()

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
theme_fivethirtyeight()

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
dark_mode(theme_fivethirtyeight())

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
theme_economist()

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
dark_mode(theme_economist())

34 / 37
lol_chart +
labs(
title = paste(
"Traffic crash death rate",
"per midwest state, 2018"
)
) +
theme_economist_white()

34 / 37

Check ggthemes website to learn more

👉

35 / 37

Thank you!

36 / 37

Closing remarks...


happy+spring

(Available at [http://gph.is/2porEcP](http://gph.is/2porEcP), Mar 14, 2021)

A big thanks to...

- Yihui Xie for xaringan,

- Gina Reynolds for flipbookr,

- Sarah Romanes for slide layout,

- Garrick Aden-Buie for xaringanExtra,

- Developers of ggplot2 extensions, and

- Ganesh Krishnan and ISU Graphics Group for the talk invitation


👀 GitHub repo | License: CC BY-SA 4.0

37 / 37

Welcome!

2 / 37
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow