ggplot2Today we will…
ggplot2Tip
Name arguments in function calls
Only include necessary arguments! (If you are using any default values, no need to repeat them in your function call.)
Good
The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.1

The tidyverse includes functions to:
| Read in data | readr |
| Visualize data | ggplot2 |
| Manipulate rectangular data | tidyr, dplyr, tibble |
| Handle special variable types | stringr, forcats , lubridate |
| Support functional programming | purrr |
This version of the course will primarily use tidyverse packages and grammar
Reasoning:
R at this point (in my opinion)tidyverse packageInstalling/loading the tidyverse package installs/loads all of the “tidyverse” packages
Avoid redundantly installing or loading packages!
ggplot2The Grammar of Graphics (GoG) is a principled way of specifying exactly how to create a particular graph from a given data set. It helps us to systematically design new graphs.
Think of a graph or a data visualization as a mapping…
FROM variables in the data set (or statistics computed from the data)…
TO visual attributes (or “aesthetics”) of marks (or “geometric elements”) on the page/screen.
data: dataframe containing variablesaes : aesthetic mappings (position, color, symbol, …)geom : geometric element (point, line, bar, box, …)stat : statistical variable transformation (identity, count, linear model, quantile, …)scale : scale transformation (log scale, color mapping, axes tick breaks, …)coord : Cartesian, polar, map projection, …facet : divide into subplots using a categorical variableWe map variables (columns) from the data to aesthetics on the graphic useing the aes() function.
We use a geom_xxx() function to represent data points.
one variable
geom_bar()geom_density()geom_histogram()geom_boxplot()two variable
geom_boxplot()geom_point()geom_line()Tip
See ggplot2 cheat sheet for more!
To create a specific type of graphic, we will combine aesthetics and geometric objects.
Complete this template to build a basic graphic:
+ to adds another layer to a graphic.Global Aesthetics
Mapping Aesthetics
Extracts subsets of data and places them in side-by-side plots.
Extracts subsets of data and places them in side-by-side plots.
facet_grid(cols = vars(b)): facet into columns based on bfacet_grid(rows = vars(a)): facet into rows based on afacet_grid(rows = vars(a), cols = vars(b)): facet into both rows and columnsfacet_wrap(vars(b)): wrap facets into a rectangular layoutYou can set scales to let axis limits vary across facets:
"fixed" – default, x- and y-axis limits are the same for each facet"free" – both x- and y-axis limits adjust to individual facets"free_x" – only x-axis limits adjust"free_y" – only y-axis limits adjustYou can set a labeller to adjust facet labels.
Include both the variable name and factor name in the labels:
facet_grid(cols = vars(b), labeller = label_both)Position adjustments determine how to arrange geom’s that would otherwise occupy the same space.
position = 'dodge': Arrange elements side by side.position = 'fill': Stack elements on top of one another + normalize height.position = 'stack': Stack elements on top of one another.position = 'jitter": Add random noise to X & Y position of each element to avoid overplotting (see geom_jitter()). It is good practice to put each geom and aes on a new line.
Some general resources:
Making your exact graphic:
Artwork by Allison Horst
During your collaboration, your group will alternate between three roles:
Starting Roles Today
The person whose family name starts first alphabetically starts as the project manager, second as the computer.