ggplot()
IntroJanuary 14, 2025
ggplot()
{patchwork}
Single Numerical Variable
Single Categorical Variable
Two Numerical Variables
Two Categorical Variables
One Numerical and One Categorical Variable
ggplot()
Note we can pipe (%>%
) a data frame into a plot
Once we use ggplot()
we use +
to add layers instead of piping
geom_*()
layers require aesthetics to map variables to plot features
Can add multiple geoms to a single plot
Every plot should include labels
Open RStudio
Check the top-right corner, next to the translucent blue box icon to verify that you are working in your MAT434
project space that is managing your GitHub Repo.
None
there instead of your project name, open your project by navigating to File -> Open Project
or by using the dropdown menu near the project boxOpen your Quarto Notebook on the MLB batted balls and home runs from last time
Add a section on data visualizations
Single Numerical Variable
geom_boxplot()
, geom_histogram()
, or geom_density()
x
or y
aesthetic (but not both!)geom_density(aes(x = hwy))
Single Categorical Variable
geom_bar()
x
or y
aesthetic (but not both!)geom_bar(aes(x = class))
geom_col()
x
and y
aestheticgeom_col(aes(x = class, y = n))
Two Numerical Variables
geom_point()
or geom_hexbin()
x
and y
aestheticgeom_point(aes(x = cty, y = hwy))
Two Categorical Variables
geom_bar()
x
and fill
aestheticsgeom_bar(aes(x = class, fill = drv))
One Numerical and One Categorical Variable
geom_boxplot()
x
and y
aestheticsgeom_boxplot(aes(x = hwy, y = class))
geom_density()
or geom_histogram()
x
aestheticfacet_wrap(~ VAR_NAME)
Other available aesthetics include color
, size
, shape
, and alpha
(transparency)
Start with a few single-variable plots
is_home_run
)pitch_mph
launch_speed
launch_angle
Ask ChatGPT (or your favorite LLM) to help you troubleshoot your plots or to “trick out” at least one
Now build some multivariable plots
Again, ask your favorite LLM for help troubleshooting code or “tricking out” your plots.
The labs()
layer permits global plot labels and labels for any mapped aesthetic
title
subtitle
caption
alt
(for alt-text)x
y
color
fill
{patchwork}
{patchwork}
package provides very easy and intuitive framework for doing this.Create each of your plots, but store them into variables p1
, p2
, …
Use +
to organize plots side-by-side, and /
to organize plots over/under one another.
(p1 + p2) / p3
will arrange plots p1
and p2
side-by-side, with plot p3
underneath them.{patchwork}
#how-do-i
channel on Slack