03: xaringan extras

By Garrick Aden-Buie & Silvia Canelón

Activity

Time6 minutes
Materials03-xaringan-extras/03-start.Rmd
Activity ModeBreak 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:

  1. Add a new chunk to your slides to use panelsets

    ```{r xaringanExtra, echo=FALSE}
    xaringanExtra::use_xaringan_extra(c("panelset"))
    ```
    
  2. Wrap the lemur-weight chunk in a .panelset[ ... ]

  3. Add panelset=TRUE to the lemur-weight chunk

    • ⚠️ If you’re using a xaringanExtra < 0.5.4, you will also need to add the results="hold" chunk option.
  4. 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.

  1. Inside the .panelset[ ... ], add a new .panel[ ... ]

  2. In the .panel[ ], add .panel-name[Regression]

  3. Add a new chunk that adds geom_smooth() to the lemur weight plot

    ```{r echo=FALSE}
    last_plot() + geom_smooth(method = "lm")
    ```
    
  4. 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.

  1. Add "scribble" to the extras in use_xaringan_extra().

  2. Save and render your slides.

  3. On the plot slide, navigate to the Output panel. Press S to enable scribble mode. Draw a regression line on the lemur weight plot.

  4. 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"))