class: center middle main-title section-title-1 # Visualize data<br>with ggplot2 .class-info[ <figure> <img src="img/04/ggplot-logo-min.png" alt="ggplot" title="ggplot" width="15%"> </figure> ] --- class: title title-1 # Cars and displacement .box-inv-1.medium[What is the relationship between a car's<br>engine weight (displacement) and its mileage (miles per gallon)?] --- class: title title-1 section-title-inv-1 # Your turn #1 .box-1[Run this code in your Quarto file to make a graph] .box-1[Pay attention to spelling, capitalization, and parentheses!] ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ```
−
+
01
:
00
--- ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-3-1.png" width="60%" style="display: block; margin: auto;" /> --- class: bg-full bg-y-75 background-image: url("img/04/napoleon-retreat.jpg") ??? Source: [Wikipedia](https://en.wikipedia.org/wiki/File:National_Museum_in_Poznan_-_Przej%C5%9Bcie_przez_Berezyn%C4%99.JPG) --- layout: true class: title title-1 --- # Long distance! .center[ <figure> <img src="img/04/napoleon-google-maps-min.png" alt="Moscow to Vilnius" title="Moscow to Vilnius" width="80%"> <figcaption>Moscow to Vilnius</figcaption> </figure> ] --- # Very cold! <img src="04_visualize-data_files/figure-html/minard-temps-1.png" width="864" style="display: block; margin: auto;" /> --- # Lots of people died! <img src="04_visualize-data_files/figure-html/minard-deaths-1.png" width="468" style="display: block; margin: auto;" /> --- layout: false class: bg-full background-image: url("img/04/minard-min.png") ??? Source: [Wikimedia Commons](https://upload.wikimedia.org/wikipedia/commons/2/29/Minard-min.png) --- layout: true class: title title-1 --- # Mapping data to aesthetics .pull-left.center[ <figure> <img src="img/04/gg-book.jpg" alt="Grammar of Graphics book" title="Grammar of Graphics book" width="55%"> </figure> ] .pull-right[ .box-inv-1.medium[Aesthetic] .box-1[Visual property of a graph] .box-1.sp-after[Position, shape, color, etc.] .box-inv-1.medium[Data] .box-1[A column in a dataset] ] --- # Mapping data to aesthetics <table> <tr> <th class="cell-left">Data</th> <th class="cell-left">Aesthetic</th> <th class="cell-left">Graphic/Geometry</th> </tr> <tr> <td class="cell-left">Longitude</td> <td class="cell-left">Position (x-axis) </td> <td class="cell-left">Point</td> </tr> <tr> <td class="cell-left">Latitude</td> <td class="cell-left">Position (y-axis)</td> <td class="cell-left">Point</td> </tr> <tr> <td class="cell-left">Army size</td> <td class="cell-left">Size</td> <td class="cell-left">Path</td> </tr> <tr> <td class="cell-left">Army direction </td> <td class="cell-left">Color</td> <td class="cell-left">Path</td> </tr> <tr> <td class="cell-left">Date</td> <td class="cell-left">Position (x-axis)</td> <td class="cell-left">Line + text</td> </tr> <tr> <td class="cell-left">Temperature</td> <td class="cell-left">Position (y-axis)</td> <td class="cell-left">Line + text</td> </tr> </table> --- # Mapping data to aesthetics <table> <tr> <th class="cell-left">Data</th> <th class="cell-left"><code class="remark-inline-code">aes()</code></th> <th class="cell-left"><code class="remark-inline-code">geom</code></th> </tr> <tr> <td class="cell-left">Longitude</td> <td class="cell-left"><code class="remark-inline-code">x</code></td> <td class="cell-left"><code class="remark-inline-code">geom_point()</code></td> </tr> <tr> <td class="cell-left">Latitude</td> <td class="cell-left"><code class="remark-inline-code">y</code></td> <td class="cell-left"><code class="remark-inline-code">geom_point()</code></td> </tr> <tr> <td class="cell-left">Army size</td> <td class="cell-left"><code class="remark-inline-code">size</code></td> <td class="cell-left"><code class="remark-inline-code">geom_path()</code></td> </tr> <tr> <td class="cell-left">Army direction </td> <td class="cell-left"><code class="remark-inline-code">color</code> </td> <td class="cell-left"><code class="remark-inline-code">geom_path()</code></td> </tr> <tr> <td class="cell-left">Date</td> <td class="cell-left"><code class="remark-inline-code">x</code></td> <td class="cell-left"><code class="remark-inline-code">geom_line() + geom_text()</code></td> </tr> <tr> <td class="cell-left">Temperature</td> <td class="cell-left"><code class="remark-inline-code">y</code></td> <td class="cell-left"><code class="remark-inline-code">geom_line() + geom_text()</code></td> </tr> </table> --- # `ggplot()` template <code class ='r hljs remark-code'>ggplot(data = <b><span style="background-color:#CBB5FF">DATA</span></b>) +<br> <b><span style="background-color:#FFDFD1">GEOM_FUNCTION</span></b>(mapping = aes(<b><span style="background-color:#FFD0CF">AESTHETIC MAPPINGS</span></b>))</code> -- <code class ='r hljs remark-code'>ggplot(data = <b><span style="background-color:#CBB5FF">troops</span></b>) +<br> <b><span style="background-color:#FFDFD1">geom_path</span></b>(mapping = aes(<b><span style="background-color:#FFD0CF">x = longitude</span></b>,<br> <b><span style="background-color:#FFD0CF">y = latitude</span></b>,<br> <b><span style="background-color:#FFD0CF">color = direction</span></b>,<br> <b><span style="background-color:#FFD0CF">size = survivors</span></b>))</code> --- layout: false .box-1[This is a dataset named `troops`:] .small[ <table> <thead> <tr> <th style="text-align:left;"> longitude </th> <th style="text-align:left;"> latitude </th> <th style="text-align:left;"> direction </th> <th style="text-align:left;"> survivors </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 24 </td> <td style="text-align:left;"> 54.9 </td> <td style="text-align:left;"> A </td> <td style="text-align:left;"> 340000 </td> </tr> <tr> <td style="text-align:left;"> 24.5 </td> <td style="text-align:left;"> 55 </td> <td style="text-align:left;"> A </td> <td style="text-align:left;"> 340000 </td> </tr> <tr> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> <td style="text-align:left;"> … </td> </tr> </tbody> </table> ] -- <code class ='r hljs remark-code'>ggplot(data = <b><span style="background-color:#CBB5FF">troops</span></b>) +<br> <b><span style="background-color:#FFDFD1">geom_path</span></b>(mapping = aes(<b><span style="background-color:#FFD0CF">x = longitude</span></b>,<br> <b><span style="background-color:#FFD0CF">y = latitude</span></b>,<br> <b><span style="background-color:#FFD0CF">color = direction</span></b>,<br> <b><span style="background-color:#FFD0CF">size = survivors</span></b>))</code> --- <img src="04_visualize-data_files/figure-html/show-basic-minard-1.png" width="100%" style="display: block; margin: auto;" /> --- ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-4-1.png" width="60%" style="display: block; margin: auto;" /> --- layout: true class: title title-1 --- # Heavy cars with better mileage? <img src="04_visualize-data_files/figure-html/unnamed-chunk-5-1.png" width="60%" style="display: block; margin: auto;" /> --- # Aesthetics .pull-left-3[ .box-inv-1.small[`color` (discrete)] <img src="04_visualize-data_files/figure-html/aes-color-discrete-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`color` (continuous)] <img src="04_visualize-data_files/figure-html/aes-color-continuous-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-middle-3[ .box-inv-1.small[`size`] <img src="04_visualize-data_files/figure-html/aes-size-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`fill`] <img src="04_visualize-data_files/figure-html/aes-fill-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right-3[ .box-inv-1.small[`shape`] <img src="04_visualize-data_files/figure-html/aes-shape-1.png" width="100%" style="display: block; margin: auto;" /> .box-inv-1.small[`alpha`] <img src="04_visualize-data_files/figure-html/aes-alpha-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Mapping columns to aesthetics .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, size = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, shape = class)) ggplot(mpg) + geom_point(aes(x = displ, y = hwy, alpha = class)) ``` ] --- layout: false ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class)) ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-7-1.png" width="60%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # Your turn #2 .box-1[Add color, size, alpha, and shape aesthetics to your graph.] .box-1[Experiment!] .box-1[Do different things happen when you map aesthetics to discrete and continuous variables?] .box-1[What happens when you use more than one aesthetic?] --- class: title title-1 # How would you make this plot? <img src="04_visualize-data_files/figure-html/unnamed-chunk-8-1.png" width="70%" style="display: block; margin: auto;" /> --- .left-code[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = class)) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/color-aes-example-1.png) ] --- .left-code[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue") ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/color-set-example-1.png) ] --- .pull-left[ .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy, color = "blue")) ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-9-1.png" width="100%" style="display: block; margin: auto;" /> ] ] .pull-right[ .small[ ```r ggplot(mpg) + geom_point(aes(x = displ, y = hwy), color = "blue") ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-10-1.png" width="100%" style="display: block; margin: auto;" /> ] ] --- layout: true class: title title-1 --- # What's the same? What's different? .pull-left[ <img src="04_visualize-data_files/figure-html/unnamed-chunk-11-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="04_visualize-data_files/figure-html/unnamed-chunk-12-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Geoms <code class ='r hljs remark-code'>ggplot(data = <b><span style="background-color:#CBB5FF">DATA</span></b>) +<br> <b><span style="background-color:#FFDFD1">GEOM_FUNCTION</span></b>(mapping = aes(<b><span style="background-color:#FFD0CF">AESTHETIC MAPPINGS</span></b>))</code> --- # Possible geoms <table> <tr> <th class="cell-left"></th> <th class="cell-left">Example geom</th> <th class="cell-left">What it makes</th> </tr> <tr> <td class="cell-left"><img src="img/04/geom_bar-min.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_col()</code></td> <td class="cell-left">Bar charts</td> </tr> <tr> <td class="cell-left"><img src="img/04/geom_text-min.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_text()</code></td> <td class="cell-left">Text</td> </tr> <tr> <td class="cell-left"><img src="img/04/geom_point-min.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_point()</code></td> <td class="cell-left">Points</td> </tr> <tr> <td class="cell-left"><img src="img/04/geom_boxplot-min.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_boxplot()</code> </td> <td class="cell-left">Boxplots</td> </tr> <tr> <td class="cell-left"><img src="img/04/geom_sf-min.png"></td> <td class="cell-left"><code class="remark-inline-code">geom_sf()</code></td> <td class="cell-left">Maps</td> </tr> </table> --- # Possible geoms .box-inv-1[There are dozens of possible geoms!] .box-1[See [the **ggplot2** documentation](https://ggplot2.tidyverse.org/reference/index.html#section-layer-geoms) for<br>complete examples of all the different geom layers] .box-1[Also see the ggplot cheatsheet] --- layout: false class: title title-1 section-title-inv-1 # Your turn #3 .box-1[Replace this scatterplot with boxplots. Use the cheatsheet.] .pull-left[ <img src="04_visualize-data_files/figure-html/unnamed-chunk-13-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="04_visualize-data_files/figure-html/unnamed-chunk-14-1.png" width="100%" style="display: block; margin: auto;" /> ]
−
+
03
:
00
--- class: title title-1 section-title-inv-1 # Your turn #4 .box-1[Make a histogram of `hwy`. Use the cheetsheet.<br>Hint: don't supply a `y` variable.] <img src="04_visualize-data_files/figure-html/unnamed-chunk-16-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # Your turn #5 .box-1[Make this density plot of `hwy` colored by `class`.<br>Use the cheatsheet. Hint: don't supply a `y` variable.] <img src="04_visualize-data_files/figure-html/unnamed-chunk-17-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 # Complex graphs! <img src="04_visualize-data_files/figure-html/unnamed-chunk-18-1.png" width="70%" style="display: block; margin: auto;" /> --- class: title title-1 section-title-inv-1 # Your turn #6 .box-1[Predict what this code will do. Then run it.] ```r ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + geom_smooth(mapping = aes(x = displ, y = hwy)) ```
−
+
02
:
00
--- class: title title-1 # Global vs. local .box-inv-1[Any aesthetics in `ggplot()` will show up in all `geom_` layers] .small[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth() ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-21-1.png" width="60%" style="display: block; margin: auto;" /> ] --- class: title title-1 # Global vs. local .box-inv-1[Any aesthetics in `geom_` layers only apply to that layer] .small[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = drv)) + geom_smooth() ``` <img src="04_visualize-data_files/figure-html/unnamed-chunk-22-1.png" width="60%" style="display: block; margin: auto;" /> ] --- layout: true class: title title-1 --- # So much more! .pull-left[ .box-inv-1[There are many other layers we can use to make and enhance graphs!] .box-inv-1[We sequentially add layers onto the foundational `ggplot()` plot to create complex figures] ] .pull-right[ ![](img/04/ggplot-layers@4x-min.png) ] --- # Putting it all together .box-inv-1.medium[We can build a plot sequentially<br>to see how each grammatical layer<br>changes the appearance] --- layout: false .left-code[ .box-1[Start with data and aesthetics] ```r *ggplot(data = mpg, * mapping = aes(x = displ, * y = hwy, * color = drv)) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-1-1.png) ] --- .left-code[ .box-1[Add a point geom] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + * geom_point() ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-2-1.png) ] --- .left-code[ .box-1[Add a smooth geom] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + * geom_smooth() ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-3-1.png) ] --- .left-code[ .box-1[Make it straight] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + * geom_smooth(method = "lm") ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-4-1.png) ] --- .left-code[ .box-1[Use a viridis color scale] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + * scale_color_viridis_d() ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-5-1.png) ] --- .left-code[ .box-1[Facet by drive] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + * facet_wrap(vars(drv), ncol = 1) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-6-1.png) ] --- .left-code[ .box-1[Add labels] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + * labs(x = "Displacement", y = "Highway MPG", * color = "Drive", * title = "Heavier cars get lower mileage", * subtitle = "Displacement indicates weight(?)", * caption = "I know nothing about cars") ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-7-1.png) ] --- .left-code[ .box-1[Add a theme] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + * theme_bw() ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-8-1.png) ] --- .left-code[ .box-1[Modify the theme] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + * theme(legend.position = "bottom", * plot.title = element_text(face = "bold")) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-9-1.png) ] --- .left-code[ .box-1[Finished!] ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(method = "lm") + scale_color_viridis_d() + facet_wrap(vars(drv), ncol = 1) + labs(x = "Displacement", y = "Highway MPG", color = "Drive", title = "Heavier cars get lower mileage", subtitle = "Displacement indicates weight(?)", caption = "I know nothing about cars") + theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold")) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/mpg-layers-finished-1.png) ] --- layout: true class: title title-1 --- # So many possibilities! .pull-left[ ![](img/04/ggplot-layers@4x-min.png) ] .pull-right[ .box-inv-1[These were just a few examples of layers!] .box-1[See [the **ggplot2** documentation](https://ggplot2.tidyverse.org/reference/index.html) for<br>complete examples of everything you can do] ] --- # A true grammar .pull-left[ .box-inv-1[With the grammar of graphics, we don't talk about specific chart *types*] .box-1.small[Hunt through Excel menus for a stacked bar chart and manually reshape your data to work with it] ] .pull-right[ .center[ <figure> <img src="img/04/excel-chart-types-min.png" alt="Excel chart types" title="Excel chart types" width="78%"> </figure> ] ] --- # A true grammar .pull-left[ .box-inv-1[With the grammar of graphics, we *do* talk about specific chart *elements*] .box-1.small[Map a column to the x-axis, fill by a different variable, and `geom_col()` to get stacked bars] .box-1.small[Geoms can be interchangable<br>(e.g. switch `geom_violin()` to `geom_boxplot()`)] ] .pull-right[ .center[ <figure> <img src="img/04/ggplot-layers@4x-min.png" alt="Grammar of graphics layers" title="Grammar of graphics layers" width="100%"> </figure> ] ] --- # Describing graphs with the grammar .left-code[ .box-1.small[Map wealth to the x-axis, health to the y-axis, add points, color by continent, size by population, scale the y-axis with a log, and facet by year] ```r ggplot(filter(gapminder, year %in% c(2002, 2007)), aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + scale_x_log10() + facet_wrap(vars(year), ncol = 1) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/describe-1-1.png) ] --- # Describing graphs with the grammar .left-code[ .box-1.small[Map health to the x-axis, add a histogram with bins for every 5 years, fill and facet by continent] ```r ggplot(gapminder_2007, aes(x = lifeExp, fill = continent)) + geom_histogram(binwidth = 5, color = "white") + guides(fill = FALSE) + # Turn off legend facet_wrap(vars(continent)) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/describe-2-1.png) ] --- # Describing graphs with the grammar .left-code[ .box-1.small[Map continent to the x-axis, health to the y-axis, add violin plots and semi-transparent boxplots, fill by continent] ```r ggplot(gapminder, aes(x = continent, y = lifeExp, fill = continent)) + geom_violin() + geom_boxplot(alpha = 0.5) + guides(fill = FALSE) # Turn off legend ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/describe-3-1.png) ] --- # Scales .box-inv-1[Scales change the properties of the variable mapping] <table> <tr> <th class="cell-left">Example layer</th> <th class="cell-left">What it does</th> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">scale_x_continuous()</code></td> <td class="cell-left">Make the x-axis continuous</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">scale_x_continuous(breaks = 1:5) </code></td> <td class="cell-left">Manually specify axis ticks</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">scale_x_log10()</code></td> <td class="cell-left">Log the x-axis</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">scale_color_gradient()</code></td> <td class="cell-left">Use a gradient</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">scale_fill_viridis_d()</code></td> <td class="cell-left">Fill with discrete viridis colors</td> </tr> </table> --- # Scales .pull-left[ .box-inv-1.small[`scale_x_log10()`] <img src="04_visualize-data_files/figure-html/scale-example-1-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ .box-inv-1.small[`scale_color_viridis_d()`] <img src="04_visualize-data_files/figure-html/scale-example-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- layout: false class: title title-1 section-title-inv-1 # Your turn #7 .box-1[Make this density plot of `displ` filled by `drv`.<br>Use the viridis fill scale.] .box-1.small[For bonus fun, try a different viridis option like `plasma` or `inferno`.] <img src="04_visualize-data_files/figure-html/unnamed-chunk-23-1.png" width="70%" style="display: block; margin: auto;" />
−
+
03
:
00
--- class: section-title-inv-1 .left-code[ ```r ggplot(mpg, aes(x = displ, fill = drv)) + geom_density(alpha = 0.75) + scale_fill_viridis_d(option = "plasma") ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/your-turn-7-1.png) ] --- layout: true class: title title-1 --- # Facets .box-inv-1[Facets show subplots for different subsets of data] <table> <tr> <th class="cell-left">Example layer</th> <th class="cell-left">What it does</th> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">facet_wrap(vars(continent))</code></td> <td class="cell-left">Plot for each continent</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">facet_wrap(vars(continent, year))</code> </td> <td class="cell-left">Plot for each continent/year</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">facet_wrap(..., ncol = 1)</code></td> <td class="cell-left">Put all facets in one column</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">facet_wrap(..., nrow = 1)</code></td> <td class="cell-left">Put all facets in one row</td> </tr> </table> --- # Facets .pull-left[ .box-inv-1.small[`facet_wrap(vars(continent))`] <img src="04_visualize-data_files/figure-html/facet-example-1-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ .box-inv-1.small[`facet_wrap(vars(continent, year))`] <img src="04_visualize-data_files/figure-html/facet-example-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- layout: false class: title title-1 section-title-inv-1 # Your turn #8 .box-1[Facet this scatterplot by `drv`. Are there any interesting trends?] <img src="04_visualize-data_files/figure-html/unnamed-chunk-25-1.png" width="70%" style="display: block; margin: auto;" />
−
+
03
:
00
--- class: section-title-inv-1 .left-code[ ```r ggplot(mpg, aes(x = displ, y = hwy, color = class)) + geom_point() + facet_wrap(vars(drv)) ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/your-turn-8-1.png) ] --- layout: true class: title title-1 --- # Coordinates .box-inv-1[Change the coordinate system] <table> <tr> <th class="cell-left">Example layer</th> <th class="cell-left">What it does</th> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">coord_cartesian()</code></td> <td class="cell-left">Standard x-y coordinate system</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">coord_cartesian(ylim = c(1, 10))</code> </td> <td class="cell-left">Zoom in where y is 1–10</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">coord_flip()</code></td> <td class="cell-left">Switch x and y</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">coord_polar()</code></td> <td class="cell-left">Use circular polar system</td> </tr> </table> --- # Coordinates .pull-left[ .box-inv-1.small[`coord_cartesian(ylim = c(70, 80), xlim = c(10000, 30000))`] <img src="04_visualize-data_files/figure-html/coord-example-1-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ .box-inv-1.small[`coord_flip()`] <img src="04_visualize-data_files/figure-html/coord-example-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Labels .box-inv-1[Add labels to the plot with a single `labs()` layer] <table> <tr> <th class="cell-left">Example layer</th> <th class="cell-left">What it does</th> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">labs(title = "Neat title")</code></td> <td class="cell-left">Title</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">labs(caption = "Something")</td> <td class="cell-left">Caption</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">labs(y = "Something")</td> <td class="cell-left">y-axis</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">labs(size = "Population")</code></td> <td class="cell-left">Title of size legend</td> </tr> </table> --- # Labels .left-code[ ```r ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point() + scale_x_log10() + labs(title = "Health and wealth grow together", subtitle = "Data from 2007", x = "Wealth (GDP per capita)", y = "Health (life expectancy)", color = "Continent", size = "Population", caption = "Source: The Gapminder Project") ``` ] .right-plot[ ![](04_visualize-data_files/figure-html/labels-example-1.png) ] --- # Theme .box-inv-1[Change the appearance of anything in the plot] .box-1[There are many built-in themes] <table> <tr> <th class="cell-left">Example layer</th> <th class="cell-left">What it does</th> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">theme_grey()</code></td> <td class="cell-left">Default grey background</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">theme_bw()</td> <td class="cell-left">Black and white</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">theme_dark()</td> <td class="cell-left">Dark</td> </tr> <tr> <td class="cell-left"><code class="remark-inline-code">theme_minimal()</code></td> <td class="cell-left">Minimal</td> </tr> </table> --- # Theme .pull-left[ .box-inv-1.small[`theme_dark()`] <img src="04_visualize-data_files/figure-html/theme-example-1-1.png" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ .box-inv-1.small[`theme_minimal()`] <img src="04_visualize-data_files/figure-html/theme-example-2-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Theme .box-inv-1[There are collections of pre-built themes online,<br>like [the **ggthemes** package](https://jrnold.github.io/ggthemes/)] .center[ <figure> <img src="img/04/ggthemes-min.png" alt="ggthemes" title="ggthemes" width="45%"> </figure> ] --- # Theme .box-inv-1[Organizations often make their own custom themes, [like the BBC](https://bbc.github.io/rcookbook/)] .center[ <figure> <img src="img/04/bbc-cookbook-min.png" alt="BBC ggplot theme" title="BBC ggplot theme" width="80%"> </figure> ] --- # Theme options .box-inv-1[Make theme adjustments with `theme()`] .box-1[There are a billion options here!] ```r theme_bw() + theme(legend.position = "bottom", plot.title = element_text(face = "bold"), panel.grid = element_blank(), axis.title.y = element_text(face = "italic")) ``` --- # Saving graphs .box-inv-1.medium[Use `ggsave()` to save a plot to your computer] .box-1[Store plot as an object, feed it to `ggsave()`] ```r my_plot <- ggplot(...) ggsave("plot_name.pdf", my_plot, width = 5, height = 3.5) ggsave("plot_name.png", my_plot, width = 5, height = 3.5) ``` --- class: title title-1 # Next up .box-inv-1.medium[Transforming and<br>manipulating data with dplyr]