class: center, middle, inverse, title-slide # Lab 9: ggplot2 extension packages for visualization ### Ramses Llobet --- ## On General Additive Models (HW3) - I gathered some useful resources on General Additive Models. They all rely on the same package, `mgcv` for GAMs. -- - This tutorial from [Noam Ross](https://noamross.github.io/gams-in-r-course/) will provide you with the basic knowledge to do EDA. -- - This other tutorial from [Michael Clark](https://m-clark.github.io/generalized-additive-models/) is a little bit more formal, but it provides some theoretical background to complement with the previous one. -- - In addition, find [here](https://github.com/gavinsimpson/gratia) the `gratia` package, which provides ‘ggplot’-based graphics and utility functions for working with generalized additive models (GAMs) fitted using the ‘mgcv’ package. --- ## Today's lab - Many new extension packages for visualization -- - `gghighlight`: highlighting specific lines and points while preserving main data. Examples adopted from [Hiroaki Yutani](https://cran.r-project.org/web/packages/gghighlight/vignettes/gghighlight.html) and [Datanovia](https://www.datanovia.com/en/blog/gghighlight-easy-way-to-highlight-a-ggplot-in-r/). -- - `geofacet`: arranging plots in a grid that resembles original geographical orientation. Example adopted from [geofacet vignette](https://hafen.github.io/geofacet/). -- - `ggridges`: visualizing multiple distributions over time or across groups. Example adopted from [Claus O. Wilke](https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html). -- - `ggforce`: annotating areas by group attributes. Example taken from [Edgar Ruiz](https://rviews.rstudio.com/2019/09/19/intro-to-ggforce/). -- - `gganimate`: creating animations from ggplot2 objects. Examples taken from [gganimate vignette](https://gganimate.com/index.html). -- - `ggalluvial`: flow diagram to represent changes in network structure over time. Example taken from [Cory Brunson](https://github.com/corybrunson/ggalluvial). -- - `flipbookr` + `xaringan`: making flipbook-style HTML slideshows. See the examples from [Evangeline Reynolds](https://evamaerey.github.io/little_flipbooks_library/about/what_the_flipbook). --- class: middle, inverse, center # gghighlight --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r *as_tibble(iris) ``` ] .panel2-gghighlight-auto[ ``` # A tibble: 150 x 5 Sepal.Length Sepal.Width Petal.Length Petal.Width Species <dbl> <dbl> <dbl> <dbl> <fct> 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa 7 4.6 3.4 1.4 0.3 setosa 8 5 3.4 1.5 0.2 setosa 9 4.4 2.9 1.4 0.2 setosa 10 4.9 3.1 1.5 0.1 setosa # ... with 140 more rows ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% * ggplot(aes(Sepal.Length, fill = Species)) ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + * geom_histogram(bins = 30) ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + geom_histogram(bins = 30) + * scale_fill_viridis_d() ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + geom_histogram(bins = 30) + scale_fill_viridis_d() + * gghighlight() ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + geom_histogram(bins = 30) + scale_fill_viridis_d() + gghighlight() + * facet_wrap(~ Species) ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + geom_histogram(bins = 30) + scale_fill_viridis_d() + gghighlight() + facet_wrap(~ Species) + * scale_y_continuous(expand = c(0, 0)) ``` ] .panel2-gghighlight-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight-auto[ ```r as_tibble(iris) %>% ggplot(aes(Sepal.Length, fill = Species)) + geom_histogram(bins = 30) + scale_fill_viridis_d() + gghighlight() + facet_wrap(~ Species) + scale_y_continuous(expand = c(0, 0)) + * theme_minimal() ``` ] .panel2-gghighlight-auto[ <!-- --> ] <style> .panel1-gghighlight-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gghighlight-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gghighlight-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap *gapminder ``` ] .panel2-gghighlight2-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 ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% * group_by(country, continent) ``` ] .panel2-gghighlight2-auto[ ``` # A tibble: 1,704 x 6 # Groups: country, continent [142] 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 ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% * summarize(mean_gdpPercap = mean(gdpPercap)) ``` ] .panel2-gghighlight2-auto[ ``` # A tibble: 142 x 3 # Groups: country [142] country continent mean_gdpPercap <fct> <fct> <dbl> 1 Afghanistan Asia 803. 2 Albania Europe 3255. 3 Algeria Africa 4426. 4 Angola Africa 3607. 5 Argentina Americas 8956. 6 Australia Oceania 19981. 7 Austria Europe 20412. 8 Bahrain Asia 18078. 9 Bangladesh Asia 818. 10 Belgium Europe 19901. # ... with 132 more rows ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% * group_by(continent) ``` ] .panel2-gghighlight2-auto[ ``` # A tibble: 142 x 3 # Groups: continent [5] country continent mean_gdpPercap <fct> <fct> <dbl> 1 Afghanistan Asia 803. 2 Albania Europe 3255. 3 Algeria Africa 4426. 4 Angola Africa 3607. 5 Argentina Americas 8956. 6 Australia Oceania 19981. 7 Austria Europe 20412. 8 Bahrain Asia 18078. 9 Bangladesh Asia 818. 10 Belgium Europe 19901. # ... with 132 more rows ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% * slice_max(mean_gdpPercap, n = 3) ``` ] .panel2-gghighlight2-auto[ ``` # A tibble: 14 x 3 # Groups: continent [5] country continent mean_gdpPercap <fct> <fct> <dbl> 1 Libya Africa 12014. 2 Gabon Africa 11530. 3 South Africa Africa 7247. 4 United States Americas 26261. 5 Canada Americas 22411. 6 Puerto Rico Americas 10863. 7 Kuwait Asia 65333. 8 Saudi Arabia Asia 20262. 9 Bahrain Asia 18078. 10 Switzerland Europe 27074. 11 Norway Europe 26747. 12 Netherlands Europe 21749. 13 Australia Oceania 19981. 14 New Zealand Oceania 17263. ``` ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> * top_gdppc ``` ] .panel2-gghighlight2-auto[ ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc *ggplot(gapminder, aes(x = year, y = gdpPercap/1000, * color = country)) ``` ] .panel2-gghighlight2-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + * geom_line(show.legend = FALSE) ``` ] .panel2-gghighlight2-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + * scale_y_log10() ``` ] .panel2-gghighlight2-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + scale_y_log10() + * facet_wrap(~ continent) ``` ] .panel2-gghighlight2-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + scale_y_log10() + facet_wrap(~ continent) + * gghighlight( * country %in% top_gdppc$country, * use_group_by = FALSE, * label_params = list(nudge_y = 2, direction = "x", * label.size = 0, label.padding = 0, * box.padding = 0.5, * arrow = arrow(length = unit(0.1, "cm"), * type = "open")) * ) ``` ] .panel2-gghighlight2-auto[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-auto[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + scale_y_log10() + facet_wrap(~ continent) + gghighlight( country %in% top_gdppc$country, use_group_by = FALSE, label_params = list(nudge_y = 2, direction = "x", label.size = 0, label.padding = 0, box.padding = 0.5, arrow = arrow(length = unit(0.1, "cm"), type = "open")) ) + * theme_minimal() ``` ] .panel2-gghighlight2-auto[ <!-- --> ] <style> .panel1-gghighlight2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gghighlight2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gghighlight2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-rotate[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + scale_y_log10() + facet_wrap(~ continent) + gghighlight( country %in% top_gdppc$country, use_group_by = FALSE, * calculate_per_facet = FALSE, label_params = list(nudge_y = 2, direction = "x", label.size = 0, label.padding = 0, box.padding = 0.5, arrow = arrow(length = unit(0.1, "cm"), type = "open")) ) + theme_minimal() ``` ] .panel2-gghighlight2-rotate[ <!-- --> ] --- count: false ### gghighlight :: highlighting specific lines and points while preserving main data .panel1-gghighlight2-rotate[ ```r ## For each continent, find top 3 countries by avg GDP per cap gapminder %>% group_by(country, continent) %>% summarize(mean_gdpPercap = mean(gdpPercap)) %>% group_by(continent) %>% slice_max(mean_gdpPercap, n = 3) -> top_gdppc ggplot(gapminder, aes(x = year, y = gdpPercap/1000, color = country)) + geom_line(show.legend = FALSE) + scale_y_log10() + facet_wrap(~ continent) + gghighlight( country %in% top_gdppc$country, use_group_by = FALSE, * calculate_per_facet = TRUE, label_params = list(nudge_y = 2, direction = "x", label.size = 0, label.padding = 0, box.padding = 0.5, arrow = arrow(length = unit(0.1, "cm"), type = "open")) ) + theme_minimal() ``` ] .panel2-gghighlight2-rotate[ <!-- --> ] <style> .panel1-gghighlight2-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gghighlight2-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gghighlight2-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle, inverse, center # geofacet --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r *as_tibble(state_unemp) ``` ] .panel2-geofacet-auto[ ``` # A tibble: 867 x 3 year rate state <int> <dbl> <chr> 1 2000 4.6 AL 2 2001 6 AL 3 2002 5.8 AL 4 2003 6 AL 5 2004 5.2 AL 6 2005 4.2 AL 7 2006 3.9 AL 8 2007 4.4 AL 9 2008 8.3 AL 10 2009 11.8 AL # ... with 857 more rows ``` ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r as_tibble(state_unemp) %>% * ggplot(aes(x = year, y = rate)) ``` ] .panel2-geofacet-auto[ <!-- --> ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + * facet_geo(~ state, grid = "us_state_grid1") ``` ] .panel2-geofacet-auto[ <!-- --> ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + facet_geo(~ state, grid = "us_state_grid1") + * geom_line(color = "steelblue") ``` ] .panel2-geofacet-auto[ <!-- --> ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + facet_geo(~ state, grid = "us_state_grid1") + geom_line(color = "steelblue") + * scale_x_continuous( * labels = function(x) paste0("'", substr(x, 3, 4)) * ) ``` ] .panel2-geofacet-auto[ <!-- --> ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-auto[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + facet_geo(~ state, grid = "us_state_grid1") + geom_line(color = "steelblue") + scale_x_continuous( labels = function(x) paste0("'", substr(x, 3, 4)) ) + * labs(y = "Unemployment Rate (%)", x = "Year") ``` ] .panel2-geofacet-auto[ <!-- --> ] <style> .panel1-geofacet-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-geofacet-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-geofacet-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-rotate[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + * facet_geo(~ state, grid = "us_state_grid1") + geom_line(color = "steelblue") + scale_x_continuous( labels = function(x) paste0("'", substr(x, 3, 4)) ) + labs(y = "Unemployment Rate (%)", x = "Year") ``` ] .panel2-geofacet-rotate[ <!-- --> ] --- count: false ### geofacet :: arranging plots in a grid that resembles original geographical orientation .panel1-geofacet-rotate[ ```r as_tibble(state_unemp) %>% ggplot(aes(x = year, y = rate)) + * facet_geo(~ state, grid = "us_state_grid2") + geom_line(color = "steelblue") + scale_x_continuous( labels = function(x) paste0("'", substr(x, 3, 4)) ) + labs(y = "Unemployment Rate (%)", x = "Year") ``` ] .panel2-geofacet-rotate[ <!-- --> ] <style> .panel1-geofacet-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-geofacet-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-geofacet-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle, inverse, center # ggridges --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r *dark_colors <- c("#D55E00", "#0072B2") ``` ] .panel2-ggridges-auto[ ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") *light_colors <- c("#D55E0050", "#0072B250") ``` ] .panel2-ggridges-auto[ ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") *as_tibble(Aus_athletes) ``` ] .panel2-ggridges-auto[ ``` # A tibble: 202 x 13 rcc wcc hc hg ferr bmi ssf pcBfat lbm height weight sex <dbl> <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct> 1 3.96 7.5 37.5 12.3 60 20.6 109. 19.8 63.3 196. 78.9 f 2 4.41 8.3 38.2 12.7 68 20.7 103. 21.3 58.6 190. 74.4 f 3 4.14 5 36.4 11.6 21 21.9 105. 19.9 55.4 178. 69.1 f 4 4.11 5.3 37.3 12.6 69 21.9 126. 23.7 57.2 185 74.9 f 5 4.45 6.8 41.5 14 29 19.0 80.3 17.6 53.2 185. 64.6 f 6 4.1 4.4 37.4 12.5 42 21.0 75.2 15.6 53.8 174 63.7 f 7 4.31 5.3 39.6 12.8 73 21.7 87.2 20.0 60.2 186. 75.2 f 8 4.42 5.7 39.9 13.2 44 20.6 97.9 22.4 48.3 174. 62.3 f 9 4.3 8.9 41.1 13.5 41 22.6 75.1 18.0 54.6 171. 66.5 f 10 4.51 4.4 41.6 12.7 44 19.4 65.1 15.1 53.4 180. 62.9 f # ... with 192 more rows, and 1 more variable: sport <chr> ``` ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% * ggplot(aes(x = height, y = sport, fill = sex, * color = sex, point_color = sex)) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + * geom_density_ridges( * jittered_points = TRUE, scale = 1, * rel_min_height = .01, point_shape = "|", * point_size = 3, size = 0.25, * position = position_points_jitter(height = 0) * ) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + * scale_y_discrete(expand = c(0, 0)) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + * scale_x_continuous(expand = c(0, 0), name = "height [cm]") ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + * scale_fill_manual(values = light_colors) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + * scale_color_manual(values = dark_colors) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + scale_color_manual(values = dark_colors) + * scale_discrete_manual("point_color", values = dark_colors) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + scale_color_manual(values = dark_colors) + scale_discrete_manual("point_color", values = dark_colors) + * guides(fill = guide_legend( * override.aes = list(point_color = NA, fill = light_colors), * color = "none", point_color = "none") * ) ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + scale_color_manual(values = dark_colors) + scale_discrete_manual("point_color", values = dark_colors) + guides(fill = guide_legend( override.aes = list(point_color = NA, fill = light_colors), color = "none", point_color = "none") ) + * coord_cartesian(clip = "off") ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + scale_color_manual(values = dark_colors) + scale_discrete_manual("point_color", values = dark_colors) + guides(fill = guide_legend( override.aes = list(point_color = NA, fill = light_colors), color = "none", point_color = "none") ) + coord_cartesian(clip = "off") + * ggtitle("Height in Australian athletes") ``` ] .panel2-ggridges-auto[ <!-- --> ] --- count: false ### ggridges :: visualizing multiple distributions over time or across groups .panel1-ggridges-auto[ ```r dark_colors <- c("#D55E00", "#0072B2") light_colors <- c("#D55E0050", "#0072B250") as_tibble(Aus_athletes) %>% ggplot(aes(x = height, y = sport, fill = sex, color = sex, point_color = sex)) + geom_density_ridges( jittered_points = TRUE, scale = 1, rel_min_height = .01, point_shape = "|", point_size = 3, size = 0.25, position = position_points_jitter(height = 0) ) + scale_y_discrete(expand = c(0, 0)) + scale_x_continuous(expand = c(0, 0), name = "height [cm]") + scale_fill_manual(values = light_colors) + scale_color_manual(values = dark_colors) + scale_discrete_manual("point_color", values = dark_colors) + guides(fill = guide_legend( override.aes = list(point_color = NA, fill = light_colors), color = "none", point_color = "none") ) + coord_cartesian(clip = "off") + ggtitle("Height in Australian athletes") + * theme_ridges(center = TRUE) ``` ] .panel2-ggridges-auto[ <!-- --> ] <style> .panel1-ggridges-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ggridges-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ggridges-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle, inverse, center # ggforce --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r *as_tibble(airports) ``` ] .panel2-ggforce-auto[ ``` # A tibble: 1,458 x 8 faa name lat lon alt tz dst tzone <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/~ 2 06A Moton Field Municipal Airport 32.5 -85.7 264 -6 A America/~ 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/~ 4 06N Randall Airport 41.4 -74.4 523 -5 A America/~ 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/~ 6 0A9 Elizabethton Municipal Airport 36.4 -82.2 1593 -5 A America/~ 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/~ 8 0G7 Finger Lakes Regional Airport 42.9 -76.8 492 -5 A America/~ 9 0P2 Shoestring Aviation Airfield 39.8 -76.6 1000 -5 U America/~ 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/~ # ... with 1,448 more rows ``` ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% * filter(lon < 0, tzone != "\\N") ``` ] .panel2-ggforce-auto[ ``` # A tibble: 1,452 x 8 faa name lat lon alt tz dst tzone <chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr> 1 04G Lansdowne Airport 41.1 -80.6 1044 -5 A America/~ 2 06A Moton Field Municipal Airport 32.5 -85.7 264 -6 A America/~ 3 06C Schaumburg Regional 42.0 -88.1 801 -6 A America/~ 4 06N Randall Airport 41.4 -74.4 523 -5 A America/~ 5 09J Jekyll Island Airport 31.1 -81.4 11 -5 A America/~ 6 0A9 Elizabethton Municipal Airport 36.4 -82.2 1593 -5 A America/~ 7 0G6 Williams County Airport 41.5 -84.5 730 -5 A America/~ 8 0G7 Finger Lakes Regional Airport 42.9 -76.8 492 -5 A America/~ 9 0P2 Shoestring Aviation Airfield 39.8 -76.6 1000 -5 U America/~ 10 0S9 Jefferson County Intl 48.1 -123. 108 -8 A America/~ # ... with 1,442 more rows ``` ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% * ggplot(aes(x = lon, y = lat, color = tzone)) ``` ] .panel2-ggforce-auto[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + * geom_point() ``` ] .panel2-ggforce-auto[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + * geom_mark_rect() ``` ] .panel2-ggforce-auto[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + geom_mark_rect() + * guides(fill = "none", color = "none") ``` ] .panel2-ggforce-auto[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-auto[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + geom_mark_rect() + guides(fill = "none", color = "none") + * theme_no_axes() ``` ] .panel2-ggforce-auto[ <!-- --> ] <style> .panel1-ggforce-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ggforce-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ggforce-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-rotate[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + * geom_mark_rect(aes(label = tzone)) + guides(fill = "none", color = "none") + theme_no_axes() ``` ] .panel2-ggforce-rotate[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-rotate[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + * geom_mark_hull(aes(label = tzone)) + guides(fill = "none", color = "none") + theme_no_axes() ``` ] .panel2-ggforce-rotate[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce-rotate[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + * geom_mark_hull(aes(label = tzone, fill = tzone)) + guides(fill = "none", color = "none") + theme_no_axes() ``` ] .panel2-ggforce-rotate[ <!-- --> ] <style> .panel1-ggforce-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ggforce-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ggforce-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ---- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce2-non_seq[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + geom_mark_hull( aes(label = tzone, fill = tzone), ) + guides(fill = "none", color = "none") + theme_no_axes() ``` ] .panel2-ggforce2-non_seq[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce2-non_seq[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + geom_mark_hull( aes(label = tzone, fill = tzone), * expand = unit(3, "mm") ) + guides(fill = "none", color = "none") + theme_no_axes() ``` ] .panel2-ggforce2-non_seq[ <!-- --> ] --- count: false ### ggforce :: annotating areas by group attributes .panel1-ggforce2-non_seq[ ```r as_tibble(airports) %>% filter(lon < 0, tzone != "\\N") %>% ggplot(aes(x = lon, y = lat, color = tzone)) + geom_point() + geom_mark_hull( aes(label = tzone, fill = tzone), expand = unit(3, "mm") ) + guides(fill = "none", color = "none") + theme_no_axes() + * facet_zoom(x = tzone == "America/Los_Angeles") ``` ] .panel2-ggforce2-non_seq[ <!-- --> ] <style> .panel1-ggforce2-non_seq { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ggforce2-non_seq { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ggforce2-non_seq { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle, inverse, center # gganimate --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r *gapminder ``` ] .panel2-gganimate-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 ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% * ggplot(aes(x = gdpPercap, y = lifeExp, * size = pop, colour = country)) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + * geom_point(alpha = 0.7, show.legend = FALSE) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + * facet_wrap(~ continent) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + * scale_x_log10() ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + scale_x_log10() + * scale_size(range = c(2, 12)) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + scale_x_log10() + scale_size(range = c(2, 12)) + * scale_colour_manual(values = country_colors) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + scale_x_log10() + scale_size(range = c(2, 12)) + scale_colour_manual(values = country_colors) + * labs(title = 'Year: {frame_time}', * x = 'GDP per capita', * y = 'life expectancy') ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + scale_x_log10() + scale_size(range = c(2, 12)) + scale_colour_manual(values = country_colors) + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + * transition_time(year) ``` ] .panel2-gganimate-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate-auto[ ```r gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + facet_wrap(~ continent) + scale_x_log10() + scale_size(range = c(2, 12)) + scale_colour_manual(values = country_colors) + labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year) + * ease_aes('linear') ``` ] .panel2-gganimate-auto[ <!-- --> ] <style> .panel1-gganimate-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gganimate-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gganimate-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r *gapminder ``` ] .panel2-gganimate2-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 ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r gapminder %>% * group_by(year) ``` ] .panel2-gganimate2-auto[ ``` # A tibble: 1,704 x 6 # Groups: year [12] 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 ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r gapminder %>% group_by(year) %>% * arrange(year, desc(gdpPercap)) ``` ] .panel2-gganimate2-auto[ ``` # A tibble: 1,704 x 6 # Groups: year [12] country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Kuwait Asia 1952 55.6 160000 108382. 2 Switzerland Europe 1952 69.6 4815000 14734. 3 United States Americas 1952 68.4 157553000 13990. 4 Canada Americas 1952 68.8 14785584 11367. 5 New Zealand Oceania 1952 69.4 1994794 10557. 6 Norway Europe 1952 72.7 3327728 10095. 7 Australia Oceania 1952 69.1 8691212 10040. 8 United Kingdom Europe 1952 69.2 50430000 9980. 9 Bahrain Asia 1952 50.9 120447 9867. 10 Denmark Europe 1952 70.8 4334000 9692. # ... with 1,694 more rows ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r gapminder %>% group_by(year) %>% arrange(year, desc(gdpPercap)) %>% * mutate(rank = row_number()) ``` ] .panel2-gganimate2-auto[ ``` # A tibble: 1,704 x 7 # Groups: year [12] country continent year lifeExp pop gdpPercap rank <fct> <fct> <int> <dbl> <int> <dbl> <int> 1 Kuwait Asia 1952 55.6 160000 108382. 1 2 Switzerland Europe 1952 69.6 4815000 14734. 2 3 United States Americas 1952 68.4 157553000 13990. 3 4 Canada Americas 1952 68.8 14785584 11367. 4 5 New Zealand Oceania 1952 69.4 1994794 10557. 5 6 Norway Europe 1952 72.7 3327728 10095. 6 7 Australia Oceania 1952 69.1 8691212 10040. 7 8 United Kingdom Europe 1952 69.2 50430000 9980. 8 9 Bahrain Asia 1952 50.9 120447 9867. 9 10 Denmark Europe 1952 70.8 4334000 9692. 10 # ... with 1,694 more rows ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r gapminder %>% group_by(year) %>% arrange(year, desc(gdpPercap)) %>% mutate(rank = row_number()) %>% * filter(rank <= 10) ``` ] .panel2-gganimate2-auto[ ``` # A tibble: 120 x 7 # Groups: year [12] country continent year lifeExp pop gdpPercap rank <fct> <fct> <int> <dbl> <int> <dbl> <int> 1 Kuwait Asia 1952 55.6 160000 108382. 1 2 Switzerland Europe 1952 69.6 4815000 14734. 2 3 United States Americas 1952 68.4 157553000 13990. 3 4 Canada Americas 1952 68.8 14785584 11367. 4 5 New Zealand Oceania 1952 69.4 1994794 10557. 5 6 Norway Europe 1952 72.7 3327728 10095. 6 7 Australia Oceania 1952 69.1 8691212 10040. 7 8 United Kingdom Europe 1952 69.2 50430000 9980. 8 9 Bahrain Asia 1952 50.9 120447 9867. 9 10 Denmark Europe 1952 70.8 4334000 9692. 10 # ... with 110 more rows ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate2-auto[ ```r gapminder %>% group_by(year) %>% arrange(year, desc(gdpPercap)) %>% mutate(rank = row_number()) %>% filter(rank <= 10) -> * gdppc_ranked_year ``` ] .panel2-gganimate2-auto[ ] <style> .panel1-gganimate2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gganimate2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gganimate2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r *gdppc_ranked_year ``` ] .panel2-gganimate3-auto[ ``` # A tibble: 120 x 7 # Groups: year [12] country continent year lifeExp pop gdpPercap rank <fct> <fct> <int> <dbl> <int> <dbl> <int> 1 Kuwait Asia 1952 55.6 160000 108382. 1 2 Switzerland Europe 1952 69.6 4815000 14734. 2 3 United States Americas 1952 68.4 157553000 13990. 3 4 Canada Americas 1952 68.8 14785584 11367. 4 5 New Zealand Oceania 1952 69.4 1994794 10557. 5 6 Norway Europe 1952 72.7 3327728 10095. 6 7 Australia Oceania 1952 69.1 8691212 10040. 7 8 United Kingdom Europe 1952 69.2 50430000 9980. 8 9 Bahrain Asia 1952 50.9 120447 9867. 9 10 Denmark Europe 1952 70.8 4334000 9692. 10 # ... with 110 more rows ``` ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% * ggplot(aes(xmin = 0, * xmax = gdpPercap / 1000, * ymin = rank - 0.45, * ymax = rank + 0.45, * fill = continent)) ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + * geom_rect(alpha = 0.8) ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + * facet_wrap(~ year) ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + * geom_text(aes(y = rank, label = country), * x = -5, hjust = "right") ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + * scale_x_continuous(limits = c(-45, 115), * breaks = seq(0, 100, 25)) ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + scale_x_continuous(limits = c(-45, 115), breaks = seq(0, 100, 25)) + * scale_y_reverse() ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + scale_x_continuous(limits = c(-45, 115), breaks = seq(0, 100, 25)) + scale_y_reverse() + * scale_fill_brewer(palette = "Set3") ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + scale_x_continuous(limits = c(-45, 115), breaks = seq(0, 100, 25)) + scale_y_reverse() + scale_fill_brewer(palette = "Set3") + * labs(y = NULL, x = "GDP per capita (thousands)") ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + scale_x_continuous(limits = c(-45, 115), breaks = seq(0, 100, 25)) + scale_y_reverse() + scale_fill_brewer(palette = "Set3") + labs(y = NULL, x = "GDP per capita (thousands)") + * theme(axis.line.x = element_line(), * axis.line.y = element_blank(), * axis.ticks.y = element_blank(), * axis.text.y = element_blank(), * panel.background = element_blank()) ``` ] .panel2-gganimate3-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate3-auto[ ```r gdppc_ranked_year %>% ggplot(aes(xmin = 0, xmax = gdpPercap / 1000, ymin = rank - 0.45, ymax = rank + 0.45, fill = continent)) + geom_rect(alpha = 0.8) + facet_wrap(~ year) + geom_text(aes(y = rank, label = country), x = -5, hjust = "right") + scale_x_continuous(limits = c(-45, 115), breaks = seq(0, 100, 25)) + scale_y_reverse() + scale_fill_brewer(palette = "Set3") + labs(y = NULL, x = "GDP per capita (thousands)") + theme(axis.line.x = element_line(), axis.line.y = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank(), panel.background = element_blank()) -> * gdppc_plot ``` ] .panel2-gganimate3-auto[ ] <style> .panel1-gganimate3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gganimate3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gganimate3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r *gdppc_plot ``` ] .panel2-gganimate4-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r gdppc_plot + * facet_null() ``` ] .panel2-gganimate4-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r gdppc_plot + facet_null() + * geom_text(aes(label = as.factor(year)), * x = 90 , y = -10, size = 15, * check_overlap = TRUE) ``` ] .panel2-gganimate4-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r gdppc_plot + facet_null() + geom_text(aes(label = as.factor(year)), x = 90 , y = -10, size = 15, check_overlap = TRUE) + * transition_time(year) ``` ] .panel2-gganimate4-auto[ <!-- --> ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r gdppc_plot + facet_null() + geom_text(aes(label = as.factor(year)), x = 90 , y = -10, size = 15, check_overlap = TRUE) + transition_time(year) -> * gdppc_animate ``` ] .panel2-gganimate4-auto[ ] --- count: false ### gganimate :: creating animations from ggplot2 objects .panel1-gganimate4-auto[ ```r gdppc_plot + facet_null() + geom_text(aes(label = as.factor(year)), x = 90 , y = -10, size = 15, check_overlap = TRUE) + transition_time(year) -> gdppc_animate *animate(gdppc_animate, * fps = 20, * duration = 15, * end_pause = 20) ``` ] .panel2-gganimate4-auto[ <!-- --> ] <style> .panel1-gganimate4-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-gganimate4-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-gganimate4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: middle, inverse, center # ggalluvial --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r *titanic_wide <- data.frame(Titanic) ``` ] .panel2-ggalluvial-auto[ ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) *as_tibble(titanic_wide) ``` ] .panel2-ggalluvial-auto[ ``` # A tibble: 32 x 5 Class Sex Age Survived Freq <fct> <fct> <fct> <fct> <dbl> 1 1st Male Child No 0 2 2nd Male Child No 0 3 3rd Male Child No 35 4 Crew Male Child No 0 5 1st Female Child No 0 6 2nd Female Child No 0 7 3rd Female Child No 17 8 Crew Female Child No 0 9 1st Male Adult No 118 10 2nd Male Adult No 154 # ... with 22 more rows ``` ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% *ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, * y = Freq)) ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + * scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + * xlab("Demographic") ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + xlab("Demographic") + * geom_alluvium(aes(fill = Survived)) ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + xlab("Demographic") + geom_alluvium(aes(fill = Survived)) + * geom_stratum() ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + xlab("Demographic") + geom_alluvium(aes(fill = Survived)) + geom_stratum() + * geom_text(stat = "stratum", aes(label = after_stat(stratum))) ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + xlab("Demographic") + geom_alluvium(aes(fill = Survived)) + geom_stratum() + geom_text(stat = "stratum", aes(label = after_stat(stratum))) + * theme_minimal() ``` ] .panel2-ggalluvial-auto[ <!-- --> ] --- count: false ### ggalluvial :: visualize frequency distributions or tables .panel1-ggalluvial-auto[ ```r titanic_wide <- data.frame(Titanic) as_tibble(titanic_wide) %>% ggplot(aes(axis1 = Class, axis2 = Sex, axis3 = Age, y = Freq)) + scale_x_discrete(limits = c("Class", "Sex", "Age"), expand = c(.2, .05)) + xlab("Demographic") + geom_alluvium(aes(fill = Survived)) + geom_stratum() + geom_text(stat = "stratum", aes(label = after_stat(stratum))) + theme_minimal() + * ggtitle("passengers on the maiden voyage of the Titanic", * "stratified by demographics and survival") ``` ] .panel2-ggalluvial-auto[ <!-- --> ] <style> .panel1-ggalluvial-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ggalluvial-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ggalluvial-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### Lastly, how do I write these slides? - `flipbookr` package by [Evangeline Reynolds](https://github.com/EvaMaeRey/flipbookr): "side-by-side, aligned, incremental code-output evolution" -- - Together with `xaringan` package by [Yihui Xie](https://github.com/yihui/xaringan) for HTML slideshows -- - Take a look at my source `.Rmd`, or [vignettes by Reynolds](https://evamaerey.github.io/little_flipbooks_library/about/what_the_flipbook) if interested --- class: middle, inverse, center # Thank you! <style type="text/css"> .remark-code{line-height: 1.5; font-size: 90%} </style>