Notes on RFDS 2

Load Packages

## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats


Bill Cleveland called these graphics trellises and implemented them in the original S language in the package Trellis.

Trellis was essentially re-created in R under the name lattice by Deepayan Sarkar. See See figures 2.8 - 2.11 on Titanic survivors.

Trellis and Lattice are strongly focused on this type of graphic and have declined in popularity since the grammar of graphics aproach ggplot2.

Tufte refers to this type of graphic as ‘small multiples.’


To produce separate graphs for each value of a single categorical variable use facet_wrap() as a layer. The syntax requires a single-variable formula in a formula (preceded by a ~).


d = ggplot(data=mpg, aes(x=cty,y=hwy)) + geom_point()

d + facet_wrap(~class)

The layout can be controlled with the arguments ncol or nrow.

d + facet_wrap(~class,nrow=2) + ggtitle("nrow = 2")

d + facet_wrap(~class,ncol=2) + ggtitle("ncol=2")


Create two graphics showing the relationship between displ and hwy broken down by the categorical variable drv in the dataframe mpg. In one version, use a single row arrangement. In the other, use a single column. Which do you prefer.


e = ggplot(data=mpg,aes(x=displ,y=hwy)) + geom_point()

e + facet_wrap(~drv,nrow=1) + ggtitle("nrow=1")

e + facet_wrap(~drv,ncol=1) + ggtitle("ncol=1")


To produce separate graphs for each combination of values of two categorical variables use facet_grid() as a layer. The syntax requires a two-variable formula (two variables separated by a ~). The first variable designates the rows and the second designates the columns.


f = ggplot(mpg,aes(x=cty,y=hwy)) + geom_point()