What is the relationship between a car's
engine weight (displacement) and its mileage (miles per gallon)?
Run this code in your Quarto file to make a graph
Pay attention to spelling, capitalization, and parentheses!
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
02:00
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
Source: Wikipedia
Source: Wikimedia Commons
Aesthetic
Visual property of a graph
Position, shape, color, etc.
Data
A column in a dataset
Data | Aesthetic | Graphic/Geometry |
---|---|---|
Longitude | Position (x-axis) | Point |
Latitude | Position (y-axis) | Point |
Army size | Size | Path |
Army direction | Color | Path |
Date | Position (x-axis) | Line + text |
Temperature | Position (y-axis) | Line + text |
Data | aes() |
geom |
---|---|---|
Longitude | x |
geom_point() |
Latitude | y |
geom_point() |
Army size | size |
geom_path() |
Army direction | color |
geom_path() |
Date | x |
geom_line() + geom_text() |
Temperature | y |
geom_line() + geom_text() |
ggplot()
templateggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
ggplot()
templateggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
ggplot(data = troops) +
geom_path(mapping = aes(x = longitude,
y = latitude,
color = direction,
size = survivors))
This is a dataset named troops
:
longitude | latitude | direction | survivors |
---|---|---|---|
24 | 54.9 | A | 340000 |
24.5 | 55 | A | 340000 |
… | … | … | … |
This is a dataset named troops
:
longitude | latitude | direction | survivors |
---|---|---|---|
24 | 54.9 | A | 340000 |
24.5 | 55 | A | 340000 |
… | … | … | … |
ggplot(data = troops) +
geom_path(mapping = aes(x = longitude,
y = latitude,
color = direction,
size = survivors))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
color
(discrete)
color
(continuous)
size
fill
shape
alpha
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, size = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, shape = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, alpha = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
Add color, size, alpha, and shape aesthetics to your graph.
Experiment!
Do different things happen when you map aesthetics to discrete and continuous variables?
What happens when you use more than one aesthetic?
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class))
ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue")
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = "blue"))
ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue")
ggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
Example geom | What it makes | |
---|---|---|
![]() |
geom_col() |
Bar charts |
![]() |
geom_text() |
Text |
![]() |
geom_point() |
Points |
![]() |
geom_boxplot() |
Boxplots |
![]() |
geom_sf() |
Maps |
There are dozens of possible geoms!
See the ggplot2 documentation for
complete examples of all the different geom layers
Also see the ggplot cheatsheet
Replace this scatterplot with boxplots. Use the cheatsheet.
03:00
Make a histogram of hwy
. Use the cheetsheet.
Hint: don't supply a y
variable.
Make this density plot of hwy
colored by class
.
Use the cheatsheet. Hint: don't supply a y
variable.
Predict what this code will do. Then run it.
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))
02:00
Any aesthetics in ggplot()
will show up in all geom_
layers
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth()
Any aesthetics in geom_
layers only apply to that layer
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = drv)) + geom_smooth()
There are many other layers we can use to make and enhance graphs!
We sequentially add layers onto the foundational ggplot()
plot to create complex figures
We can build a plot sequentially
to see how each grammatical layer
changes the appearance
Start with data and aesthetics
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv))
Add a point geom
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point()
Add a smooth geom
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth()
Make it straight
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm")
Use a viridis color scale
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d()
Facet by drive
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1)
Add labels
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars")
Add a theme
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw()
Modify the theme
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold"))
Finished!
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold"))
Transforming and
manipulating data with dplyr
What is the relationship between a car's
engine weight (displacement) and its mileage (miles per gallon)?
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 |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
What is the relationship between a car's
engine weight (displacement) and its mileage (miles per gallon)?
Run this code in your Quarto file to make a graph
Pay attention to spelling, capitalization, and parentheses!
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
02:00
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
Source: Wikipedia
Source: Wikimedia Commons
Aesthetic
Visual property of a graph
Position, shape, color, etc.
Data
A column in a dataset
Data | Aesthetic | Graphic/Geometry |
---|---|---|
Longitude | Position (x-axis) | Point |
Latitude | Position (y-axis) | Point |
Army size | Size | Path |
Army direction | Color | Path |
Date | Position (x-axis) | Line + text |
Temperature | Position (y-axis) | Line + text |
Data | aes() |
geom |
---|---|---|
Longitude | x |
geom_point() |
Latitude | y |
geom_point() |
Army size | size |
geom_path() |
Army direction | color |
geom_path() |
Date | x |
geom_line() + geom_text() |
Temperature | y |
geom_line() + geom_text() |
ggplot()
templateggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
ggplot()
templateggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
ggplot(data = troops) +
geom_path(mapping = aes(x = longitude,
y = latitude,
color = direction,
size = survivors))
This is a dataset named troops
:
longitude | latitude | direction | survivors |
---|---|---|---|
24 | 54.9 | A | 340000 |
24.5 | 55 | A | 340000 |
… | … | … | … |
This is a dataset named troops
:
longitude | latitude | direction | survivors |
---|---|---|---|
24 | 54.9 | A | 340000 |
24.5 | 55 | A | 340000 |
… | … | … | … |
ggplot(data = troops) +
geom_path(mapping = aes(x = longitude,
y = latitude,
color = direction,
size = survivors))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
color
(discrete)
color
(continuous)
size
fill
shape
alpha
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, size = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, shape = class))ggplot(mpg) + geom_point(aes(x = displ, y = hwy, alpha = class))
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
Add color, size, alpha, and shape aesthetics to your graph.
Experiment!
Do different things happen when you map aesthetics to discrete and continuous variables?
What happens when you use more than one aesthetic?
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class))
ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue")
ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = "blue"))
ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue")
ggplot(data = DATA) +
GEOM_FUNCTION(mapping = aes(AESTHETIC MAPPINGS))
Example geom | What it makes | |
---|---|---|
![]() |
geom_col() |
Bar charts |
![]() |
geom_text() |
Text |
![]() |
geom_point() |
Points |
![]() |
geom_boxplot() |
Boxplots |
![]() |
geom_sf() |
Maps |
There are dozens of possible geoms!
See the ggplot2 documentation for
complete examples of all the different geom layers
Also see the ggplot cheatsheet
Replace this scatterplot with boxplots. Use the cheatsheet.
03:00
Make a histogram of hwy
. Use the cheetsheet.
Hint: don't supply a y
variable.
Make this density plot of hwy
colored by class
.
Use the cheatsheet. Hint: don't supply a y
variable.
Predict what this code will do. Then run it.
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy))
02:00
Any aesthetics in ggplot()
will show up in all geom_
layers
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth()
Any aesthetics in geom_
layers only apply to that layer
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = drv)) + geom_smooth()
There are many other layers we can use to make and enhance graphs!
We sequentially add layers onto the foundational ggplot()
plot to create complex figures
We can build a plot sequentially
to see how each grammatical layer
changes the appearance
Start with data and aesthetics
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv))
Add a point geom
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point()
Add a smooth geom
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth()
Make it straight
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm")
Use a viridis color scale
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d()
Facet by drive
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1)
Add labels
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars")
Add a theme
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw()
Modify the theme
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold"))
Finished!
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold"))
Transforming and
manipulating data with dplyr