ggplot()
IntroSeptember 13, 2024
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 MAT300
project space
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 from last time
Add some questions that you think could be answered using a data visualization and describe the relevant viz type
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)
\(\bigstar\) Now that you know about data visualization types, build basic plots to answer at least two of the questions you wrote out earlier
03:00
\(\bigstar\) Now that you know about data visualization types, build basic plots to answer at least two of the questions you wrote out earlier
05:00
\(\bigstar\) Now that you know about data visualization types, build basic plots to answer at least two of the questions you wrote out earlier
05:00
The labs()
layer permits global plot labels and labels for any mapped aesthetic
title
subtitle
caption
alt
(for alt-text)x
y
color
fill
\(\bigstar\) Now that you know about labeling options in the labs()
layer, update your plots with meaningful labels
05:00
{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.
\(\bigstar\) Use {patchwork}
to experiment with different arrangements of your plots
05:00
Ask at least four more questions that can be answered using data visualization
Construct relevant visuals (including meaningful labels) to answer your questions
{patchwork}
if you like{patchwork}
is used versus when it is notProvide interpretations of the plots you are seeing
Reminder: You have a fully complete notebook using the penguins
data on the class webpage.
In that notebook, I split the available data into training and testing sets – we’ll talk about why later on.
A Workshop Day on Quarto and R