class: center, middle, inverse, title-slide # Data Visualization ## Splitting graphs into facets ### Andrew Irwin,
a.irwin@dal.ca
### Math & Stats, Dalhousie University ### 2021-01-27 (updated: 2021-01-09) --- class: middle # Splitting a plot into facets * Using one categorical variable * Using two categorical variables * Using quantitative variables --- class: middle, inverse # One categorical variable * `facet_wrap( ~ variable)` --- class: middle ```r mtcars %>% ggplot(aes(x =wt, y=mpg)) + geom_point() ``` ![](09-facets_files/figure-html/unnamed-chunk-1-1.png)<!-- --> --- class: middle ```r mtcars %>% ggplot(aes(x =wt, y=mpg)) + geom_point() + facet_wrap(~cyl) ``` ![](09-facets_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- class: middle ```r penguins %>% ggplot(aes(x=body_mass_g, y= flipper_length_mm, color = sex)) + geom_point() + facet_wrap( ~ species) ``` ![](09-facets_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- class:middle, inverse # Using two categorical variables * `facet_grid( y ~ x)` * `facet_wrap( ~ x + y)` --- class: middle ```r mtcars %>% ggplot(aes(x =wt, y=mpg)) + geom_point() + facet_grid(gear~cyl) ``` ![](09-facets_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- class: middle ```r mtcars %>% ggplot(aes(x =wt, y=mpg)) + geom_point() + facet_grid(gear~cyl, labeller = labeller(.cols = function(x) paste(x, " cylinders"), .rows = label_both)) ``` ![](09-facets_files/figure-html/unnamed-chunk-5-1.png)<!-- --> --- class: middle ```r penguins %>% ggplot(aes(x=body_mass_g, y= flipper_length_mm, color = sex)) + geom_point() + facet_wrap(~ species + island) ``` ![](09-facets_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- class: middle ```r gapminder %>% ggplot(aes(x=year, y = lifeExp, group = country)) + geom_line() + facet_grid( continent ~ . ) ``` ![](09-facets_files/figure-html/unnamed-chunk-7-1.png)<!-- --> --- class: middle ```r gapminder %>% ggplot(aes(x=year, y = lifeExp)) + geom_line(aes(group = country), color = "gray40", size=0.25) + geom_smooth(method = "lm", color = "blue", se=FALSE) + facet_grid( continent ~ . ) ``` ![](09-facets_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- class: middle, inverse # Splitting on a quantitative variable * Start by converting the quantitative variable into a cateogorical one For example: ```r penguins %>% mutate(bill_categories = cut_number(bill_length_mm, n = 4)) %>% select(species, bill_length_mm, bill_categories, everything()) ``` ``` ## # A tibble: 344 x 9 ## species bill_length_mm bill_categories island bill_depth_mm flipper_length_… ## <fct> <dbl> <fct> <fct> <dbl> <int> ## 1 Adelie 39.1 [32.1,39.2] Torge… 18.7 181 ## 2 Adelie 39.5 (39.2,44.5] Torge… 17.4 186 ## 3 Adelie 40.3 (39.2,44.5] Torge… 18 195 ## 4 Adelie NA <NA> Torge… NA NA ## 5 Adelie 36.7 [32.1,39.2] Torge… 19.3 193 ## 6 Adelie 39.3 (39.2,44.5] Torge… 20.6 190 ## 7 Adelie 38.9 [32.1,39.2] Torge… 17.8 181 ## 8 Adelie 39.2 [32.1,39.2] Torge… 19.6 195 ## 9 Adelie 34.1 [32.1,39.2] Torge… 18.1 193 ## 10 Adelie 42 (39.2,44.5] Torge… 20.2 190 ## # … with 334 more rows, and 3 more variables: body_mass_g <int>, sex <fct>, ## # year <int> ``` --- class: middle ```r penguins %>% mutate(bill_categories = cut_number(bill_length_mm, n = 3)) %>% ggplot(aes(x= body_mass_g, y = flipper_length_mm, color = species)) + geom_point() + facet_wrap( ~ bill_categories) ``` ![](09-facets_files/figure-html/unnamed-chunk-10-1.png)<!-- --> --- class: middle # Summary * `facet_wrap()`, `facet_grid()` * You can set the number of rows and columns with `nrow = Y` or `ncol=X` * You can label facets with both the variable and value using `labeller = labeller(.rows = label_both)` --- class: middle # Further reading * More examples with different data in the course notes * R4DS [Section 3.5](https://r4ds.had.co.nz/data-visualisation.html#facets) * Wilke [Multi-panel figures](https://clauswilke.com/dataviz/multi-panel-figures.html) discussing facets and other suggestions for combining different views of data together into one figure * Healy [Section 4.3](https://socviz.co/groupfacettx.html#facet-to-make-small-multiples) --- class: middle, inverse # Task Practice these skills by making the plots in Task 6.