Week 2
ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA ALWAYS PLOT YOUR DATA
.
ggplot2
package (which comes with tidyverse
) includes:
mpg
?mpg
in the RStudio console to get a help page on the mpg
datasetmpg
to see the first few rows# 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…
# ℹ 224 more rows
ggplot2
package (which comes with tidyverse
) includes:
mpg
nrow(mpg)
to count the number of rowsncol(mpg)
to count the number of colsglimpse()
:Rows: 234
Columns: 11
$ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
$ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
$ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
$ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
$ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
$ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
$ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
$ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
$ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
$ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
$ class <chr> "compact", "compact", "compact", "compact", "compact", "c…
View(mpg)
to bring up a spreadsheet-like view of the data framedispl
: engine size, in litreshwy
: fuel efficiency (highway), in miles per gallon (mpg)ggplot
ggplot
ggplot
ggplot(data = mpg)
geom_point()
mapping = aes(x = displ, y = hwy)
geom_point
to map displ
values on to X-axis and hwy
values onto Y-axisgeom_point()
geom_smooth()
ggplot()
geom_smooth()
optionsgeom_smooth()
optionsggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>
ggplot2
will provide useful defaults for everything except the data, the mappings, and the geom function
ggplot
—play around!ggplot
is simple to get started with
ggplot
—play around!ggplot
—Homework 2