class: center, middle, inverse, title-slide # Adding figures in R Markdown ## 15’ class for the R-Studio Certification ###
Beatriz Milz
### September, 2020
Hex-sticker wall as background image made by
Amelia McNamara
.
--- class: center, middle <!-- inicio font awesome --> <script src="https://kit.fontawesome.com/1f72d6921a.js" crossorigin="anonymous"></script> <!-- final font awesome --> <!-- inicio academic icons --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/jpswalsh/academicons@1/css/academicons.min.css"> <!-- final academic icons --> ## Previous classes: R Markdown! <img src="https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/rmarkdown_rockstar.png" width="70%" /> Illustration made by [@Allison Horst](https://github.com/allisonhorst/stats-illustrations). ??? - Open RStudio, create new Rmarkdown file, talk about the structure of the file: yaml, markdown, R Code. Talk about chunk options. --- class: inverse, middle, center ## How to add figures to an R Markdown report? --- ## How to add figures to an R Markdown report? - __Markdown__. For example: `![description of image](file path)` - __R__. - Generated by other R code, such as `ggplot` - With `knitr::include_graphics()` 👩💻 - Let's practice using R code + `knitr::include_graphics()`! --- ## Adding an figure with knitr - Function `include_graphics()` from the package `knitr` .panelset[ .panel[.panel-name[Complete URL] ```r knitr::include_graphics("https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png") ``` ![](https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png)<!-- --> ] .panel[.panel-name[Image saved in a directory of the RProject] ```r knitr::include_graphics("img/rmarkdown.png") ``` <img src="img/rmarkdown.png" width="371" /> ] ] --- ## Output .panelset[ .panel[.panel-name[Don't show the code] - `echo = FALSE` ![](https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png)<!-- --> ] .panel[.panel-name[Show the code] - `echo = TRUE` ```r knitr::include_graphics("https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png") ``` ![](https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png)<!-- --> ] ] --- ## Align .panelset[ .panel[.panel-name[Default] - `fig.align='default'` ![](https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png)<!-- --> ] .panel[.panel-name[Left] - `fig.align='left'` <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" style="display: block; margin: auto auto auto 0;" /> ] .panel[.panel-name[Center] - `fig.align='center'` <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" style="display: block; margin: auto;" /> ] .panel[.panel-name[Right] - `fig.align='right'` <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" style="display: block; margin: auto 0 auto auto;" /> ] ] --- ## Size - Relative - `out.width="__%"` - Percentage of width of the page .panelset[ .panel[.panel-name[50%] <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" width="50%" style="display: block; margin: auto;" /> ] .panel[.panel-name[100%] <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" width="100%" style="display: block; margin: auto;" /> ] ] --- ## Size - Absolute - `out.width="__px"` - Size in pixels .panelset[ .panel[.panel-name[out.width="100px"] <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" width="100px" style="display: block; margin: auto;" /> ] .panel[.panel-name[out.width="300px"] <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" width="300px" style="display: block; margin: auto;" /> ] ] --- ## Caption - `fig.cap="caption text"` Ex: `fig.cap="Image of knitr package. From https://yihui.org/knitr/"` <div class="figure" style="text-align: center"> <img src="https://d33wubrfki0l68.cloudfront.net/eb48bae40b24285c0c8f281e6d2a7500cf3b9fc8/a2e5f/knitr/images/knit-logo.png" alt="Image of knitr package. From https://yihui.org/knitr/" width="25%" /> <p class="caption">Image of knitr package. From https://yihui.org/knitr/</p> </div> --- ## Questions
[<i class="fas fa-external-link-alt"></i> Open full page](https://beatriz-milz.shinyapps.io/formative_assessment/) --- class: inverse, middle, center ## Thank you! Slides made with the R packages [xaringan](https://github.com/yihui/xaringan), [xaringanthemer](https://github.com/gadenbuie/xaringanthemer), [metathis](https://pkg.garrickadenbuie.com/metathis/) and [xaringanExtra](https://github.com/gadenbuie/xaringanExtra). Hex-sticker wall as background image made by [Amelia McNamara](https://www.amelia.mn/blog/misc/2019/08/17/Tidy-Dress.html).