<!-- https://evamaerey.github.io/flipbooks/flipbook_recipes#49 --> class: title-slide, center, bottom # Introduction to ggplot2 ### *The 100 Days of Data.trek Challenge* ![](https://ivado.ca/wp-content/themes/ivadooo/assets/svg/siteLogo.svg) February 4, 2021 (updated: 2021-08-20) ## Philippe Massicotte --- <br> <center><img src="img/myname.png" alt="drawing" width="400"/></center> <p align="left"> <b>Research assistant at Takuvik (Laval University)</b><br> - Remote sensing, modelling, data science, data visualization<br> <br>
<small>https://github.com/PMassicotte</small> <br>
<small>philippe.massicotte@takuvik.ulaval.ca</small> <br>
<small>@philmassicotte</small> <br>
<small>www.pmassicotte.com</small> </p> --- class: inverse-two, center, middle # Ateliers gestion et archivage des données <br> <center> <figure> <img src="img/qo.png" width="400"> </figure> </center> <br> ### 18, 22 et 25 mars 2021 --- # Outline - What is data visualization? - `ggplot2` basic plots - Histograms and bar plots - Points and lines plots - Boxplots - `ggplot2` aesthetics and appearance - Color, size - Axes and titles - Faceting (small multiples) - Overview of the `theme()` function - Combining plots with `patchwork` - Exporting `ggplot2` graphics --- # Data visualization <center> <figure> <img src="https://blog.rstudio.com/2019/11/18/artist-in-residence/horst-eco-r4ds.png" width="800"> <figcaption>Artwork by <a href="https://twitter.com/allison_horst?s=20">@allison_horst</a></figcaption> </figure> </center> --- # Data visualization -- - Important aspect of data sciences - <span class="gradient-underline">Communicate information clearly and efficiently to the community.</span> -- - Powerful tool to discovers patterns in the data. - It makes complex data more accessible: <span class="gradient-underline">reveal the data</span>. -- - Bad graphics can be a reason for paper rejection. - <span class="gradient-underline">Readers should rapidly understand the message you are trying to convey.</span> -- - A picture is worth a thousand words. - <span class="gradient-underline">Always, always, always plot your data!</span> - When possible, replace tables with more compelling figures. --- # The Anscombe data -- .gradient-underline[These four datasets have the same statistical properties (*mean*, *variance*, *correlation*).] However, they look quite different. -- <img src="index_files/figure-html/unnamed-chunk-1-1.svg" style="display: block; margin: auto;" /> --- # Different types of visualizations There are many types of visualization to chose from to present data. The decision depends on the data itself and how you want to present it to your audience. <center> <img src="https://scx2.b-cdn.net/gfx/news/hires/2013/whatmakesada.png" alt="drawing" height="400"/> <figcaption> <a href="https://phys.org/news/2013-10-visualization.html">What makes a data visualization memorable?</a> </figcaption> </center> --- class: inverse, center, middle <center> <figure> <img src="https://github.com/allisonhorst/stats-illustrations/blob/master/rstats-artwork/ggplot2_masterpiece.png?raw=true" width="700"> <figcaption>Artwork by <a href="https://twitter.com/allison_horst?s=20">@allison_horst</a></figcaption> </figure> </center> --- # ggplot2 `ggplot2` is a system for declaratively creating graphics, based on [The Grammar of Graphics](https://amzn.to/2ef1eWp). .left-column[ <center> <figure> <img src="https://raw.githubusercontent.com/tidyverse/ggplot2/master/man/figures/logo.png" width="300"> </figure> </center> ] .right-column[ `ggplot2` is not part of base R, so it needs to be installed. ```r install.packages("ggplot2") ``` After the installation, you will have to load it. ```r library(ggplot2) ``` ] --- # ggplot2 .column-left[ `ggplot2` is a very powerful tool. The learning curve can be difficult, but .gradient-underline[the time investment will eventually pay off.] ] .column-right[ <center> <figure> <img src="https://github.com/allisonhorst/stats-illustrations/blob/master/rstats-artwork/r_first_then.png?raw=true" height="500"> <figcaption> Artwork by <a href="https://twitter.com/allison_horst?s=20">@allison_horst</a> </figcaption> </figure> </center> ] --- # TidyTuesday TidyTuesday is a weekly challenge where people use (mostly) `ggplot2` to explore a new dataset. > A weekly data project aimed at the R ecosystem. As this project was borne out of the R4DS Online Learning Community and the R for Data Science textbook, an emphasis was placed on understanding how to summarize and arrange data to make meaningful charts with `ggplot2`, `tidyr`, `dplyr`, and other tools in the `tidyverse` ecosystem. <center> <img src="https://github.com/rfordatascience/tidytuesday/blob/master/static/tt_logo.png?raw=true" alt="drawing" width="450"/> <figcaption> <a href="https://twitter.com/search?q=%23TidyTuesday&src=hashtag_click">#TidyTuesday on <i class="fab fa-twitter"></i></a> </figcaption> </center> --- # TidyTuesday As already said, `ggplot2` can be intimidating at first. However, with some practice, you will be able to make stunning graphics. <div class="row"> <div class="column"> <img src="https://christianburkhart.de/static/30599239f2edece767ae2434a6665c60/47218/tidytuesday_birds.png" alt="Snow" style="width:80%"> <figcaption> Graphic by <a href=https://twitter.com/ChBurkhart">@ChBurkhart</a> </figcaption> </div> <div class="column"> <img src="https://github.com/jkaupp/tidytuesdays/blob/437b47ad14bd3d6d66dca41e0a4a84c9d96716fb/2020/week44/tw44_plot.png?raw=true" alt="Forest" style="width:80%"> <figcaption> Graphic by <a href=https://twitter.com/jakekaupp">@jakekaupp</a> </figcaption> </div> <div class="column"> <img src="https://github.com/gkaramanis/aRtist/blob/main/genuary/2021/2021-3/2021-3.png?raw=true" alt="Forest" style="width:80%"> <figcaption> Graphic by <a href=https://twitter.com/geokaramanis?lang=en">@geokaramanis</a> </figcaption> </div> </div> <span class="gradient-underline">Most people participating in the TidyTuesday challenges share their code on Github. It is a great way to learn more advanced techniques!</span> --- class: inverse, center, middle # Let's start with ggplot2! --- # The data For the following examples, we are going to use the data from the `mpg` dataset. This dataset contains a subset of the [fuel economy data](http://fueleconomy.gov) from the EPA. <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":["manufacturer"],"name":[1],"type":["chr"],"align":["left"]},{"label":["model"],"name":[2],"type":["chr"],"align":["left"]},{"label":["displ"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["year"],"name":[4],"type":["int"],"align":["right"]},{"label":["cyl"],"name":[5],"type":["int"],"align":["right"]},{"label":["trans"],"name":[6],"type":["chr"],"align":["left"]},{"label":["drv"],"name":[7],"type":["chr"],"align":["left"]},{"label":["cty"],"name":[8],"type":["int"],"align":["right"]},{"label":["hwy"],"name":[9],"type":["int"],"align":["right"]},{"label":["fl"],"name":[10],"type":["chr"],"align":["left"]},{"label":["class"],"name":[11],"type":["chr"],"align":["left"]}],"data":[{"1":"audi","2":"a4","3":"1.8","4":"1999","5":"4","6":"auto(l5)","7":"f","8":"18","9":"29","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"1.8","4":"1999","5":"4","6":"manual(m5)","7":"f","8":"21","9":"29","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"2.0","4":"2008","5":"4","6":"manual(m6)","7":"f","8":"20","9":"31","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"2.0","4":"2008","5":"4","6":"auto(av)","7":"f","8":"21","9":"30","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"2.8","4":"1999","5":"6","6":"auto(l5)","7":"f","8":"16","9":"26","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"2.8","4":"1999","5":"6","6":"manual(m5)","7":"f","8":"18","9":"26","10":"p","11":"compact"},{"1":"audi","2":"a4","3":"3.1","4":"2008","5":"6","6":"auto(av)","7":"f","8":"18","9":"27","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"1.8","4":"1999","5":"4","6":"manual(m5)","7":"4","8":"18","9":"26","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"1.8","4":"1999","5":"4","6":"auto(l5)","7":"4","8":"16","9":"25","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"2.0","4":"2008","5":"4","6":"manual(m6)","7":"4","8":"20","9":"28","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"2.0","4":"2008","5":"4","6":"auto(s6)","7":"4","8":"19","9":"27","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"2.8","4":"1999","5":"6","6":"auto(l5)","7":"4","8":"15","9":"25","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"2.8","4":"1999","5":"6","6":"manual(m5)","7":"4","8":"17","9":"25","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"3.1","4":"2008","5":"6","6":"auto(s6)","7":"4","8":"17","9":"25","10":"p","11":"compact"},{"1":"audi","2":"a4 quattro","3":"3.1","4":"2008","5":"6","6":"manual(m6)","7":"4","8":"15","9":"25","10":"p","11":"compact"},{"1":"audi","2":"a6 quattro","3":"2.8","4":"1999","5":"6","6":"auto(l5)","7":"4","8":"15","9":"24","10":"p","11":"midsize"},{"1":"audi","2":"a6 quattro","3":"3.1","4":"2008","5":"6","6":"auto(s6)","7":"4","8":"17","9":"25","10":"p","11":"midsize"},{"1":"audi","2":"a6 quattro","3":"4.2","4":"2008","5":"8","6":"auto(s6)","7":"4","8":"16","9":"23","10":"p","11":"midsize"},{"1":"chevrolet","2":"c1500 suburban 2wd","3":"5.3","4":"2008","5":"8","6":"auto(l4)","7":"r","8":"14","9":"20","10":"r","11":"suv"},{"1":"chevrolet","2":"c1500 suburban 2wd","3":"5.3","4":"2008","5":"8","6":"auto(l4)","7":"r","8":"11","9":"15","10":"e","11":"suv"},{"1":"chevrolet","2":"c1500 suburban 2wd","3":"5.3","4":"2008","5":"8","6":"auto(l4)","7":"r","8":"14","9":"20","10":"r","11":"suv"},{"1":"chevrolet","2":"c1500 suburban 2wd","3":"5.7","4":"1999","5":"8","6":"auto(l4)","7":"r","8":"13","9":"17","10":"r","11":"suv"},{"1":"chevrolet","2":"c1500 suburban 2wd","3":"6.0","4":"2008","5":"8","6":"auto(l4)","7":"r","8":"12","9":"17","10":"r","11":"suv"},{"1":"chevrolet","2":"corvette","3":"5.7","4":"1999","5":"8","6":"manual(m6)","7":"r","8":"16","9":"26","10":"p","11":"2seater"},{"1":"chevrolet","2":"corvette","3":"5.7","4":"1999","5":"8","6":"auto(l4)","7":"r","8":"15","9":"23","10":"p","11":"2seater"},{"1":"chevrolet","2":"corvette","3":"6.2","4":"2008","5":"8","6":"manual(m6)","7":"r","8":"16","9":"26","10":"p","11":"2seater"},{"1":"chevrolet","2":"corvette","3":"6.2","4":"2008","5":"8","6":"auto(s6)","7":"r","8":"15","9":"25","10":"p","11":"2seater"},{"1":"chevrolet","2":"corvette","3":"7.0","4":"2008","5":"8","6":"manual(m6)","7":"r","8":"15","9":"24","10":"p","11":"2seater"},{"1":"chevrolet","2":"k1500 tahoe 4wd","3":"5.3","4":"2008","5":"8","6":"auto(l4)","7":"4","8":"14","9":"19","10":"r","11":"suv"},{"1":"chevrolet","2":"k1500 tahoe 4wd","3":"5.3","4":"2008","5":"8","6":"auto(l4)","7":"4","8":"11","9":"14","10":"e","11":"suv"},{"1":"chevrolet","2":"k1500 tahoe 4wd","3":"5.7","4":"1999","5":"8","6":"auto(l4)","7":"4","8":"11","9":"15","10":"r","11":"suv"},{"1":"chevrolet","2":"k1500 tahoe 4wd","3":"6.5","4":"1999","5":"8","6":"auto(l4)","7":"4","8":"14","9":"17","10":"d","11":"suv"},{"1":"chevrolet","2":"malibu","3":"2.4","4":"1999","5":"4","6":"auto(l4)","7":"f","8":"19","9":"27","10":"r","11":"midsize"},{"1":"chevrolet","2":"malibu","3":"2.4","4":"2008","5":"4","6":"auto(l4)","7":"f","8":"22","9":"30","10":"r","11":"midsize"},{"1":"chevrolet","2":"malibu","3":"3.1","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"18","9":"26","10":"r","11":"midsize"},{"1":"chevrolet","2":"malibu","3":"3.5","4":"2008","5":"6","6":"auto(l4)","7":"f","8":"18","9":"29","10":"r","11":"midsize"},{"1":"chevrolet","2":"malibu","3":"3.6","4":"2008","5":"6","6":"auto(s6)","7":"f","8":"17","9":"26","10":"r","11":"midsize"},{"1":"dodge","2":"caravan 2wd","3":"2.4","4":"1999","5":"4","6":"auto(l3)","7":"f","8":"18","9":"24","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.0","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"17","9":"24","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.3","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"16","9":"22","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.3","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"16","9":"22","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.3","4":"2008","5":"6","6":"auto(l4)","7":"f","8":"17","9":"24","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.3","4":"2008","5":"6","6":"auto(l4)","7":"f","8":"17","9":"24","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.3","4":"2008","5":"6","6":"auto(l4)","7":"f","8":"11","9":"17","10":"e","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.8","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"15","9":"22","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.8","4":"1999","5":"6","6":"auto(l4)","7":"f","8":"15","9":"21","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"3.8","4":"2008","5":"6","6":"auto(l6)","7":"f","8":"16","9":"23","10":"r","11":"minivan"},{"1":"dodge","2":"caravan 2wd","3":"4.0","4":"2008","5":"6","6":"auto(l6)","7":"f","8":"16","9":"23","10":"r","11":"minivan"},{"1":"dodge","2":"dakota pickup 4wd","3":"3.7","4":"2008","5":"6","6":"manual(m6)","7":"4","8":"15","9":"19","10":"r","11":"pickup"},{"1":"dodge","2":"dakota pickup 4wd","3":"3.7","4":"2008","5":"6","6":"auto(l4)","7":"4","8":"14","9":"18","10":"r","11":"pickup"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[7],"max":[7]},"pages":{}}} </script> </div> --- # The data It should be noted that `ggplot2` package operates on R `data.frame`. ```r str(mpg) ``` ``` ## tibble [234 × 11] (S3: tbl_df/tbl/data.frame) ## $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ... ## $ model : chr [1:234] "a4" "a4" "a4" "a4" ... ## $ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ... ## $ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ... ## $ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ... ## $ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ... ## $ drv : chr [1:234] "f" "f" "f" "f" ... ## $ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ... ## $ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ... ## $ fl : chr [1:234] "p" "p" "p" "p" ... ## $ class : chr [1:234] "compact" "compact" "compact" "compact" ... ``` --- # Understanding the ggplot2 syntax A `ggplot2` plot is built layer by layer by using the `+` operator. <center> <img src="img/ggplot2_structure.svg" alt="drawing" width="800"/> </center> <figcaption> The basic structure of a ggplot2 plot. </figcaption> --- # The geoms `geoms` is the abbreviation for *geometric objects* which are used to specify which type of graphic you want to produce (boxplot, barplot, scatter, ...). All `ggplot2` geoms start with the `geom_` prefix. <small> ``` ## [1] "geom_abline" "geom_area" "geom_bar" ## [4] "geom_bin2d" "geom_blank" "geom_boxplot" ## [7] "geom_col" "geom_contour" "geom_contour_filled" ## [10] "geom_count" "geom_crossbar" "geom_curve" ## [13] "geom_density" "geom_density_2d" "geom_density_2d_filled" ## [16] "geom_density2d" "geom_density2d_filled" "geom_dotplot" ## [19] "geom_errorbar" "geom_errorbarh" "geom_freqpoly" ## [22] "geom_function" "geom_hex" "geom_histogram" ## [25] "geom_hline" "geom_jitter" "geom_label" ## [28] "geom_line" "geom_linerange" "geom_map" ## [31] "geom_path" "geom_point" "geom_pointrange" ## [34] "geom_polygon" "geom_qq" "geom_qq_line" ## [37] "geom_quantile" "geom_raster" "geom_rect" ## [40] "geom_ribbon" "geom_rug" "geom_segment" ## [43] "geom_sf" "geom_sf_label" "geom_sf_text" ## [46] "geom_smooth" "geom_spoke" "geom_step" ## [49] "geom_text" "geom_tile" "geom_violin" ## [52] "geom_vline" ``` </small> --- class: inverse, center, middle # One variable graphics --- # One variable graphics In this section, we are going to see the two main types of one variable graphics: | **Graphic type** | **Geom** | **Description** | | ---------------- | ------------------ | ---------------------------------------- | | Histogram | `geom_histogram()` | Produces histograms for continuous data. | | Barplot | `geom_bar()` | Produces histograms for discrete data. | --- count: false # Histogram .panel1-histogram1-auto[ ```r *ggplot( * data = mpg, * mapping = aes(x = cty) *) ``` ] .panel2-histogram1-auto[ <img src="index_files/figure-html/histogram1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Histogram .panel1-histogram1-auto[ ```r ggplot( data = mpg, mapping = aes(x = cty) ) + * geom_histogram() ``` ] .panel2-histogram1-auto[ <img src="index_files/figure-html/histogram1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-histogram1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-histogram1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-histogram1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Barplot .panel1-barplot1-auto[ ```r *ggplot( * data = mpg, * mapping = aes(x = class) *) ``` ] .panel2-barplot1-auto[ <img src="index_files/figure-html/barplot1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Barplot .panel1-barplot1-auto[ ```r ggplot( data = mpg, mapping = aes(x = class) ) + * geom_bar() ``` ] .panel2-barplot1-auto[ <img src="index_files/figure-html/barplot1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-barplot1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-barplot1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-barplot1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Two variables graphics --- # Two variables graphics There are several types of charts with two variables. Here are the most used. | **Graphic type** | **Geom** | **Description** | | ---------------- | ---------------- | ------------------------------------------------------------- | | Barplot | `geom_col()` | Produces bars with heights that represent values in the data. | | Scatter plot | `geom_point()` | Produces scatter plot between `x` and `y`. | | Line plot | `geom_line()` | Produces line plot between `x` and `y`. | | Boxplot | `geom_boxplot()` | Boxplot between `x` and `y`. | --- # Barplot We used `geom_bar()` to visualize the number/count of cases for each value of `x`. If we want to represent values in the data (`y`), we can use `geom_col()`. For this example, I will use the `starwars` data that is included in the `dplyr` package. .gradient-underline[This graph shows the top 20 tallest characters (in cm).] .pull-left[ ```r ggplot( data = sw, mapping = aes( * x = name, * y = height ) ) + geom_col() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-8-1.svg" style="display: block; margin: auto;" /> ] -- **What is wrong with this graph?** --- # Barplot In the previous graph, it was difficult to read the names on the `x` axis. .gradient-underline[A better way to present the data is by swapping the `x` and `y` axes.] -- .pull-left[ ```r ggplot( data = sw, mapping = aes( * x = height, * y = name ) ) + geom_col() ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-9-1.svg" style="display: block; margin: auto;" /> ] --- # Ordering things We have made some improvements in the previous graph by swapping the axes. However, .gradient-underline[it would be even better if the characters were sorted by their height] to make the plot easier to understand. -- One way to do it is with `forcats::fct_reorder()`. Note that the `forcats` library is not part of base R and must be installed separately. -- .pull-left[ <small> ```r ggplot( data = sw, mapping = aes( x = height, * y = forcats::fct_reorder(name, height) ) ) + geom_col() ``` </small> ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-10-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Scatter plot .panel1-scatterplot1-auto[ ```r *ggplot(mpg, aes(x = displ, y = hwy)) ``` ] .panel2-scatterplot1-auto[ <img src="index_files/figure-html/scatterplot1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Scatter plot .panel1-scatterplot1-auto[ ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point() ``` ] .panel2-scatterplot1-auto[ <img src="index_files/figure-html/scatterplot1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-scatterplot1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scatterplot1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scatterplot1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Line plot .panel1-lineplot1-auto[ ```r *ggplot(mpg, aes(x = displ, y = hwy)) ``` ] .panel2-lineplot1-auto[ <img src="index_files/figure-html/lineplot1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Line plot .panel1-lineplot1-auto[ ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_line() ``` ] .panel2-lineplot1-auto[ <img src="index_files/figure-html/lineplot1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-lineplot1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-lineplot1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-lineplot1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Boxplot > In descriptive statistics, a box plot or boxplot is a convenient way of graphically depicting groups of numerical data through their quartiles (Wikipedia). To make a boxplot, we need to have a **discrete/categorical** variable on `x` and a **continuous** variable on `y`. --- count: false # Boxplot .panel1-boxplot1-auto[ ```r *ggplot( * data = mpg, * mapping = aes(x = class, y = cty) *) ``` ] .panel2-boxplot1-auto[ <img src="index_files/figure-html/boxplot1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Boxplot .panel1-boxplot1-auto[ ```r ggplot( data = mpg, mapping = aes(x = class, y = cty) ) + * geom_boxplot() ``` ] .panel2-boxplot1-auto[ <img src="index_files/figure-html/boxplot1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-boxplot1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-boxplot1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-boxplot1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Boxplot As we did with the StarWars data, we can reorder the `class` variable based on the `cty` variable to make the graphic more appealing with `forcats::fct_reorder()`. -- .pull-left[ <small> ```r ggplot( data = mpg, mapping = aes( * x = forcats::fct_reorder(class, cty), y = cty ) ) + geom_boxplot() ``` </small> ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-11-1.svg" style="display: block; margin: auto;" /> ] --- class: inverse, center, middle # Geom aesthetics --- # Geom aesthetics Aesthetics such as *color*, *shape*, *size* of the displayed geoms can be controlled inside the `geom_()` functions. For example, we can change the `color` and the `size` of the point in the `geom_point()` function. ```r geom_point(color = "red", size = 10) ``` <img src="index_files/figure-html/unnamed-chunk-13-1.svg" style="display: block; margin: auto;" /> --- # Working with colors If we want to set a specific color .gradient-underline[for all the points], we have to set it outside the aesthetic: `geom_point(color = "orange")`. ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(color = "orange") ``` <img src="index_files/figure-html/unnamed-chunk-14-1.svg" style="display: block; margin: auto;" /> --- # Working with colors If we want to set a color .gradient-underline[based on a variable], we have to use the aesthetic: `aes(colour = variable)`. ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(aes(color = class)) ``` <img src="index_files/figure-html/unnamed-chunk-15-1.svg" style="display: block; margin: auto;" /> --- # Setting colors manually By default, `ggplot2` uses a default color palette. ```r ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = drv)) ``` <img src="index_files/figure-html/unnamed-chunk-16-1.svg" style="display: block; margin: auto;" /> --- count: false # Setting colors manually We can also create our own palette of colors using *scale_color_manual()*. .panel1-manual_colors1-auto[ ```r *ggplot(mpg, aes(x = displ, y = hwy)) ``` ] .panel2-manual_colors1-auto[ <img src="index_files/figure-html/manual_colors1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Setting colors manually We can also create our own palette of colors using *scale_color_manual()*. .panel1-manual_colors1-auto[ ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(aes(color = drv)) ``` ] .panel2-manual_colors1-auto[ <img src="index_files/figure-html/manual_colors1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Setting colors manually We can also create our own palette of colors using *scale_color_manual()*. .panel1-manual_colors1-auto[ ```r ggplot(mpg, aes(x = displ, y = hwy)) + geom_point(aes(color = drv)) + * scale_color_manual( * breaks = c("4", "f", "r"), * values = c( * "orange", * "#CB2955", * "blue" * ), * labels = c( * "Front wheel drive", * "Rear wheel drive", * "4wd" * ) * ) ``` ] .panel2-manual_colors1-auto[ <img src="index_files/figure-html/manual_colors1_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-manual_colors1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-manual_colors1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-manual_colors1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- background-image: url(https://github.com/EmilHvitfeldt/paletteer/blob/master/man/figures/logo.png?raw=true) background-size: 90px background-position: 90% 8% # Color palettes for ggplot2 There are many different R packages that provide color palettes. `paletteer` is a comprehensive collection of color palettes in R created by [@Emil_Hvitfeldt](https://twitter.com/Emil_Hvitfeldt). I have made a [paletteer gallery](https://github.com/PMassicotte/paletteer_gallery) to help me navigate all the palettes. .pull-left[ <small> ```r *library(paletteer) ggplot( data = sw, mapping = aes( x = height, y = forcats::fct_reorder(name, height), * fill = height ) ) + geom_col() + * scale_fill_paletteer_c("ggthemes::Red-Gold") ``` </small> ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-17-1.svg" style="display: block; margin: auto;" /> ] --- # Types of color scales .gradient-underline[Attention to the type of color scale that is mapped to your data.] -- - Use `scale_color_*()` of `scale_colour_*()` for the .gradient-underline[color] of the geom. -- - Use `scale_fill_*()` for the .gradient-underline[fill color] of the geom. -- .pull-left[ <br> ```r ggplot( data = mpg, mapping = aes(x = class) ) + geom_bar( size = 1, * aes(fill = fl, color = class) ) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-18-1.svg" height="80%" style="display: block; margin: auto;" /> ] --- # Working with size As we did for the colors, the size of the geom (ex.: dots) can be based on a particular variable. ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(aes(size = cyl)) ``` <img src="index_files/figure-html/unnamed-chunk-19-1.svg" style="display: block; margin: auto;" /> --- # Continuous vs discrete mapping .gradient-underline[Pay attention to the type of data you are mapping to a color or shape scale.] If the mapped variable is .gradient-underline[continuous], the generated guide on the legend will be also .gradient-underline[continuous]. .pull-left[ <br> ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(aes(color = year)) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-20-1.svg" height="100%" style="display: block; margin: auto;" /> ] --- # Continuous vs discrete mapping .gradient-underline[Pay attention to the type of data you are mapping to a color or shape scale.] If the mapped variable is .gradient-underline[discrete], the generated guide on the legend will be also .gradient-underline[discrete]. You can convert a continuous variable into a factor with the `factor()` function. .pull-left[ <br> ```r ggplot(mpg, aes(x = displ, y = hwy)) + * geom_point(aes(color = factor(year))) ``` ] .pull-right[ <img src="index_files/figure-html/unnamed-chunk-21-1.svg" height="100%" style="display: block; margin: auto;" /> ] --- class: inverse, center, middle # Axes and titles --- # Axes and titles There are many ways to change the titles of the graphic and the axes. Here we are going to use the `labs()` function. These are the main parameters: - `title`: Main title of the graph - `x`, `y`: Titles for the axes - `subtitle`: Subtitle title of the graph (default: under the main title) - `caption`: Caption of the graph (default: bottom right of the graph) --- count: false # Axes and titles .panel1-axes_and_titles1-auto[ ```r *ggplot( * mpg, * aes(x = displ, y = cty) *) ``` ] .panel2-axes_and_titles1-auto[ <img src="index_files/figure-html/axes_and_titles1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Axes and titles .panel1-axes_and_titles1-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + * geom_point() ``` ] .panel2-axes_and_titles1-auto[ <img src="index_files/figure-html/axes_and_titles1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Axes and titles .panel1-axes_and_titles1-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + geom_point() + * labs( * title = "Car fuel economy", * subtitle = "Data from 1999 to 2008 for 38 popular models of cars", * caption = "Data from: https://fueleconomy.gov/", * x = "Engine displacement (L)", * y = "City miles per gallon" * ) ``` ] .panel2-axes_and_titles1-auto[ <img src="index_files/figure-html/axes_and_titles1_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-axes_and_titles1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-axes_and_titles1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-axes_and_titles1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Controling axis labels .panel1-axes_and_titles2-auto[ ```r *ggplot( * mpg, * aes(x = displ, y = cty) *) ``` ] .panel2-axes_and_titles2-auto[ <img src="index_files/figure-html/axes_and_titles2_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Controling axis labels .panel1-axes_and_titles2-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + * geom_point() ``` ] .panel2-axes_and_titles2-auto[ <img src="index_files/figure-html/axes_and_titles2_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Controling axis labels .panel1-axes_and_titles2-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + geom_point() + * scale_x_continuous( * breaks = c(3, 6), * labels = c("Three", "Six") * ) ``` ] .panel2-axes_and_titles2-auto[ <img src="index_files/figure-html/axes_and_titles2_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-axes_and_titles2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-axes_and_titles2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-axes_and_titles2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Faceting --- # Faceting Faceting is a technique that allows displaying additional categorical variables in facets. Within `ggplot2`, there are two types of faceting: `facet_grid()` and `facet_wrap()`. <center> <figure> <img src="img/makeovermonday_2020w11.png" width="500"> <figcaption> Small multiple graphs can be useful to visualize data divided into smaller subsets. </figcaption> </figure> </center> --- count: false # 1D faceting .panel1-facet1-auto[ ```r *ggplot( * mpg, * aes(x = displ, y = cty) *) ``` ] .panel2-facet1-auto[ <img src="index_files/figure-html/facet1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 1D faceting .panel1-facet1-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + * geom_point() ``` ] .panel2-facet1-auto[ <img src="index_files/figure-html/facet1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 1D faceting .panel1-facet1-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + geom_point() + * facet_wrap(~class) ``` ] .panel2-facet1-auto[ <img src="index_files/figure-html/facet1_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-facet1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-facet1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-facet1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # 1D faceting .panel1-facet2-auto[ ```r *ggplot( * mpg, * aes(x = displ, y = cty) *) ``` ] .panel2-facet2-auto[ <img src="index_files/figure-html/facet2_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 1D faceting .panel1-facet2-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + * geom_point() ``` ] .panel2-facet2-auto[ <img src="index_files/figure-html/facet2_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 1D faceting .panel1-facet2-auto[ ```r ggplot( mpg, aes(x = displ, y = cty) ) + geom_point() + * facet_wrap(~class, scales = "free") ``` ] .panel2-facet2-auto[ <img src="index_files/figure-html/facet2_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-facet2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-facet2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-facet2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # 2D faceting .panel1-facet3-auto[ ```r *ggplot( * mpg, * aes(x = cty) *) ``` ] .panel2-facet3-auto[ <img src="index_files/figure-html/facet3_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 2D faceting .panel1-facet3-auto[ ```r ggplot( mpg, aes(x = cty) ) + * geom_histogram() ``` ] .panel2-facet3-auto[ <img src="index_files/figure-html/facet3_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # 2D faceting .panel1-facet3-auto[ ```r ggplot( mpg, aes(x = cty) ) + geom_histogram() + * facet_grid(cyl ~ class) ``` ] .panel2-facet3-auto[ <img src="index_files/figure-html/facet3_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-facet3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-facet3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-facet3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Using ggplot2 themes --- # ggplot2 themes > Themes are a powerful way to customize the non-data components of your plots: i.e. titles, labels, fonts, background, gridlines, and legends. Many components can be changed using the `theme()` function. Today we are going to see just a few of them, but feel free to have a look at `?theme` to have more information. -- `theme()` can be used to modify: -- - The background aesthetics (color, grid, etc.). -- - Axis titles and ticks aesthetics. -- - Legend titles and positions. -- - Aesthetics of plot titles (title, subtitle, caption, etc.). -- - Plot margins. --- count: false # Built in themes .panel1-theme1-auto[ ```r *p ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Built in themes .panel1-theme1-auto[ ```r p + * theme_bw() ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Built in themes .panel1-theme1-auto[ ```r p + theme_bw() + * theme_light() ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Built in themes .panel1-theme1-auto[ ```r p + theme_bw() + theme_light() + * theme_gray() ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_04_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Built in themes .panel1-theme1-auto[ ```r p + theme_bw() + theme_light() + theme_gray() + * theme_dark() ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_05_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Built in themes .panel1-theme1-auto[ ```r p + theme_bw() + theme_light() + theme_gray() + theme_dark() + * theme_void() ``` ] .panel2-theme1-auto[ <img src="index_files/figure-html/theme1_auto_06_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-theme1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-theme1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-theme1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # External themes .panel1-theme2-auto[ ```r *p ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + * ggthemes::theme_solarized() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + * ggthemes::theme_fivethirtyeight() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + ggthemes::theme_fivethirtyeight() + * ggthemes::theme_calc() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_04_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + ggthemes::theme_fivethirtyeight() + ggthemes::theme_calc() + * ggthemes::theme_excel() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_05_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + ggthemes::theme_fivethirtyeight() + ggthemes::theme_calc() + ggthemes::theme_excel() + * ggpubr::theme_pubr() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_06_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + ggthemes::theme_fivethirtyeight() + ggthemes::theme_calc() + ggthemes::theme_excel() + ggpubr::theme_pubr() + * ggpubr::theme_cleveland() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_07_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # External themes .panel1-theme2-auto[ ```r p + ggthemes::theme_solarized() + ggthemes::theme_fivethirtyeight() + ggthemes::theme_calc() + ggthemes::theme_excel() + ggpubr::theme_pubr() + ggpubr::theme_cleveland() + * ggpubr::theme_pubclean() ``` ] .panel2-theme2-auto[ <img src="index_files/figure-html/theme2_auto_08_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-theme2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-theme2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-theme2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Legend position .panel1-legend_position-auto[ ```r *p ``` ] .panel2-legend_position-auto[ <img src="index_files/figure-html/legend_position_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Legend position .panel1-legend_position-auto[ ```r p + * theme(legend.position = "top") ``` ] .panel2-legend_position-auto[ <img src="index_files/figure-html/legend_position_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Legend position .panel1-legend_position-auto[ ```r p + theme(legend.position = "top") + * theme(legend.position = "left") ``` ] .panel2-legend_position-auto[ <img src="index_files/figure-html/legend_position_auto_03_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Legend position .panel1-legend_position-auto[ ```r p + theme(legend.position = "top") + theme(legend.position = "left") + * theme(legend.position = "bottom") ``` ] .panel2-legend_position-auto[ <img src="index_files/figure-html/legend_position_auto_04_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-legend_position-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-legend_position-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-legend_position-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # ggplot2 themes There are many components that can be modified with the `theme()` function. Most of these components are modified using the `element_*()` functions. -- - `element_rect()`: for borders and backgrounds -- - `element_line()`: for lines elements -- - `element_text()`: for text --- count: false # Grid .panel1-grid-auto[ ```r *p ``` ] .panel2-grid-auto[ <img src="index_files/figure-html/grid_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Grid .panel1-grid-auto[ ```r p + * theme( * panel.grid = element_line( * size = 3, * color = "red" * ) * ) ``` ] .panel2-grid-auto[ <img src="index_files/figure-html/grid_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-grid-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-grid-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-grid-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Axes ticks .panel1-ticks-auto[ ```r *p ``` ] .panel2-ticks-auto[ <img src="index_files/figure-html/ticks_auto_01_output-1.svg" style="display: block; margin: auto;" /> ] --- count: false # Axes ticks .panel1-ticks-auto[ ```r p + * theme( * axis.ticks = element_line( * size = 2, * color = "blue" * ), * axis.ticks.length = unit(1, "cm") * ) ``` ] .panel2-ticks-auto[ <img src="index_files/figure-html/ticks_auto_02_output-1.svg" style="display: block; margin: auto;" /> ] <style> .panel1-ticks-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ticks-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ticks-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle <center> <figure> <img src="https://github.com/allisonhorst/stats-illustrations/blob/master/rstats-artwork/patchwork_1.jpg?raw=true" width="700"> <figcaption> Artwork by <a href="https://twitter.com/allison_horst?s=20">@allison_horst</a> </figcaption> </figure> </center> --- # Combining plots There are many R packages that can be used to combine plots. Here we are going to have a quick overview of the `patchwork` package. The package is not part of base R so it needs to be installed and loaded before you can use it. ```r install.packages("patchwork") library(patchwork) ``` <center> <figure> <img src="https://github.com/thomasp85/patchwork/blob/master/man/figures/logo.png?raw=true" width="200"> <figcaption> <a href="https://github.com/thomasp85/patchwork">The patchwork package</a> </figcaption> </figure> </center> --- # Combining plots First, let's make some plots. We are going to use a dataset about [alcohol use by country](https://datahub.io/five-thirty-eight/alcohol-consumption/r/0.html). It is a dataset containing 193 rows and 5 columns. Here is a preview of the data. <div data-pagedtable="false"> <script data-pagedtable-source type="application/json"> {"columns":[{"label":["country"],"name":[1],"type":["chr"],"align":["left"]},{"label":["beer_servings"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["spirit_servings"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["wine_servings"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["total_litres_of_pure_alcohol"],"name":[5],"type":["dbl"],"align":["right"]}],"data":[{"1":"afghanistan","2":"0","3":"0","4":"0","5":"0.0"},{"1":"albania","2":"89","3":"132","4":"54","5":"4.9"},{"1":"algeria","2":"25","3":"0","4":"14","5":"0.7"},{"1":"andorra","2":"245","3":"138","4":"312","5":"12.4"},{"1":"angola","2":"217","3":"57","4":"45","5":"5.9"},{"1":"antigua & barbuda","2":"102","3":"128","4":"45","5":"4.9"},{"1":"argentina","2":"193","3":"25","4":"221","5":"8.3"},{"1":"armenia","2":"21","3":"179","4":"11","5":"3.8"},{"1":"australia","2":"261","3":"72","4":"212","5":"10.4"},{"1":"austria","2":"279","3":"75","4":"191","5":"9.7"},{"1":"azerbaijan","2":"21","3":"46","4":"5","5":"1.3"},{"1":"bahamas","2":"122","3":"176","4":"51","5":"6.3"},{"1":"bahrain","2":"42","3":"63","4":"7","5":"2.0"},{"1":"bangladesh","2":"0","3":"0","4":"0","5":"0.0"},{"1":"barbados","2":"143","3":"173","4":"36","5":"6.3"},{"1":"belarus","2":"142","3":"373","4":"42","5":"14.4"},{"1":"belgium","2":"295","3":"84","4":"212","5":"10.5"},{"1":"belize","2":"263","3":"114","4":"8","5":"6.8"},{"1":"benin","2":"34","3":"4","4":"13","5":"1.1"},{"1":"bhutan","2":"23","3":"0","4":"0","5":"0.4"},{"1":"bolivia","2":"167","3":"41","4":"8","5":"3.8"},{"1":"bosnia-herzegovina","2":"76","3":"173","4":"8","5":"4.6"},{"1":"botswana","2":"173","3":"35","4":"35","5":"5.4"},{"1":"brazil","2":"245","3":"145","4":"16","5":"7.2"},{"1":"brunei","2":"31","3":"2","4":"1","5":"0.6"},{"1":"bulgaria","2":"231","3":"252","4":"94","5":"10.3"},{"1":"burkina faso","2":"25","3":"7","4":"7","5":"4.3"},{"1":"burundi","2":"88","3":"0","4":"0","5":"6.3"},{"1":"cote d'ivoire","2":"37","3":"1","4":"7","5":"4.0"},{"1":"cabo verde","2":"144","3":"56","4":"16","5":"4.0"},{"1":"cambodia","2":"57","3":"65","4":"1","5":"2.2"},{"1":"cameroon","2":"147","3":"1","4":"4","5":"5.8"},{"1":"canada","2":"240","3":"122","4":"100","5":"8.2"},{"1":"central african republic","2":"17","3":"2","4":"1","5":"1.8"},{"1":"chad","2":"15","3":"1","4":"1","5":"0.4"},{"1":"chile","2":"130","3":"124","4":"172","5":"7.6"},{"1":"china","2":"79","3":"192","4":"8","5":"5.0"},{"1":"colombia","2":"159","3":"76","4":"3","5":"4.2"},{"1":"comoros","2":"1","3":"3","4":"1","5":"0.1"},{"1":"congo","2":"76","3":"1","4":"9","5":"1.7"},{"1":"cook islands","2":"0","3":"254","4":"74","5":"5.9"},{"1":"costa rica","2":"149","3":"87","4":"11","5":"4.4"},{"1":"croatia","2":"230","3":"87","4":"254","5":"10.2"},{"1":"cuba","2":"93","3":"137","4":"5","5":"4.2"},{"1":"cyprus","2":"192","3":"154","4":"113","5":"8.2"},{"1":"czech republic","2":"361","3":"170","4":"134","5":"11.8"},{"1":"north korea","2":"0","3":"0","4":"0","5":"0.0"},{"1":"dr congo","2":"32","3":"3","4":"1","5":"2.3"},{"1":"denmark","2":"224","3":"81","4":"278","5":"10.4"},{"1":"djibouti","2":"15","3":"44","4":"3","5":"1.1"},{"1":"dominica","2":"52","3":"286","4":"26","5":"6.6"},{"1":"dominican republic","2":"193","3":"147","4":"9","5":"6.2"},{"1":"ecuador","2":"162","3":"74","4":"3","5":"4.2"},{"1":"egypt","2":"6","3":"4","4":"1","5":"0.2"},{"1":"el salvador","2":"52","3":"69","4":"2","5":"2.2"},{"1":"equatorial guinea","2":"92","3":"0","4":"233","5":"5.8"},{"1":"eritrea","2":"18","3":"0","4":"0","5":"0.5"},{"1":"estonia","2":"224","3":"194","4":"59","5":"9.5"},{"1":"ethiopia","2":"20","3":"3","4":"0","5":"0.7"},{"1":"fiji","2":"77","3":"35","4":"1","5":"2.0"},{"1":"finland","2":"263","3":"133","4":"97","5":"10.0"},{"1":"france","2":"127","3":"151","4":"370","5":"11.8"},{"1":"gabon","2":"347","3":"98","4":"59","5":"8.9"},{"1":"gambia","2":"8","3":"0","4":"1","5":"2.4"},{"1":"georgia","2":"52","3":"100","4":"149","5":"5.4"},{"1":"germany","2":"346","3":"117","4":"175","5":"11.3"},{"1":"ghana","2":"31","3":"3","4":"10","5":"1.8"},{"1":"greece","2":"133","3":"112","4":"218","5":"8.3"},{"1":"grenada","2":"199","3":"438","4":"28","5":"11.9"},{"1":"guatemala","2":"53","3":"69","4":"2","5":"2.2"},{"1":"guinea","2":"9","3":"0","4":"2","5":"0.2"},{"1":"guinea-bissau","2":"28","3":"31","4":"21","5":"2.5"},{"1":"guyana","2":"93","3":"302","4":"1","5":"7.1"},{"1":"haiti","2":"1","3":"326","4":"1","5":"5.9"},{"1":"honduras","2":"69","3":"98","4":"2","5":"3.0"},{"1":"hungary","2":"234","3":"215","4":"185","5":"11.3"},{"1":"iceland","2":"233","3":"61","4":"78","5":"6.6"},{"1":"india","2":"9","3":"114","4":"0","5":"2.2"},{"1":"indonesia","2":"5","3":"1","4":"0","5":"0.1"},{"1":"iran","2":"0","3":"0","4":"0","5":"0.0"},{"1":"iraq","2":"9","3":"3","4":"0","5":"0.2"},{"1":"ireland","2":"313","3":"118","4":"165","5":"11.4"},{"1":"israel","2":"63","3":"69","4":"9","5":"2.5"},{"1":"italy","2":"85","3":"42","4":"237","5":"6.5"},{"1":"jamaica","2":"82","3":"97","4":"9","5":"3.4"},{"1":"japan","2":"77","3":"202","4":"16","5":"7.0"},{"1":"jordan","2":"6","3":"21","4":"1","5":"0.5"},{"1":"kazakhstan","2":"124","3":"246","4":"12","5":"6.8"},{"1":"kenya","2":"58","3":"22","4":"2","5":"1.8"},{"1":"kiribati","2":"21","3":"34","4":"1","5":"1.0"},{"1":"kuwait","2":"0","3":"0","4":"0","5":"0.0"},{"1":"kyrgyzstan","2":"31","3":"97","4":"6","5":"2.4"},{"1":"laos","2":"62","3":"0","4":"123","5":"6.2"},{"1":"latvia","2":"281","3":"216","4":"62","5":"10.5"},{"1":"lebanon","2":"20","3":"55","4":"31","5":"1.9"},{"1":"lesotho","2":"82","3":"29","4":"0","5":"2.8"},{"1":"liberia","2":"19","3":"152","4":"2","5":"3.1"},{"1":"libya","2":"0","3":"0","4":"0","5":"0.0"},{"1":"lithuania","2":"343","3":"244","4":"56","5":"12.9"},{"1":"luxembourg","2":"236","3":"133","4":"271","5":"11.4"},{"1":"madagascar","2":"26","3":"15","4":"4","5":"0.8"},{"1":"malawi","2":"8","3":"11","4":"1","5":"1.5"},{"1":"malaysia","2":"13","3":"4","4":"0","5":"0.3"},{"1":"maldives","2":"0","3":"0","4":"0","5":"0.0"},{"1":"mali","2":"5","3":"1","4":"1","5":"0.6"},{"1":"malta","2":"149","3":"100","4":"120","5":"6.6"},{"1":"marshall islands","2":"0","3":"0","4":"0","5":"0.0"},{"1":"mauritania","2":"0","3":"0","4":"0","5":"0.0"},{"1":"mauritius","2":"98","3":"31","4":"18","5":"2.6"},{"1":"mexico","2":"238","3":"68","4":"5","5":"5.5"},{"1":"micronesia","2":"62","3":"50","4":"18","5":"2.3"},{"1":"monaco","2":"0","3":"0","4":"0","5":"0.0"},{"1":"mongolia","2":"77","3":"189","4":"8","5":"4.9"},{"1":"montenegro","2":"31","3":"114","4":"128","5":"4.9"},{"1":"morocco","2":"12","3":"6","4":"10","5":"0.5"},{"1":"mozambique","2":"47","3":"18","4":"5","5":"1.3"},{"1":"myanmar","2":"5","3":"1","4":"0","5":"0.1"},{"1":"namibia","2":"376","3":"3","4":"1","5":"6.8"},{"1":"nauru","2":"49","3":"0","4":"8","5":"1.0"},{"1":"nepal","2":"5","3":"6","4":"0","5":"0.2"},{"1":"netherlands","2":"251","3":"88","4":"190","5":"9.4"},{"1":"new zealand","2":"203","3":"79","4":"175","5":"9.3"},{"1":"nicaragua","2":"78","3":"118","4":"1","5":"3.5"},{"1":"niger","2":"3","3":"2","4":"1","5":"0.1"},{"1":"nigeria","2":"42","3":"5","4":"2","5":"9.1"},{"1":"niue","2":"188","3":"200","4":"7","5":"7.0"},{"1":"norway","2":"169","3":"71","4":"129","5":"6.7"},{"1":"oman","2":"22","3":"16","4":"1","5":"0.7"},{"1":"pakistan","2":"0","3":"0","4":"0","5":"0.0"},{"1":"palau","2":"306","3":"63","4":"23","5":"6.9"},{"1":"panama","2":"285","3":"104","4":"18","5":"7.2"},{"1":"papua new guinea","2":"44","3":"39","4":"1","5":"1.5"},{"1":"paraguay","2":"213","3":"117","4":"74","5":"7.3"},{"1":"peru","2":"163","3":"160","4":"21","5":"6.1"},{"1":"philippines","2":"71","3":"186","4":"1","5":"4.6"},{"1":"poland","2":"343","3":"215","4":"56","5":"10.9"},{"1":"portugal","2":"194","3":"67","4":"339","5":"11.0"},{"1":"qatar","2":"1","3":"42","4":"7","5":"0.9"},{"1":"south korea","2":"140","3":"16","4":"9","5":"9.8"},{"1":"moldova","2":"109","3":"226","4":"18","5":"6.3"},{"1":"romania","2":"297","3":"122","4":"167","5":"10.4"},{"1":"russian federation","2":"247","3":"326","4":"73","5":"11.5"},{"1":"rwanda","2":"43","3":"2","4":"0","5":"6.8"},{"1":"st. kitts & nevis","2":"194","3":"205","4":"32","5":"7.7"},{"1":"st. lucia","2":"171","3":"315","4":"71","5":"10.1"},{"1":"st. vincent & the grenadines","2":"120","3":"221","4":"11","5":"6.3"},{"1":"samoa","2":"105","3":"18","4":"24","5":"2.6"},{"1":"san marino","2":"0","3":"0","4":"0","5":"0.0"},{"1":"sao tome & principe","2":"56","3":"38","4":"140","5":"4.2"},{"1":"saudi arabia","2":"0","3":"5","4":"0","5":"0.1"},{"1":"senegal","2":"9","3":"1","4":"7","5":"0.3"},{"1":"serbia","2":"283","3":"131","4":"127","5":"9.6"},{"1":"seychelles","2":"157","3":"25","4":"51","5":"4.1"},{"1":"sierra leone","2":"25","3":"3","4":"2","5":"6.7"},{"1":"singapore","2":"60","3":"12","4":"11","5":"1.5"},{"1":"slovakia","2":"196","3":"293","4":"116","5":"11.4"},{"1":"slovenia","2":"270","3":"51","4":"276","5":"10.6"},{"1":"solomon islands","2":"56","3":"11","4":"1","5":"1.2"},{"1":"somalia","2":"0","3":"0","4":"0","5":"0.0"},{"1":"south africa","2":"225","3":"76","4":"81","5":"8.2"},{"1":"spain","2":"284","3":"157","4":"112","5":"10.0"},{"1":"sri lanka","2":"16","3":"104","4":"0","5":"2.2"},{"1":"sudan","2":"8","3":"13","4":"0","5":"1.7"},{"1":"suriname","2":"128","3":"178","4":"7","5":"5.6"},{"1":"swaziland","2":"90","3":"2","4":"2","5":"4.7"},{"1":"sweden","2":"152","3":"60","4":"186","5":"7.2"},{"1":"switzerland","2":"185","3":"100","4":"280","5":"10.2"},{"1":"syria","2":"5","3":"35","4":"16","5":"1.0"},{"1":"tajikistan","2":"2","3":"15","4":"0","5":"0.3"},{"1":"thailand","2":"99","3":"258","4":"1","5":"6.4"},{"1":"macedonia","2":"106","3":"27","4":"86","5":"3.9"},{"1":"timor-leste","2":"1","3":"1","4":"4","5":"0.1"},{"1":"togo","2":"36","3":"2","4":"19","5":"1.3"},{"1":"tonga","2":"36","3":"21","4":"5","5":"1.1"},{"1":"trinidad & tobago","2":"197","3":"156","4":"7","5":"6.4"},{"1":"tunisia","2":"51","3":"3","4":"20","5":"1.3"},{"1":"turkey","2":"51","3":"22","4":"7","5":"1.4"},{"1":"turkmenistan","2":"19","3":"71","4":"32","5":"2.2"},{"1":"tuvalu","2":"6","3":"41","4":"9","5":"1.0"},{"1":"uganda","2":"45","3":"9","4":"0","5":"8.3"},{"1":"ukraine","2":"206","3":"237","4":"45","5":"8.9"},{"1":"united arab emirates","2":"16","3":"135","4":"5","5":"2.8"},{"1":"united kingdom","2":"219","3":"126","4":"195","5":"10.4"},{"1":"tanzania","2":"36","3":"6","4":"1","5":"5.7"},{"1":"usa","2":"249","3":"158","4":"84","5":"8.7"},{"1":"uruguay","2":"115","3":"35","4":"220","5":"6.6"},{"1":"uzbekistan","2":"25","3":"101","4":"8","5":"2.4"},{"1":"vanuatu","2":"21","3":"18","4":"11","5":"0.9"},{"1":"venezuela","2":"333","3":"100","4":"3","5":"7.7"},{"1":"vietnam","2":"111","3":"2","4":"1","5":"2.0"},{"1":"yemen","2":"6","3":"0","4":"0","5":"0.1"},{"1":"zambia","2":"32","3":"19","4":"4","5":"2.5"},{"1":"zimbabwe","2":"64","3":"18","4":"4","5":"4.7"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[7],"max":[7]},"pages":{}}} </script> </div> --- # Overview of the data The data description is as follows: - `country`: Name of the country - `beer_servings`: Average number of beer servings per capita - `spirit_servings`: Average number of spirit servings per capita - `wine_servings`: Average number of wine servings per capita - `total_litres_of_pure_alcohol`: Average quantity of pure alcohol consumed per capita --- # Let's make a plot This graph shows the top 10 countries with the highest average quantity of pure alcohol consumed. ```r p1 ``` <img src="index_files/figure-html/unnamed-chunk-26-1.svg" style="display: block; margin: auto;" /> --- # Let's make another plot This graph shows the type of servings for the top 10 countries with the highest average quantity of pure alcohol consumed. ```r p2 ``` <img src="index_files/figure-html/unnamed-chunk-28-1.svg" style="display: block; margin: auto;" /> --- # Combining horizontally Combining horizontally is done using the `+` operator. ```r p1 + p2 ``` <img src="index_files/figure-html/unnamed-chunk-29-1.svg" style="display: block; margin: auto;" /> --- # Combining vertically Combining vertically is done using the `/` operator. ```r p1 / p2 ``` <img src="index_files/figure-html/unnamed-chunk-30-1.svg" style="display: block; margin: auto;" /> --- # More complex arrangement ```r (p1 + p2) / p1 ``` <img src="index_files/figure-html/unnamed-chunk-31-1.svg" width="100%" style="display: block; margin: auto;" /> --- # Adding labels .pull-left[ It is often interesting to add a title and necessary to identify each panel (graph) with a letter. This can be achieved with the `plot_annotation()`. ```r p1 / p2 + plot_annotation( * tag_levels = "A", * title = "World alcohol consumption" ) ``` ] .pull-right[ <img src="index_files/figure-html/plot-annotation2-1.svg" style="display: block; margin: auto;" /> ] --- class: inverse, center, middle # Saving your graphics <br> .pull-left[
] .pull-right[
] --- # Saving your graphics Saving `ggplot2` graphics is done with the `ggsave()` function. ```r p <- ggplot(mpg, aes(x = displ, y = cty)) + geom_point() ``` **Vector formats** ```r ggsave("path/to/myfile.pdf", p, width = 5.97, height = 4.79) ggsave("path/to/myfile.eps", p, width = 5.97, height = 4.79) ggsave("path/to/myfile.ps", p, width = 5.97, height = 4.79) ``` **Raster formats** ```r ggsave("path/to/myfile.jpg", p, width = 5.97, height = 4.79, dpi = 300) ggsave("path/to/myfile.tiff", p, width = 5.97, height = 4.79, dpi = 300) ggsave("path/to/myfile.png", p, width = 5.97, height = 4.79, dpi = 300) ``` --- # Vector vs raster graphics .pull-left[ > The main difference between vector and raster graphics is that raster graphics are composed of pixels, while vector graphics are composed of paths. .left[ <credit> Source: <a href="https://www.geeksforgeeks.org/vector-vs-raster-graphics/">Vector vs Raster Graphics</a> </credit> ] - Scientific journals often require at least 300 DPI raster or vector graphics. - .gradient-underline[Attention: vector graphics can produce very large files for certain types of graphics (ex.: 3D plots).] ] .pull-right[ <center> <figure> <img src="https://upload.wikimedia.org/wikipedia/commons/a/aa/VectorBitmapExample.svg" height="400"> <figcaption> Source: Wikipedia </figcaption> </figure> </center> ] --- # A final note... There is a lot to learn about `ggplot2` and data visualization, but I hope you have learned something today. -- If it is not the case... -- <div class="row"> <div class="column"> <img src="https://media.giphy.com/media/62cTOas6rcoRQsxwSy/giphy.gif" alt="Snow" style="width:80%"> </div> <div class="column"> <img src="https://media.giphy.com/media/3Gl8sOjurR3ihZj27S/giphy.gif" alt="Forest" style="width:80%"> </div> <div class="column"> <img src="https://media.giphy.com/media/ekvSclOoygt90LW8Ro/giphy.gif" alt="Forest" style="width:80%"> </div> </div>