ggplot() statementgeom_[style]() such as
geom_point()geom_bar()geom_boxplot()geom_density()geom_vline()geom_segment()geom_histogram()geom_ash()aes( )+ symbolThis can all be a lot to keep straight… visit “Canvas >> Files >> Cheatsheets”" for some resources to help you shoulder the burden:
ggplot2 tools and usage from RStudiodplyr (& tidyr) tools and usage from RStudioAlso see…
facet_wrap()The syntax for facets can be a bit complicated for new users. Here are a few pointers:
facet_wrap() just makes a box for each level of the categorical variable
facet_wrap( ~ categoricalVariable)data("NCHS")
# 1is.na(smoker) gets cases that are non-missing for `smoker` (i.e. removes NA's)
Heights <-
NCHS %>%
filter(age > 20, !is.na(smoker)) %>%
group_by(sex, smoker, age) %>%
summarise(height = mean(height, na.rm = TRUE))
Heights %>%
ggplot(aes(x = age, y = height)) +
geom_line(aes(linetype = smoker)) +
facet_wrap( ~ sex)
facet_grid()facet_grid() allows control of row & column facetsfacet_grid() syntax:
facet_grid(rows ~ cols)facet_grid( rows ~ . ) (note the required “.”)facet_grid( ~ cols) (no “.” this time)Heights %>%
ggplot(aes(x = age, y = height)) +
geom_line(aes(linetype = smoker)) +
facet_grid(sex ~ .)
Heights %>%
ggplot(aes(x = age, y = height)) +
geom_line() +
facet_grid(sex ~ smoker)
Assignment is worth a total of 10 points.
[EXTRA CREDIT: 2 pts] complete both A.3 & A.5
teaching | stat 184 home | syllabus | piazza | canvas