Professional, Polished, Presentable > Act I > 03: xaringan extras
03: xaringan extras
By Garrick Aden-Buie & Silvia Canelón
Activity
Time | 6 minutes |
Materials | 03-xaringan-extras/03-start.Rmd |
Activity Mode | Break out room & work with your group |
Goal
03-start.Rmd
contains one slide with a plot and the code used to create it,
but neither is presented well.
Our goal for this activity is to present both the code and the plot
in a way that helps the presenter and the audience.
Two Slides
For our first configuration, split the presentation into two slides. First present the code, followed by the plot on a new slide.
Use knitr::fig_chunk()
or the ref.label
option to separate the code and the plot.
knitr::fig\_chunk()
```{r lemur-weight, fig.show="hide"}
# ggplot2 plotting code
```
---
![Figure alt text](`r knitr::fig_chunk("lemur-weight", "png")`)
ref.label
```{r lemur-weight, eval=FALSE}
# ggplot2 plotting code
```
---
```{r ref.label="lemur-weight", echo=FALSE}
```
Use Panelsets
Reset the code in your slides to the original lemur-weight
chunk.
Original Chunk
```{r lemur-weight}
library(tidyverse)
lemurs <- readRDS("lemurs.rds")
lemurs %>%
filter(
common_name == "Ring-Tailed Lemur",
between(age_at_wt_y, 1, 5)
) %>%
ggplot() +
aes(x = age_at_wt_y, y = weight) +
geom_point() +
labs(
x = "Age at Weight",
y = "Weight (g)",
title = "Weight Gain of Young Ring-Tailed Lemurs"
)
```
Now, use panelsets to place the code and output in the same slide:
Add a new chunk to your slides to use panelsets
```{r xaringanExtra, echo=FALSE} xaringanExtra::use_xaringan_extra(c("panelset")) ```
Wrap the
lemur-weight
chunk in a.panelset[ ... ]
Add
panelset=TRUE
to thelemur-weight
chunk- ⚠️ If you’re using a xaringanExtra
< 0.5.4
, you will also need to add theresults="hold"
chunk option.
- ⚠️ If you’re using a xaringanExtra
Save and render the slides to preview!
Checkpoint
.panelset[
```{r lemur-weight, panelset=TRUE}
library(tidyverse)
lemurs <- readRDS("lemurs.rds")
lemurs %>%
filter(
common_name == "Ring-Tailed Lemur",
between(age_at_wt_y, 1, 5)
) %>%
ggplot() +
aes(x = age_at_wt_y, y = weight) +
geom_point() +
labs(
x = "Age at Weight",
y = "Weight (g)",
title = "Weight Gain of Young Ring-Tailed Lemurs"
)
```
]
Add another panel with a regression line over the lemur weight plot.
Inside the
.panelset[ ... ]
, add a new.panel[ ... ]
In the
.panel[ ]
, add.panel-name[Regression]
Add a new chunk that adds
geom_smooth()
to the lemur weight plot```{r echo=FALSE} last_plot() + geom_smooth(method = "lm") ```
Save and render your slides. Flip between each of the panels.
Checkpoint
.panel[
.panel-name[Regression]
```{r echo=FALSE}
last_plot() + geom_smooth(method = "lm")
```
]
Add Scribble
For our last trick, we’ll add the scribble feature to our slides. Scribble lets you draw right on the slides!
Add scribble and guess the regression.
Add
"scribble"
to the extras inuse_xaringan_extra()
.Save and render your slides.
On the plot slide, navigate to the Output panel. Press S to enable scribble mode. Draw a regression line on the lemur weight plot.
Press S again to turn off scribble mode. Navigate to the Regression panel. How good was your guestimated regression line?
Checkpoint
xaringanExtra::use_xaringan_extra(c("panelset", "scribble"))