class: center, middle, inverse, title-slide # A Gentle Guide to the Grammar of Graphics
with
ggplot2
### Garrick Aden-Buie
@grrrck
### Slides and code:
bit.ly/gentle-ggplot2
--- class: fullscreen, inverse, top, center, text-white background-image: url("images/letter-g.jpg") .font150[**Brought to you by the letter...**] --- layout: true # Why *ggplot2*? --- .left-column[ ![](images/hadley.jpg) __Hadley Wickham__ ] .right-column[.font150[ The transferrable skills from ggplot2 are not the idiosyncracies of plotting syntax, but a powerful way of thinking about visualisation, as a way of **mapping between variables and the visual properties of geometric objects** that you can perceive. ] .footnote[<http://disq.us/p/sv640d>] ] --- ## My personal reasons - .hl[Functional] data visualization 1. Wrangle data 2. Map data to visual elements 3. Tweak scales, guides, axis, labels, theme - Easy to .hl[reason] about how data drives visualization - Easy to .hl[iterate] - Easy to be .hl[consistent] --- layout: false # What are we getting into? <br> `ggplot2` is a huge package: philosophy + functions <br>...but it's very well organized -- <br><br> *Lots* of examples of not-so-great plots in these slides <br>...but that's okay -- <br><br> Going to throw a lot at you <br>...but you'll know *where* and *what* to look for -- .img-right[![](images/poppins-bag.gif)] -- .img-right[![](images/poppins-bag-kids.gif)] --- layout: true # G is for getting started --- **Easy**: install the [tidyverse](http://tidyverse.org) ```r install.packages('tidyverse') ``` **Medium**: install just `ggplot2` ```r install.packages('ggplot2') ``` **Expert**: install from GitHub ```r devtools::install_github('tidyverse/ggplot2') ``` --- ## Load the tidyverse ```r library(tidyverse) ``` ``` ## ── Attaching packages ───────────────────────────────────────────────────────────────── tidyverse 1.2.1 ── ``` ``` ## ✔ ggplot2 3.1.0 ✔ purrr 0.2.5 ## ✔ tibble 1.4.2 ✔ dplyr 0.7.7 ## ✔ tidyr 0.8.1 ✔ stringr 1.3.1 ## ✔ readr 1.1.1 ✔ forcats 0.3.0 ``` ``` ## ── Conflicts ──────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── ## ✖ dplyr::filter() masks stats::filter() ## ✖ dplyr::lag() masks stats::lag() ``` --- ## Other packages you'll need for this adventure We'll use an excerpt of the [gapminder](http://www.gapminder.org/data/) dataset provided by the [`gapminder` package](https://github.com/jennybc/gapminder) by Jenny Bryan. <https://github.com/jennybc/gapminder> ```r ## install.packages("gapminder") library(gapminder) ``` --- layout: false class: inverse center middle text-white .font200[gg is for<br>Grammar of Graphics] --- # What is a grammar of graphics? .left-code[ "Good grammar is just the first step in creating a good sentence." #### How is the drawing on the right connected to data? .footnote[<http://vita.had.co.nz/papers/layered-grammar.pdf>] ] .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-0-1.png" width="100%" /> ] --- layout: true # Guess the data behind this plot? .left-code[ ### MPG Ratings of Cars - Manufacturer - Car Type (Class) - City MPG - Highway MPG ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-2-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-3-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-1-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-4-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-5-1.png" width="100%" /> ] --- .right-plot[ <img src="index_files/figure-html/guess-data-from-plot-6-1.png" width="100%" /> ] --- .right-plot[ <table> <thead> <tr> <th style="text-align:left;"> manufacturer </th> <th style="text-align:left;"> class </th> <th style="text-align:right;"> cty </th> <th style="text-align:right;"> hwy </th> <th style="text-align:left;"> model </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> audi </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:left;"> a4 </td> </tr> <tr> <td style="text-align:left;"> audi </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 20 </td> <td style="text-align:right;"> 28 </td> <td style="text-align:left;"> a4 quattro </td> </tr> <tr> <td style="text-align:left;"> ford </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 12 </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> expedition 2wd </td> </tr> <tr> <td style="text-align:left;"> ford </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 13 </td> <td style="text-align:right;"> 19 </td> <td style="text-align:left;"> explorer 4wd </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 14 </td> <td style="text-align:right;"> 17 </td> <td style="text-align:left;"> 4runner 4wd </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 22 </td> <td style="text-align:right;"> 31 </td> <td style="text-align:left;"> camry solara </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> compact </td> <td style="text-align:right;"> 26 </td> <td style="text-align:right;"> 35 </td> <td style="text-align:left;"> corolla </td> </tr> <tr> <td style="text-align:left;"> toyota </td> <td style="text-align:left;"> suv </td> <td style="text-align:right;"> 13 </td> <td style="text-align:right;"> 18 </td> <td style="text-align:left;"> land cruiser wagon 4wd </td> </tr> </tbody> </table> ] --- layout: false # How do we express visuals in words? .font120[ - **Data** to be visualized ] -- .font120[ - **.hlb[Geom]etric objects** that appear on the plot ] -- .font120[ - **.hlb[Aes]thetic mappings** from data to visual component ] -- .font120[ - **.hlb[Stat]istics** transform data on the way to visualization ] -- .font120[ - **.hlb[Coord]inates** organize location of geometric objects ] -- .font120[ - **.hlb[Scale]s** define the range of values for aesthetics ] -- .font120[ - **.hlb[Facet]s** group into subplots ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ```r ggplot(data) ``` ] --- .right-column[ #### Tidy Data 1. Each variable forms a .hl[column] 2. Each observation forms a .hl[row] 3. Each observational unit forms a table ] -- .right-column[ #### Start by asking 1. What information do I want to use in my visualization? 1. Is that data contained in .hl[one column/row] for a given data point? ] --- .right-column[ <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:right;"> 1997 </th> <th style="text-align:right;"> 2002 </th> <th style="text-align:right;"> 2007 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:right;"> 30.30584 </td> <td style="text-align:right;"> 31.90227 </td> <td style="text-align:right;"> 33.39014 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:right;"> 1230.07500 </td> <td style="text-align:right;"> 1280.40000 </td> <td style="text-align:right;"> 1318.68310 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:right;"> 272.91176 </td> <td style="text-align:right;"> 287.67553 </td> <td style="text-align:right;"> 301.13995 </td> </tr> </tbody> </table> ] -- .right-column[ ```r tidy_pop <- gather(messy_pop, 'year', 'pop', -country) ``` <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> year </th> <th style="text-align:right;"> pop </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 30.306 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 1230.075 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> 1997 </td> <td style="text-align:right;"> 272.912 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 31.902 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 1280.400 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> 2002 </td> <td style="text-align:right;"> 287.676 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> 2007 </td> <td style="text-align:right;"> 33.390 </td> </tr> <tr> <td style="text-align:left;"> China </td> <td style="text-align:left;"> 2007 </td> <td style="text-align:right;"> 1318.683 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> 2007 </td> <td style="text-align:right;"> 301.140 </td> </tr> </tbody> </table> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ```r + aes() ``` ] --- .right-column[ Map data to visual elements or parameters - year - pop - country ] --- .right-column[ Map data to visual elements or parameters - year → **x** - pop → **y** - country → *shape*, *color*, etc. ] --- .right-column[ Map data to visual elements or parameters ```r aes( x = year, y = pop, color = country ) ``` ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ```r + geom_*() ``` ] --- .right-column[ Geometric objects displayed on the plot <img src="index_files/figure-html/geom_demo-1.png" width="650px" /> ] --- .right-column[ Here are the [some of the most widely used geoms](https://eric.netlify.com/2017/08/10/most-popular-ggplot2-geoms/) .font70.center[ | Type | Function | |:----:|:--------:| | Point | `geom_point()` | | Line | `geom_line()` | | Bar | `geom_bar()`, `geom_col()` | | Histogram | `geom_histogram()` | | Regression | `geom_smooth()` | | Boxplot | `geom_boxplot()` | | Text | `geom_text()` | | Vert./Horiz. Line | `geom_{vh}line()` | | Count | `geom_count()` | | Density | `geom_density()` | <https://eric.netlify.com/2017/08/10/most-popular-ggplot2-geoms/> ] ] --- .right-column[ See <http://ggplot2.tidyverse.org/reference/> for many more options .font70[ ``` ## [1] "geom_abline" "geom_area" "geom_bar" "geom_bin2d" ## [5] "geom_blank" "geom_boxplot" "geom_col" "geom_contour" ## [9] "geom_count" "geom_crossbar" "geom_curve" "geom_density" ## [13] "geom_density_2d" "geom_density2d" "geom_dotplot" "geom_errorbar" ## [17] "geom_errorbarh" "geom_freqpoly" "geom_hex" "geom_histogram" ## [21] "geom_hline" "geom_jitter" "geom_label" "geom_line" ## [25] "geom_linerange" "geom_map" "geom_path" "geom_point" ## [29] "geom_pointrange" "geom_polygon" "geom_qq" "geom_qq_line" ## [33] "geom_quantile" "geom_raster" "geom_rect" "geom_ribbon" ## [37] "geom_rug" "geom_segment" "geom_sf" "geom_sf_label" ## [41] "geom_sf_text" "geom_smooth" "geom_spoke" "geom_step" ## [45] "geom_text" "geom_tile" "geom_violin" "geom_vline" ``` ] ] -- .right-column[ <img src="images/geom.gif" width="200px" style="float: right; margin-right: 100px; margin-top: -25px;"> Or just start typing `geom_` in RStudio ] --- layout: true # Our first plot! --- .left-code[ ```r ggplot(tidy_pop) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1a-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + * aes(x = year, * y = pop) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1b-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop) + * geom_point() ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1c-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, * color = country) + geom_point() ``` ] .right-plot[ <img src="index_files/figure-html/first-plot1-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + * geom_line() ``` .font80[ ```r geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? ``` ] ] .right-plot[ <img src="index_files/figure-html/first-plot2-fake-out-1.png" width="100%" /> ] --- .left-code[ ```r ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + geom_line( * aes(group = country)) ``` ] .right-plot[ <img src="index_files/figure-html/first-plot2-out-1.png" width="100%" /> ] --- .left-code[ ```r g <- ggplot(tidy_pop) + aes(x = year, y = pop, color = country) + geom_point() + geom_line( aes(group = country)) g ``` ] .right-plot[ <img src="index_files/figure-html/first-plot3-out-1.png" width="100%" /> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ```r + geom_*() ``` ] --- .right-column[ ```r geom_*(mapping, data, stat, position) ``` - `data` Geoms can have their own data - Has to map onto global coordinates - `map` Geoms can have their own aesthetics - Inherits global aesthetics - Have geom-specific aesthetics - `geom_point` needs `x` and `y`, optional `shape`, `color`, `size`, etc. - `geom_ribbon` requires `x`, `ymin` and `ymax`, optional `fill` - `?geom_ribbon` ] --- .right-column[ ```r geom_*(mapping, data, stat, position) ``` - `stat` Some geoms apply further transformations to the data - All respect `stat = 'identity'` - Ex: `geom_histogram` uses `stat_bin()` to group observations - `position` Some adjust location of objects - `'dodge'`, `'stack'`, `'jitter'` ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ```r +facet_wrap() +facet_grid() ``` ] --- .right-column[ ```r g + facet_wrap(~ country) ``` <img src="index_files/figure-html/geom_facet-1.png" width="90%" /> ] --- .right-column[ ```r g + facet_grid(continent ~ country) ``` <img src="index_files/figure-html/geom_grid-1.png" width="90%" /> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ```r + labs() ``` ] --- .right-column[ ```r g + labs(x = "Year", y = "Population") ``` <img src="index_files/figure-html/labs-ex-1.png" width="90%" /> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ```r + coord_*() ``` ] --- .right-column[ ```r g + coord_flip() ``` <img src="index_files/figure-html/coord-ex-1.png" width="90%" /> ] --- .right-column[ ```r g + coord_polar() ``` <img src="index_files/figure-html/coord-ex2-1.png" width="90%" /> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ### Scales ```r + scale_*_*() ``` ] --- .right-column[ `scale` + `_` + `<aes>` + `_` + `<type>` + `()` What parameter do you want to adjust? → `<aes>` <br> What type is the parameter? → `<type>` - I want to change my discrete x-axis<br>`scale_x_discrete()` - I want to change range of point sizes from continuous variable<br>`scale_size_continuous()` - I want to rescale y-axis as log<br>`scale_y_log10()` - I want to use a different color palette<br>`scale_fill_discrete()`<br>`scale_color_manual()` ] --- .right-column[ ```r g + scale_color_manual(values = c("peru", "pink", "plum")) ``` <img src="index_files/figure-html/scale_ex1-1.png" width="90%" /> ] --- .right-column[ ```r g + scale_y_log10() ``` <img src="index_files/figure-html/scale_ex2-1.png" width="90%" /> ] --- .right-column[ ```r g + scale_x_discrete(labels = c("MCMXCVII", "MMII", "MMVII")) ``` <img src="index_files/figure-html/scale_ex4-1.png" width="90%" /> ] --- layout: true # gg is for Grammar of Graphics .left-column[ ### Data ### Aesthetics ### Geoms ### Facet ### Labels ### Coords ### Scales ### Theme ```r + theme() ``` ] --- .right-column[ Change the appearance of plot decorations<br> i.e. things that aren't mapped to data A few "starter" themes ship with the package - `g + theme_bw()` - `g + theme_dark()` - `g + theme_gray()` - `g + theme_light()` - `g + theme_minimal()` ] --- .right-column[ Huge number of parameters, grouped by plot area: - Global options: `line`, `rect`, `text`, `title` - `axis`: x-, y- or other axis title, ticks, lines - `legend`: Plot legends - `panel`: Actual plot area - `plot`: Whole image - `strip`: Facet labels ] --- .right-column[ Theme options are supported by helper functions: - `element_blank()` removes the element - `element_line()` - `element_rect()` - `element_text()` ] --- .right-column[ ```r g + theme_bw() ``` <img src="index_files/figure-html/unnamed-chunk-1-1.png" width="90%" /> ] --- .right-column[ .font80[ ```r g + theme_minimal() + theme(text = element_text(family = "Palatino")) ``` <img src="index_files/figure-html/unnamed-chunk-2-1.png" width="90%" /> ] ] --- .right-column[ You can also set the theme globally with `theme_set()` ```r my_theme <- theme_bw() + theme( text = element_text(family = "Palatino", size = 12), panel.border = element_rect(colour = 'grey80'), panel.grid.minor = element_blank() ) theme_set(my_theme) ``` All plots will now use this theme! ] --- .right-column[ ```r g ``` <img src="index_files/figure-html/unnamed-chunk-3-1.png" width="90%" /> ] --- .right-column[ ```r g + theme(legend.position = 'bottom') ``` <img src="index_files/figure-html/unnamed-chunk-4-1.png" width="90%" /> ] --- layout: false # Save Your Work To save your plot, use **ggsave** ```r ggsave( filename = "my_plot.png", plot = my_plot, width = 10, height = 8, dpi = 100, device = "png" ) ``` --- layout: false count: hide class: fullscreen, inverse, top, left, text-white background-image: url(images/super-grover.jpg) .font200[You have the power!] --- class: inverse, center, middle # "Live" Coding ```r library(gapminder) ``` --- # head(gapminder) <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> <th style="text-align:right;"> lifeExp </th> <th style="text-align:right;"> pop </th> <th style="text-align:right;"> gdpPercap </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 28.801 </td> <td style="text-align:right;"> 8425333 </td> <td style="text-align:right;"> 779.4453 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 30.332 </td> <td style="text-align:right;"> 9240934 </td> <td style="text-align:right;"> 820.8530 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 31.997 </td> <td style="text-align:right;"> 10267083 </td> <td style="text-align:right;"> 853.1007 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 34.020 </td> <td style="text-align:right;"> 11537966 </td> <td style="text-align:right;"> 836.1971 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 36.088 </td> <td style="text-align:right;"> 13079460 </td> <td style="text-align:right;"> 739.9811 </td> </tr> <tr> <td style="text-align:left;"> Afghanistan </td> <td style="text-align:left;"> Asia </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 38.438 </td> <td style="text-align:right;"> 14880372 </td> <td style="text-align:right;"> 786.1134 </td> </tr> </tbody> </table> </div> --- # glimpse(gapminder) ``` Observations: 1,704 Variables: 6 $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Afghanistan, Af... $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, A... $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2... $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854, 40.822, 41.... $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 12881816,... $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.1134, 978.011... ``` -- Let's start with `lifeExp` vs `gdpPercap` --- class: fullscreen layout: true --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-1-1.png) ] -- Add points... --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp) + * geom_point() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-2-1.png) ] -- How can I tell countries apart? --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, * color = continent) + geom_point() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-3-1.png) ] -- GDP is squished together on the left --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_point() + * scale_x_log10() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-4-1.png) ] -- Still lots of overlap in the countries... --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_point() + scale_x_log10() + * facet_wrap(~ continent) + * guides(color = FALSE) ``` No need for color legend thanks to facet titles ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-5-1.png) ] -- Lots of overplotting due to point size --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + * geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-6-1.png) ] -- Is there a trend? --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + * geom_line() + geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-7-1.png) ] -- Okay, that line just connected all of the points sequentially... --- .left-code[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, color = continent) + geom_line( * aes(group = country) ) + geom_point(size = 0.25) + scale_x_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` .font200.center[🤔] ] .right-plot[ ![](index_files/figure-html/gapminder-le-gdp-8-1.png) ] -- 💡 We need time on x-axis! --- .left-code[ ```r ggplot(gapminder) + * aes(x = year, * y = gdpPercap, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * scale_y_log10() + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-gdp-year-1-1.png) ] -- Can't see x-axis labels, though --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = gdpPercap, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + scale_y_log10() + * scale_x_continuous(breaks = * seq(1950, 2000, 25) * ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-gdp-year-2-1.png) ] -- What about life expectancy? --- .left-code[ ```r ggplot(gapminder) + aes(x = year, * y = lifeExp, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * #scale_y_log10() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-1-1.png) ] -- Okay, let's add a trend line --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country) ) + geom_point(size = 0.25) + * geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-2-1.png) ] -- De-emphasize individual countries --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), * color = "grey75" ) + geom_point(size = 0.25) + geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-3-1.png) ] -- Points are still in the way --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + * #geom_point(size = 0.25) + geom_smooth() + scale_x_continuous(breaks = seq(1950, 2000, 25) ) + facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-4-1.png) ] -- Let's compare continents --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + # scale_x_continuous( # breaks = # seq(1950, 2000, 25) # ) + * # facet_wrap(~ continent) + guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-5-1.png) ] -- Wait, what color is each continent? --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + * theme( * legend.position = "bottom" * ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-6-1.png) ] -- Let's try the minimal theme --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + * theme_minimal() + theme( legend.position = "bottom" ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-7-1.png) ] -- Fonts are kind of big --- .left-code[ ```r ggplot(gapminder) + aes(x = year, y = lifeExp, color = continent) + geom_line( aes(group = country), color = "grey75" ) + geom_smooth() + theme_minimal( * base_size = 8) + theme( legend.position = "bottom" ) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-le-year-8-1.png) ] -- Cool, let's switch gears --- .left-code[ ```r americas <- gapminder %>% filter( country %in% c( "United States", "Canada", "Mexico", "Ecuador" ) ) ``` Let's look at four countries in more detail. How do their populations compare to each other? ] .right-plot[ <!-- ![](index_files/figure-html/gapminder-le-year-8-1.png) --> <div class="kable-table"> <table> <thead> <tr> <th style="text-align:left;"> country </th> <th style="text-align:left;"> continent </th> <th style="text-align:right;"> year </th> <th style="text-align:right;"> lifeExp </th> <th style="text-align:right;"> pop </th> <th style="text-align:right;"> gdpPercap </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 68.750 </td> <td style="text-align:right;"> 14785584 </td> <td style="text-align:right;"> 11367.161 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 69.960 </td> <td style="text-align:right;"> 17010154 </td> <td style="text-align:right;"> 12489.950 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 71.300 </td> <td style="text-align:right;"> 18985849 </td> <td style="text-align:right;"> 13462.486 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 72.130 </td> <td style="text-align:right;"> 20819767 </td> <td style="text-align:right;"> 16076.588 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 72.880 </td> <td style="text-align:right;"> 22284500 </td> <td style="text-align:right;"> 18970.571 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 74.210 </td> <td style="text-align:right;"> 23796400 </td> <td style="text-align:right;"> 22090.883 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 75.760 </td> <td style="text-align:right;"> 25201900 </td> <td style="text-align:right;"> 22898.792 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 76.860 </td> <td style="text-align:right;"> 26549700 </td> <td style="text-align:right;"> 26626.515 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 77.950 </td> <td style="text-align:right;"> 28523502 </td> <td style="text-align:right;"> 26342.884 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 78.610 </td> <td style="text-align:right;"> 30305843 </td> <td style="text-align:right;"> 28954.926 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 79.770 </td> <td style="text-align:right;"> 31902268 </td> <td style="text-align:right;"> 33328.965 </td> </tr> <tr> <td style="text-align:left;"> Canada </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 80.653 </td> <td style="text-align:right;"> 33390141 </td> <td style="text-align:right;"> 36319.235 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 48.357 </td> <td style="text-align:right;"> 3548753 </td> <td style="text-align:right;"> 3522.111 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 51.356 </td> <td style="text-align:right;"> 4058385 </td> <td style="text-align:right;"> 3780.547 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 54.640 </td> <td style="text-align:right;"> 4681707 </td> <td style="text-align:right;"> 4086.114 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 56.678 </td> <td style="text-align:right;"> 5432424 </td> <td style="text-align:right;"> 4579.074 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 58.796 </td> <td style="text-align:right;"> 6298651 </td> <td style="text-align:right;"> 5280.995 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 61.310 </td> <td style="text-align:right;"> 7278866 </td> <td style="text-align:right;"> 6679.623 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 64.342 </td> <td style="text-align:right;"> 8365850 </td> <td style="text-align:right;"> 7213.791 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 67.231 </td> <td style="text-align:right;"> 9545158 </td> <td style="text-align:right;"> 6481.777 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 69.613 </td> <td style="text-align:right;"> 10748394 </td> <td style="text-align:right;"> 7103.703 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 72.312 </td> <td style="text-align:right;"> 11911819 </td> <td style="text-align:right;"> 7429.456 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 74.173 </td> <td style="text-align:right;"> 12921234 </td> <td style="text-align:right;"> 5773.045 </td> </tr> <tr> <td style="text-align:left;"> Ecuador </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 74.994 </td> <td style="text-align:right;"> 13755680 </td> <td style="text-align:right;"> 6873.262 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 50.789 </td> <td style="text-align:right;"> 30144317 </td> <td style="text-align:right;"> 3478.126 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 55.190 </td> <td style="text-align:right;"> 35015548 </td> <td style="text-align:right;"> 4131.547 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 58.299 </td> <td style="text-align:right;"> 41121485 </td> <td style="text-align:right;"> 4581.609 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 60.110 </td> <td style="text-align:right;"> 47995559 </td> <td style="text-align:right;"> 5754.734 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 62.361 </td> <td style="text-align:right;"> 55984294 </td> <td style="text-align:right;"> 6809.407 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 65.032 </td> <td style="text-align:right;"> 63759976 </td> <td style="text-align:right;"> 7674.929 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 67.405 </td> <td style="text-align:right;"> 71640904 </td> <td style="text-align:right;"> 9611.148 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 69.498 </td> <td style="text-align:right;"> 80122492 </td> <td style="text-align:right;"> 8688.156 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 71.455 </td> <td style="text-align:right;"> 88111030 </td> <td style="text-align:right;"> 9472.384 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 73.670 </td> <td style="text-align:right;"> 95895146 </td> <td style="text-align:right;"> 9767.298 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 74.902 </td> <td style="text-align:right;"> 102479927 </td> <td style="text-align:right;"> 10742.441 </td> </tr> <tr> <td style="text-align:left;"> Mexico </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 76.195 </td> <td style="text-align:right;"> 108700891 </td> <td style="text-align:right;"> 11977.575 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1952 </td> <td style="text-align:right;"> 68.440 </td> <td style="text-align:right;"> 157553000 </td> <td style="text-align:right;"> 13990.482 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1957 </td> <td style="text-align:right;"> 69.490 </td> <td style="text-align:right;"> 171984000 </td> <td style="text-align:right;"> 14847.127 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1962 </td> <td style="text-align:right;"> 70.210 </td> <td style="text-align:right;"> 186538000 </td> <td style="text-align:right;"> 16173.146 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1967 </td> <td style="text-align:right;"> 70.760 </td> <td style="text-align:right;"> 198712000 </td> <td style="text-align:right;"> 19530.366 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1972 </td> <td style="text-align:right;"> 71.340 </td> <td style="text-align:right;"> 209896000 </td> <td style="text-align:right;"> 21806.036 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1977 </td> <td style="text-align:right;"> 73.380 </td> <td style="text-align:right;"> 220239000 </td> <td style="text-align:right;"> 24072.632 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1982 </td> <td style="text-align:right;"> 74.650 </td> <td style="text-align:right;"> 232187835 </td> <td style="text-align:right;"> 25009.559 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1987 </td> <td style="text-align:right;"> 75.020 </td> <td style="text-align:right;"> 242803533 </td> <td style="text-align:right;"> 29884.350 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1992 </td> <td style="text-align:right;"> 76.090 </td> <td style="text-align:right;"> 256894189 </td> <td style="text-align:right;"> 32003.932 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 1997 </td> <td style="text-align:right;"> 76.810 </td> <td style="text-align:right;"> 272911760 </td> <td style="text-align:right;"> 35767.433 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2002 </td> <td style="text-align:right;"> 77.310 </td> <td style="text-align:right;"> 287675526 </td> <td style="text-align:right;"> 39097.100 </td> </tr> <tr> <td style="text-align:left;"> United States </td> <td style="text-align:left;"> Americas </td> <td style="text-align:right;"> 2007 </td> <td style="text-align:right;"> 78.242 </td> <td style="text-align:right;"> 301139947 </td> <td style="text-align:right;"> 42951.653 </td> </tr> </tbody> </table> </div> ] --- .left-code[ ```r ggplot(americas) + aes( x = year, y = pop ) + geom_col() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-1-1.png) ] -- Yeah, but how many people are in each country? --- .left-code[ ```r ggplot(americas) + aes( x = year, y = pop, * fill = country ) + geom_col() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-2-1.png) ] -- Bars are "stacked", can we separate? --- .left-code[ ```r ggplot(americas) + aes( x = year, y = pop, fill = country ) + geom_col( * position = "dodge" ) ``` `position = "dodge"` places objects _next to each other_ instead of overlapping ] .right-plot[ ![](index_files/figure-html/gapminder-americas-3-1.png) ] -- 🤓 What is scientific notation anyway? --- .left-code[ ```r ggplot(americas) + aes( x = year, * y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) ``` ggplot aesthetics can take expressions! ] .right-plot[ ![](index_files/figure-html/gapminder-americas-4-1.png) ] -- Might be easier to see countries individually --- .left-code[ ```r ggplot(americas) + aes( x = year, y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) + * facet_wrap(~ country) + * guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-5-1.png) ] -- Let range of y-axis vary in each plot --- .left-code[ ```r ggplot(americas) + aes( x = year, y = pop / 10^6, fill = country ) + geom_col( position = "dodge" ) + facet_wrap(~ country, * scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-6-1.png) ] -- What about life expectancy again? --- .left-code[ ```r ggplot(americas) + aes( x = year, * y = lifeExp, fill = country ) + geom_col( position = "dodge" ) + facet_wrap(~ country, scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-7-1.png) ] -- This should really be 📈 instead of 📊 --- .left-code[ ```r ggplot(americas) + aes( x = year, y = lifeExp, fill = country ) + * geom_line() + facet_wrap(~ country, scales = "free_y") + guides(fill = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-8-1.png) ] -- 📊 are **fill**ed, 📈 are **color**ed --- .left-code[ ```r ggplot(americas) + aes( x = year, y = lifeExp, * color = country ) + geom_line() + facet_wrap(~ country, scales = "free_y") + * guides(color = FALSE) ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-9-1.png) ] -- Altogether now! --- .left-code[ ```r ggplot(americas) + aes( x = year, y = lifeExp, color = country ) + geom_line() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-americas-10-1.png) ] -- Okay, changing gears again. What is range of life expectancy in Americas? --- .left-code[ ```r gapminder %>% filter( continent == "Americas" * ) %>% * ggplot() + aes( x = year, y = lifeExp ) ``` You can pipe into `ggplot()`! Just watch for `%>%` changing to `+` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-1-1.png) ] -- Boxplot for life expectancy range --- .left-code[ ```r gapminder %>% filter( continent == "Americas" ) %>% ggplot() + aes( x = year, y = lifeExp ) + * geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-2-1.png) ] -- Why not boxplots by year? --- .left-code[ ```r gapminder %>% filter( continent == "Americas" ) %>% * mutate( * year = factor(year) * ) %>% ggplot() + aes( x = year, y = lifeExp ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-3-1.png) ] -- OK, what about global life expectancy? --- .left-code[ ```r gapminder %>% # filter( # continent == "Americas" # ) %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-4-1.png) ] -- Can we have cute little boxplots for each continent? --- .left-code[ ```r gapminder %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp, * fill = continent ) + geom_boxplot() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-5-1.png) ] -- Hard to read years, let's rotate 🔄 --- .left-code[ ```r gapminder %>% mutate( year = factor(year) ) %>% ggplot() + aes( x = year, y = lifeExp, fill = continent ) + geom_boxplot() + * coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-6-1.png) ] -- Use `dplyr` to group by decade --- .left-code[ ```r gapminder %>% mutate( * decade = floor(year / 10), * decade = decade * 10, * decade = factor(decade) ) %>% ggplot() + aes( * x = decade, y = lifeExp, fill = continent ) + geom_boxplot() + coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-7-1.png) ] -- Let's hide Oceania, sorry 🇦🇺🇳🇿🇮🇩🇫🇯🇵🇬 --- .left-code[ ```r g <- gapminder %>% * filter( * continent != "Oceania" * ) %>% mutate( decade = floor(year / 10) * 10, decade = factor(decade) ) %>% ggplot() + aes( x = decade, y = lifeExp, fill = continent ) + geom_boxplot() + coord_flip() ``` ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-8-1.png) ] --- .left-code[ ```r g + theme_minimal(8) + labs( y = "Life Expectancy", x = "Decade", fill = NULL, title = "Life Expectancy by Continent and Decade", caption = "gapminder.org" ) ``` Note `x` and `y` are _original_ aesthetics, `coord_flip()` happens _after_. Remove labels by setting `= NULL`. ] .right-plot[ ![](index_files/figure-html/gapminder-all-americas-9-1.png) ] --- layout: false class: inverse, center, middle # Level up ```r Inspired by "The Best Stats You've Ever Seen" by Hans Rosling http://www.ted.com/talks/hans_rosling_shows_the_best_stats_you_ve_ever_seen ``` --- # Create the initial layout ```r g_hr <- ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-1-1.png) ] --- # Hide the guides ```r g_hr <- ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) + guides(color = FALSE, size = FALSE) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-1a-1.png) ] --- # Adjust scales of x-axis, color, and size ```r g_hr <- g_hr + scale_x_log10(breaks = c(10^3, 10^4, 10^5), labels = c("1k", "10k", "100k")) + scale_color_manual(values = gapminder::country_colors) + scale_size(range = c(0.5, 12)) ``` .plot-callout[ ![](index_files/figure-html/hans-rosling-2-1.png) ] --- # Tweak Anotations <br><br> ```r g_hr <- g_hr + labs( x = "GDP per capita", y = "Life Expectancy" ) + theme_minimal(base_family = "Fira Sans") + theme( strip.text = element_text(size = 16, face = "bold"), panel.border = element_rect(fill = NA, color = "grey40"), panel.grid.minor = element_blank() ) ``` .plot-callout.top-right[ ![](index_files/figure-html/hans-rosling-3-1.png) ] --- # The final code and plot .font70[ ```r ggplot(gapminder) + aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point() + facet_wrap(~year) + guides(color = FALSE, size = FALSE) + scale_x_log10( breaks = c(10^3, 10^4, 10^5), labels = c("1k", "10k", "100k")) + scale_color_manual(values = gapminder::country_colors) + scale_size(range = c(0.5, 12)) + labs( x = "GDP per capita", y = "Life Expectancy") + theme_minimal(14, base_family = "Fira Sans") + theme( strip.text = element_text(size = 16, face = "bold"), panel.border = element_rect(fill = NA, color = "grey40"), panel.grid.minor = element_blank()) ``` ] --- class: fullscreen background-image: url(index_files/figure-html/hans-rosling-final-1.png) background-size: cover --- # Special Bonus: Animated! .left-code[ .font70[ ```r # library(devtools) # install_github("thomasp85/gganimate") library(gganimate) # Same plot without facet_wrap() g_hra + transition_states(year, 1, 0) + ggtitle("{closest_state}") ``` ] ] -- .right-plot[ ![](images/hans-rosling-esque.gif) ] --- layout: false class: inverse, middle, center # g is for Goodbye --- layout: true # Stack Exchange is Awesome --- ![](images/stack-exchange-search.png) --- <img src="images/stack-exchange-answer.png" style="max-height: 100%"> --- layout: false # ggplot2 Extensions: exts.ggplot2.tidyverse.org/gallery <img src="images/ggplot2-exts-gallery.png" style="max-height: 100%"> --- # ggplot2 and beyond ### Learn more - **ggplot2 docs:** <http://ggplot2.tidyverse.org/> - **R4DS - Data visualization:** <http://r4ds.had.co.nz/data-visualisation.html> - **Hadley Wickham's ggplot2 book:** <https://www.amazon.com/dp/0387981403/> ### Noteworthy RStudio Add-Ins - [esquisse](https://github.com/dreamRs/esquisse): Interactively build ggplot2 plots - [ggplotThemeAssist](https://github.com/calligross/ggthemeassist): Customize your ggplot theme interactively - [ggedit](https://github.com/metrumresearchgroup/ggedit): Layer, scale, and theme editing --- # Practice and Review ### #TidyTuesday - <https://github.com/rfordatascience/tidytuesday> ### Fun Datasets - `fivethirtyeight` - `nycflights` - `ggplot2movies` ### Review - Slides and code on GitHub: <http://github.com/gadenbuie/gentle-ggplot2> --- class: inverse, center, middle # Thanks! .font150.text-white[ @grrrck <br> github.com/gadenbuie <br> Garrick Aden-Buie ]