wildlife_impacts <- read_csv(file.path('data', 'wildlife_impacts.csv'))
federal_spending <- read_csv(file.path('data', 'federal_spending_long.csv'))
milk_production <- read_csv(file.path('data', 'milk_production.csv'))
lotr_words <- read_csv(file.path('data', 'lotr_words.csv'))
msleep <- read_csv(file.path('data', 'msleep.csv'))





Good scaling

Y scale too large

X scale too large


Approx. to golden ratio:
| width | height |
|---|---|
| 5 | 3.1 or 3 |
| 6 | 3.7 or 4 |
| 7 | 4.3 |
scale_* functionsContinous variables
Discrete variables
Others
milk_bars <- milk_production %>%
filter(year == 2017) %>%
arrange(desc(milk_produced)) %>%
slice(1:10) %>%
mutate(
milk_produced = milk_produced / 10^9,
state = fct_reorder(state, milk_produced)
) %>%
ggplot() +
geom_col(aes(x = milk_produced, y = state)) +
theme_minimal_vgrid(font_size = 18) +
labs(x = 'Milk produced (billions of lbs)', y = 'State')
milk_bars




Summarise the data
library(lubridate)
plot <- wildlife_impacts %>%
filter(incident_year == 2016) %>%
count(operator, incident_date) %>%
mutate(incident_date = ymd(incident_date)) %>%
ggplot() +
geom_col(
aes(x = incident_date, y = n, color = operator)
) +
facet_wrap(~operator, ncol = 1) +
theme_minimal_grid(font_size = 16) +
panel_border() +
theme(legend.position = 'none') +
labs(x = 'Incident date (2016)', y = 'Number of incidents')
plot
scales converts numbers to formatted charactersfederal_spending %>%
filter(year == 2017) %>%
mutate(
department = fct_reorder(
department,
rd_budget
)
) %>%
ggplot() +
geom_col(aes(x = rd_budget, y = department)) +
scale_x_continuous(
expand = expand_scale(mult = c(0, 0.05))
) +
theme_minimal_vgrid(font_size = 16) +
labs(x = 'Department', y = 'R&D spending ($ Millions)')
federal_spending %>%
filter(year == 2017) %>%
mutate(
department = fct_reorder(
department,
rd_budget
)
) %>%
ggplot() +
geom_col(aes(x = rd_budget, y = department)) +
scale_x_continuous(
labels = scales::comma,
expand = expand_scale(mult = c(0, 0.05))
) +
theme_minimal_vgrid(font_size = 16) +
labs(x = 'Department', y = 'R&D spending ($ Millions)')
federal_spending %>%
filter(year == 2017) %>%
mutate(
department = fct_reorder(
department,
rd_budget
)
) %>%
ggplot() +
geom_col(aes(x = rd_budget, y = department)) +
scale_x_continuous(
labels = scales::dollar,
expand = expand_scale(mult = c(0, 0.05))
) +
theme_minimal_vgrid(font_size = 16) +
labs(x = 'Department', y = 'R&D spending ($ Millions)')
Adjust the scales in the code chunk provided to match the chart on the slides.

![]()
Michelle Borkin, et al. (2015) Beyond Memorability: Visualization Recognition and Recall

Source: https://ourworldindata.org/less-meat-or-sustainable-meat
Use mapped variables in aes() in labs()
milk_production %>%
filter(year %in% c(1970, 2017)) %>%
group_by(year, region) %>%
summarise(milk_produced = sum(milk_produced) / 10^9) %>%
ungroup() %>%
mutate(
region = fct_reorder2(region, year, desc(milk_produced))
) %>%
ggplot() +
geom_col(
aes(x = milk_produced, y = region, fill = as.factor(year)),
position = "dodge"
) +
scale_x_continuous(expand = expansion(mult = c(0, 0.05))) +
theme_minimal_vgrid() +
labs(
x = 'Milk produced (billions lbs)',
y = 'Region',
title = 'Milk production by region',
subtitle = '1970 & 2017'
)
Use mapped variables in aes() in labs()
milk_production %>%
filter(year %in% c(1970, 2017)) %>%
group_by(year, region) %>%
summarise(milk_produced = sum(milk_produced) / 10^9) %>%
ungroup() %>%
mutate(
region = fct_reorder2(region, year, desc(milk_produced))
) %>%
ggplot() +
geom_col(
aes(x = milk_produced, y = region, fill = as.factor(year)),
position = "dodge"
) +
scale_x_continuous(expand = expansion(mult = c(0, 0.05))) +
theme_minimal_vgrid() +
labs(
x = 'Milk produced (billions lbs)',
y = 'Region',
title = 'Milk production by region',
subtitle = '1970 & 2017',
fill = "Year"
)




annotate() to add text to chartdod_spending <- ggplot(federal_spending_summary) +
geom_area(aes(x = year, y = rd_budget, fill = department)) +
annotate(
geom = 'text',
x = 1995,
y = 85,
label = 'Other',
size = 6,
color = 'black'
) +
annotate(
geom = 'text',
x = 1995,
y = 25,
label = 'DOD',
size = 6,
color = 'white'
) +
scale_y_continuous(
expand = expand_scale(mult = c(0, 0.05))
) +
scale_fill_manual(values = c('grey', 'sienna')) +
theme_minimal_hgrid() +
theme(legend.position = 'none') +
labs(
x = NULL,
y = 'R&D Budget ($ Billions)',
title = 'Federal R&D spending, 1976 - 2017',
subtitle = 'Source: AAAS Historical Trends'
)
dod_spending
geom_text() to add text to chartdod_spending <- ggplot(federal_spending_summary) +
geom_area(aes(x = year, y = rd_budget, fill = department)) +
geom_text(
data = data.frame(x = 1995, y = 85, label = 'Other'),
aes(x = x, y = y, label = label),
size = 6,
color = 'black'
) +
geom_text(
data = data.frame(x = 1995, y = 25, label = 'DOD'),
aes(x = x, y = y, label = label),
size = 6,
color = 'white'
) +
scale_y_continuous(
expand = expand_scale(mult = c(0, 0.05))
) +
scale_fill_manual(values = c('grey', 'sienna')) +
theme_minimal_hgrid() +
theme(legend.position = 'none') +
labs(
x = NULL,
y = 'R&D Budget ($ Billions)',
title = 'Federal R&D spending, 1976 - 2017',
subtitle = 'Source: AAAS Historical Trends'
)
dod_spending
geom_label() to add text to chart with a backgrounddod_spending <- ggplot(federal_spending_summary) +
geom_area(aes(x = year, y = rd_budget, fill = department)) +
geom_label(
data = data.frame(x = 1995, y = 85, label = 'Other'),
aes(x = x, y = y, label = label),
size = 6
) +
geom_label(
data = data.frame(x = 1995, y = 25, label = 'DOD'),
aes(x = x, y = y, label = label),
size = 6,
fill = "black",
color = "white"
) +
scale_y_continuous(
expand = expand_scale(mult = c(0, 0.05))
) +
scale_fill_manual(values = c('grey', 'sienna')) +
theme_minimal_hgrid() +
theme(legend.position = 'none') +
labs(
x = NULL,
y = 'R&D Budget ($ Billions)',
title = 'Federal R&D spending, 1976 - 2017',
subtitle = 'Source: AAAS Historical Trends'
)
dod_spending
geom_curve() + geom_label() to direct attentionlabel <- "The Dept. of Defense R&D Budget
has made up nearly half of all
federal R&D spending"
dod_spending +
geom_curve(
data = data.frame(
x = 1981,
xend = 1987,
y = 160,
yend = 25
),
mapping = aes(x = x, xend = xend, y = y, yend = yend),
color = 'grey75',
size = 0.5,
curvature = 0.1,
arrow = arrow(length = unit(0.01, "npc"), type = "closed")
) +
geom_label(
data = data.frame(x = 1977, y = 160, label = label),
mapping = aes(x = x, y = y, label = label),
hjust = 0,
lineheight = 0.8
)
annotate() to direct attentionUse geom = "rect" for box, geom = "text" for label
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(
aes(fill = as.factor(cyl)),
color = 'white',
alpha = 0.8,
size = 3.5,
shape = 21
) +
annotate(
geom = "rect",
xmin = 5,
xmax = 7.5,
ymin = 21,
ymax = 28,
fill = "grey55",
alpha = 0.2
) +
annotate(
geom = "text",
x = 5,
y = 29,
label = "Hybrid vehicles",
hjust = 0,
size = 5
) +
theme_half_open(font_size = 15) +
labs(
x = "Engine displacement",
y = "Fuel efficiency (mpg)",
fill = '# cylinders',
title = "Vehicle fuel efficiency vs. engine displacement",
caption = "Source: U.S. EPA."
)
ggannotateUse the lotr_summary data frame to create the following chart.
Hints:
geom_label(), use these points:
x = 0.6y = 2100geom_curve(), use these points:
x = 1.2xend = 1y = 1300yend = 200

Image from this color wheel tool

Image from this color wheel tool

Image from this color wheel tool

Image from this color wheel tool

Map color to variable
mpg_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(
aes(color = as.factor(cyl)),
alpha = 0.8,
size = 3
) +
theme_half_open(font_size = 16) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
color = '# cylinders',
title = "Vehicle fuel efficiency vs. power",
caption = "Source: 1974 Motor Trend U.S. magazine."
)
Map color to variable
mpg_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(
aes(color = as.factor(cyl)),
alpha = 0.8,
size = 3
) +
theme_half_open(font_size = 16) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
color = '# cylinders',
title = "Vehicle fuel efficiency vs. power",
caption = "Source: 1974 Motor Trend U.S. magazine."
)Manually change colors



Image from betterfigures.org

Image from betterfigures.org

Image from betterfigures.org
Map color to variable
mpg_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = as.factor(cyl)), alpha = 0.8, size = 3) +
theme_half_open(font_size = 16) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
color = '# cylinders',
title = "Vehicle fuel efficiency vs. power",
caption = "Source: 1974 Motor Trend U.S. magazine."
)Use “Dark2” palette

Map color to variable
mpg_plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = as.factor(cyl)), alpha = 0.8, size = 3) +
theme_half_open(font_size = 16) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
color = '# cylinders',
title = "Vehicle fuel efficiency vs. power",
caption = "Source: 1974 Motor Trend U.S. magazine."
)Use viridis colors

Fun custom palettes
color + fill for pointsggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(
aes(fill = as.factor(cyl)),
color = 'white',
shape = 21,
size = 3.5,
alpha = 0.8
) +
scale_fill_brewer(palette = 'Dark2') +
theme_half_open(font_size = 15) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
fill = '# cylinders',
title = "Vehicle fuel efficiency vs. power",
caption = "Source: 1974 Motor Trend U.S. magazine."
)
grey = “Other”dod_spending_plot <- federal_spending %>%
mutate(
department = fct_other(
department,
keep = 'DOD'
)
) %>%
group_by(department, year) %>%
summarise(rd_budget = sum(rd_budget) / 10^3) %>%
ungroup() %>%
mutate(
department = fct_relevel(
department,
c('Other', 'DOD')
)
) %>%
ggplot() +
geom_area(aes(x = year, y = rd_budget, fill = department)) +
scale_y_continuous(
expand = expand_scale(mult = c(0, 0.05))
) +
scale_fill_manual(
values = c('grey', 'sienna')
) +
theme_minimal_hgrid() +
labs(
x = NULL,
y = 'R&D Budget ($ Billions)',
fill = 'Department',
caption = 'Source: AAAS'
)
dod_spending_plot
grey = “Other”Make 3 different versions of this chart:
Change the colors to the "RdYlBu" ColorBrewer palette.
Change the colors to the "inferno" palette from the viridis library.
Change the colors to your custom triadic palette:


“Fast Taco”

“Mega Flicks”

Roboto
Source
Fira
Alegreya
Lato
Download:
# Hierarchy
## Hierarchy
### Hierarchy
#### Hierarchy
Title
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Weight
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Color
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Spacing
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Typeface
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
This is some text that goes into detail and explains a lot more about the topic described in the title. Here’s some random Latin words: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.


For “Base R” themes, use base_family:
For “cowplot” themes, use font_family:
Make the base plot
Use base_family with base themes
mpg_plot <- ggplot(mtcars) +
geom_point(aes(x = mpg, y = hp)) +
theme_minimal(
base_family = 'Source Sans Pro',
base_size = 15
) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
title = "Vehicle fuel efficiency vs. power",
subtitle = "Select makes and models",
caption = "Source: 1974 Motor Trend U.S. magazine."
)
Use font_family with cowplot themes
mpg_plot <- ggplot(mtcars) +
geom_point(aes(x = mpg, y = hp)) +
theme_minimal_grid(
font_family = 'Source Sans Pro',
font_size = 15
) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
title = "Vehicle fuel efficiency vs. power",
subtitle = "Select makes and models",
caption = "Source: 1974 Motor Trend U.S. magazine."
)
element_text()mpg_plot <- ggplot(mtcars) +
geom_point(aes(x = mpg, y = hp)) +
theme_minimal_grid(
font_family = 'Source Sans Pro',
font_size = 15
) +
theme(
plot.title = element_text(
family = "Roboto Condensed",
size = 20
)
) +
labs(
x = "Fuel efficiency (mpg)",
y = "Power (hp)",
title = "Vehicle fuel efficiency vs. power",
subtitle = "Select makes and models",
caption = "Source: 1974 Motor Trend U.S. magazine."
)See theme components here

geom_text(), geom_label(), and annotate()Modify the fonts and annotations in the dumbbell chart to match the chart shown here. The main font is 'Roboto Condensed'.
Once you’ve recreated the plot, try other fonts and themes, such as:
'Source Sans Pro' font.'Lato' font.theme_ipsum() theme from the hrbrthemes library.Hint: Use annotate() to insert the year labels at the top:
x = 9, y = 10.5x = 40, y = 10.5
https://quarto.org/docs/extensions/
Some personal favoriates: