class: center, middle, inverse, title-slide # Facet ### Gina Reynolds --- --- name: facets # Punctuation: Small multiples Punctuation helps us break up language into digestible pieces. Breaking up visualization can also be helpful. Creating *small multiples* also known as *faceting* is a powerful way to make data more digestible; we can process the encoding in just one of the plots easily translate those encodings to the remaining plots. In ggplot, small plots based on a categorical variable can be visualized side-by-side. --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + NULL ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent)) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), ncol = 2) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), nrow = 1) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), scales = "free_x") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), scales = "free_x") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), scales = "free") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), strip.position = "top") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), strip.position = "bottom") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), strip.position = "left") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent), strip.position = "right") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(continent ~ pop > 10000000) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(continent ~ pop > 10000000, switch = "x") # y also possible ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_13_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(continent ~ pop > 10000000, switch = "both") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_14_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(. ~ continent) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_15_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * ggforce::facet_col(facets = vars(continent), scales = "free_y", space = "free", strip.position = "top") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_16_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * ggforce::facet_col(facets = vars(continent), scales = "free_y", space = "free", strip.position = "left") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_17_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(facets = continent ~ .) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_18_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(rows = vars(continent)) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_19_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(cols = vars(continent)) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_20_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_grid(vars(continent), vars(pop > 10000000)) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_21_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_wrap(vars(continent, pop > 10000000), scales = "free_y") ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_22_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * ggforce::facet_zoom(xlim = c(5,10), horizontal = FALSE) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_23_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * ggforce::facet_zoom(xlim = c(20, 25), ylim = c(65, 80), horizontal = FALSE) ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_24_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-facets-rotate[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point() + aes(color = continent) + * facet_null() # overrides any faceting ``` ] .right-panel-facets-rotate[ <img src="facet_files/figure-html/facets_rotate_25_output-1.png" width="100%" /> ] <style> .left-panel-facets-rotate { color: #777; width: 54%; height: 92%; float: left; font-size: 80% } .right-panel-facets-rotate { width: 45%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-facets-rotate { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-background-auto[ ```r *gapminder ``` ] .right-panel-background-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% * filter(year == 2002) ``` ] .right-panel-background-auto[ ``` # A tibble: 142 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 2002 42.1 25268405 727. 2 Albania Europe 2002 75.7 3508512 4604. 3 Algeria Africa 2002 71.0 31287142 5288. 4 Angola Africa 2002 41.0 10866106 2773. 5 Argentina Americas 2002 74.3 38331121 8798. 6 Australia Oceania 2002 80.4 19546792 30688. 7 Austria Europe 2002 79.0 8148312 32418. 8 Bahrain Asia 2002 74.8 656397 23404. 9 Bangladesh Asia 2002 62.0 135656790 1136. 10 Belgium Europe 2002 78.3 10311970 30486. # … with 132 more rows ``` ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% * ggplot() ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + * aes(x = gdpPercap / 1000) ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + * aes(y = lifeExp) ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + * geom_point(data = . %>% select(-continent), * color = "darkgrey") ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point(data = . %>% select(-continent), color = "darkgrey") + * geom_point(color = "magenta") ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-background-auto[ ```r gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap / 1000) + aes(y = lifeExp) + geom_point(data = . %>% select(-continent), color = "darkgrey") + geom_point(color = "magenta") + * facet_wrap(~ continent) ``` ] .right-panel-background-auto[ <img src="facet_files/figure-html/background_auto_8_output-1.png" width="100%" /> ] <style> .left-panel-background-auto { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-background-auto { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-background-auto { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-categorical_free-rotate[ ```r ggplot(mpg, aes(drv, model)) + geom_point() + facet_grid(manufacturer ~ ., scales = "free", space = "free") + # no strip position argument theme(strip.text.y = element_text(angle = 0)) ``` ] .right-panel-categorical_free-rotate[ <img src="facet_files/figure-html/categorical_free_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-categorical_free-rotate[ ```r ggplot(mpg, aes(drv, model)) + geom_point() + * ggforce::facet_col(facets = vars(manufacturer), scales = "free_y", space = "free", strip.position = "top") + theme(strip.text.y = element_text(angle = 0)) ``` ] .right-panel-categorical_free-rotate[ <img src="facet_files/figure-html/categorical_free_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-categorical_free-rotate[ ```r ggplot(mpg, aes(drv, model)) + geom_point() + * ggforce::facet_col(facets = vars(manufacturer), scales = "free_y", space = "free", strip.position = "left") + theme(strip.text.y = element_text(angle = 0)) ``` ] .right-panel-categorical_free-rotate[ <img src="facet_files/figure-html/categorical_free_rotate_3_output-1.png" width="100%" /> ] <style> .left-panel-categorical_free-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-categorical_free-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-categorical_free-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-pyramid-auto[ ```r *gapminder ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 1,704 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 1952 28.8 8425333 779. 2 Afghanistan Asia 1957 30.3 9240934 821. 3 Afghanistan Asia 1962 32.0 10267083 853. 4 Afghanistan Asia 1967 34.0 11537966 836. 5 Afghanistan Asia 1972 36.1 13079460 740. 6 Afghanistan Asia 1977 38.4 14880372 786. 7 Afghanistan Asia 1982 39.9 12881816 978. 8 Afghanistan Asia 1987 40.8 13867957 852. 9 Afghanistan Asia 1992 41.7 16317921 649. 10 Afghanistan Asia 1997 41.8 22227415 635. # … with 1,694 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% * filter(year == 2002 | year == 2007) ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 284 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Afghanistan Asia 2002 42.1 25268405 727. 2 Afghanistan Asia 2007 43.8 31889923 975. 3 Albania Europe 2002 75.7 3508512 4604. 4 Albania Europe 2007 76.4 3600523 5937. 5 Algeria Africa 2002 71.0 31287142 5288. 6 Algeria Africa 2007 72.3 33333216 6223. 7 Angola Africa 2002 41.0 10866106 2773. 8 Angola Africa 2007 42.7 12420476 4797. 9 Argentina Americas 2002 74.3 38331121 8798. 10 Argentina Americas 2007 75.3 40301927 12779. # … with 274 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% * filter(continent == "Americas") ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 50 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Argentina Americas 2002 74.3 38331121 8798. 2 Argentina Americas 2007 75.3 40301927 12779. 3 Bolivia Americas 2002 63.9 8445134 3413. 4 Bolivia Americas 2007 65.6 9119152 3822. 5 Brazil Americas 2002 71.0 179914212 8131. 6 Brazil Americas 2007 72.4 190010647 9066. 7 Canada Americas 2002 79.8 31902268 33329. 8 Canada Americas 2007 80.7 33390141 36319. 9 Chile Americas 2002 77.9 15497046 10779. 10 Chile Americas 2007 78.6 16284741 13172. # … with 40 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% * mutate(year = factor(year)) # as categorical ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 50 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <fct> <dbl> <int> <dbl> 1 Argentina Americas 2002 74.3 38331121 8798. 2 Argentina Americas 2007 75.3 40301927 12779. 3 Bolivia Americas 2002 63.9 8445134 3413. 4 Bolivia Americas 2007 65.6 9119152 3822. 5 Brazil Americas 2002 71.0 179914212 8131. 6 Brazil Americas 2007 72.4 190010647 9066. 7 Canada Americas 2002 79.8 31902268 33329. 8 Canada Americas 2007 80.7 33390141 36319. 9 Chile Americas 2002 77.9 15497046 10779. 10 Chile Americas 2007 78.6 16284741 13172. # … with 40 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp * mutate(country = reorder(country, -lifeExp, mean)) ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 50 x 6 country continent year lifeExp pop gdpPercap <fct> <fct> <fct> <dbl> <int> <dbl> 1 Argentina Americas 2002 74.3 38331121 8798. 2 Argentina Americas 2007 75.3 40301927 12779. 3 Bolivia Americas 2002 63.9 8445134 3413. 4 Bolivia Americas 2007 65.6 9119152 3822. 5 Brazil Americas 2002 71.0 179914212 8131. 6 Brazil Americas 2007 72.4 190010647 9066. 7 Canada Americas 2002 79.8 31902268 33329. 8 Canada Americas 2007 80.7 33390141 36319. 9 Chile Americas 2002 77.9 15497046 10779. 10 Chile Americas 2007 78.6 16284741 13172. # … with 40 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step * mutate(life_exp_mod = * ifelse(year == 2002, -lifeExp, lifeExp)) ``` ] .right-panel-pyramid-auto[ ``` # A tibble: 50 x 7 country continent year lifeExp pop gdpPercap life_exp_mod <fct> <fct> <fct> <dbl> <int> <dbl> <dbl> 1 Argentina Americas 2002 74.3 38331121 8798. -74.3 2 Argentina Americas 2007 75.3 40301927 12779. 75.3 3 Bolivia Americas 2002 63.9 8445134 3413. -63.9 4 Bolivia Americas 2007 65.6 9119152 3822. 65.6 5 Brazil Americas 2002 71.0 179914212 8131. -71.0 6 Brazil Americas 2007 72.4 190010647 9066. 72.4 7 Canada Americas 2002 79.8 31902268 33329. -79.8 8 Canada Americas 2007 80.7 33390141 36319. 80.7 9 Chile Americas 2002 77.9 15497046 10779. -77.9 10 Chile Americas 2007 78.6 16284741 13172. 78.6 # … with 40 more rows ``` ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% * ggplot() ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + * aes(x = life_exp_mod, y = country, fill = year) ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + aes(x = life_exp_mod, y = country, fill = year) + * geom_col() ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + aes(x = life_exp_mod, y = country, fill = year) + geom_col() + * ggpol::facet_share(~ year, * reverse_num = TRUE, * scales = "free") ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + aes(x = life_exp_mod, y = country, fill = year) + geom_col() + ggpol::facet_share(~ year, reverse_num = TRUE, scales = "free") + * theme(legend.position = "none") ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + aes(x = life_exp_mod, y = country, fill = year) + geom_col() + ggpol::facet_share(~ year, reverse_num = TRUE, scales = "free") + theme(legend.position = "none") + * labs(y = NULL) ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-pyramid-auto[ ```r gapminder %>% filter(year == 2002 | year == 2007) %>% filter(continent == "Americas") %>% mutate(year = factor(year)) %>% # as categorical # reorder by reverse size of mean life exp mutate(country = reorder(country, -lifeExp, mean)) %>% # pay attention to this weird prep step mutate(life_exp_mod = ifelse(year == 2002, -lifeExp, lifeExp)) %>% ggplot() + aes(x = life_exp_mod, y = country, fill = year) + geom_col() + ggpol::facet_share(~ year, reverse_num = TRUE, scales = "free") + theme(legend.position = "none") + labs(y = NULL) + * scale_y_discrete() ``` ] .right-panel-pyramid-auto[ <img src="facet_files/figure-html/pyramid_auto_13_output-1.png" width="100%" /> ] <style> .left-panel-pyramid-auto { color: #777; width: 54%; height: 92%; float: left; font-size: 80% } .right-panel-pyramid-auto { width: 45%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-pyramid-auto { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-matrix-auto[ ```r *set.seed(12345) ``` ] .right-panel-matrix-auto[ ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) *library(ggforce) ``` ] .right-panel-matrix-auto[ ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) *diamonds ``` ] .right-panel-matrix-auto[ ``` # A tibble: 53,940 x 10 carat cut color clarity depth table price x y z <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 # … with 53,930 more rows ``` ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% * select(carat, table, price, clarity) ``` ] .right-panel-matrix-auto[ ``` # A tibble: 53,940 x 4 carat table price clarity <dbl> <dbl> <int> <ord> 1 0.23 55 326 SI2 2 0.21 61 326 SI1 3 0.23 65 327 VS1 4 0.290 58 334 VS2 5 0.31 58 335 SI2 6 0.24 57 336 VVS2 7 0.24 57 336 VVS1 8 0.26 55 337 SI1 9 0.22 61 337 VS2 10 0.23 61 338 VS1 # … with 53,930 more rows ``` ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% * sample_frac(.1) ``` ] .right-panel-matrix-auto[ ``` # A tibble: 5,394 x 4 carat table price clarity <dbl> <dbl> <int> <ord> 1 1.01 56 4886 SI1 2 0.41 61 827 VS2 3 1.26 57 4704 SI2 4 0.3 57 554 SI1 5 0.34 56 726 VS2 6 2 56 15851 SI1 7 1.16 57 9526 VVS1 8 0.54 56 1760 VS2 9 1.51 60 14375 VS1 10 0.31 58 907 VVS2 # … with 5,384 more rows ``` ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% * ggplot() ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + * ggforce::facet_matrix( * rows = vars(everything()) * ) ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + * aes(x = .panel_x) ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + aes(x = .panel_x) + * aes(y = .panel_y) ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + aes(x = .panel_x) + aes(y = .panel_y) + # first geom layer * geom_point(alpha = 0.2) ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + aes(x = .panel_x) + aes(y = .panel_y) + # first geom layer geom_point(alpha = 0.2) + # second geom layer * ggforce::geom_autodensity() ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + aes(x = .panel_x) + aes(y = .panel_y) + # first geom layer geom_point(alpha = 0.2) + # second geom layer ggforce::geom_autodensity() + # third geom layer * geom_density2d() ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-matrix-auto[ ```r set.seed(12345) library(ggforce) diamonds %>% select(carat, table, price, clarity) %>% sample_frac(.1) %>% ggplot() + ggforce::facet_matrix( rows = vars(everything()) ) + aes(x = .panel_x) + aes(y = .panel_y) + # first geom layer geom_point(alpha = 0.2) + # second geom layer ggforce::geom_autodensity() + # third geom layer geom_density2d() + # overwrites above facet specification with new * ggforce::facet_matrix( * rows = vars(everything()), * layer.lower = 1, # first geom layer * layer.diag = c(1,2), # first, second geom layer * layer.upper = 3, # third geom layer * grid.y.diag = FALSE * ) ``` ] .right-panel-matrix-auto[ <img src="facet_files/figure-html/matrix_auto_13_output-1.png" width="100%" /> ] <style> .left-panel-matrix-auto { color: #777; width: 59%; height: 92%; float: left; font-size: 80% } .right-panel-matrix-auto { width: 40%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-matrix-auto { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- Next up is ['greetings'](themes.html) (themes are what greets the eye). <style type="text/css"> .remark-code{line-height: 1.5; font-size: 60%} </style>