Add a setup chunk that loads the tidyverse packages.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(readr)
library(tidyr)
library(tibble)
library(stringr)
mpg
## # A tibble: 234 × 11
## manufacturer model displ year cyl trans drv cty hwy fl class
## <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi a4 1.8 1999 4 auto… f 18 29 p comp…
## 2 audi a4 1.8 1999 4 manu… f 21 29 p comp…
## 3 audi a4 2 2008 4 manu… f 20 31 p comp…
## 4 audi a4 2 2008 4 auto… f 21 30 p comp…
## 5 audi a4 2.8 1999 6 auto… f 16 26 p comp…
## 6 audi a4 2.8 1999 6 manu… f 18 26 p comp…
## 7 audi a4 3.1 2008 6 auto… f 18 27 p comp…
## 8 audi a4 quattro 1.8 1999 4 manu… 4 18 26 p comp…
## 9 audi a4 quattro 1.8 1999 4 auto… 4 16 25 p comp…
## 10 audi a4 quattro 2 2008 4 manu… 4 20 28 p comp…
## # … with 224 more rows
Run the code on the slide to make a graph. Pay strict attention to spelling, capitalization, and parentheses!
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
Add color
, size
, alpha
, and shape
aesthetics to your graph. Experiment.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "coral", size=3, alpha=.5, shape=18)
What do facet_grid()
and facet_wrap()
do? (run the code, interpret, convince your group)
# Makes a plot that the commands below will modify
q <- ggplot(mpg) + geom_point(aes(x = displ, y = hwy))
q + facet_grid(. ~ cyl)
q + facet_grid(drv ~ .)
q + facet_grid(drv ~ cyl)
q + facet_wrap(~ class)
Add the black code to your graph. What does it do?
ggplot(data = mpg) +
geom_point(mapping = aes(displ, hwy, color = class))
#Displays different classes as different colors
Replace this scatterplot with one that draws boxplots. Use the cheatsheet. Try your best guess.
ggplot(data = mpg) +
geom_boxplot(mapping = aes(x = class, y = hwy))
Make a histogram of the hwy
variable from mpg
. Hint: do not supply a y variable.
ggplot(data = mpg) +
geom_histogram(mapping = aes(x = hwy))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Use the help page for geom_histogram
to make the bins 2 units wide.
ggplot(data = mpg) +
geom_histogram(mapping = aes(x = hwy), binwidth = 2)
Make a bar chart class
colored by class
. Use the help page for geom_bar
to choose a “color” aesthetic for class.
ggplot(data = mpg) +
geom_bar(mapping = aes(x = class, fill = drv))
What will this code do?
ggplot(mpg) +
geom_point(aes(displ, hwy)) +
geom_smooth(aes(displ, hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#Adds a smooth conditional means line
What is different about this plot? Run the code!
p <- ggplot(mpg) +
geom_point(aes(displ, hwy)) +
geom_smooth(aes(displ, hwy))
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#Uses a different function ggplotly instead of ggplot.
You can use this code template to make thousands of graphs with ggplot2.
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))