library(gapminder)
Examples
Data
Load the gapminder
data:
Examples
This section includes code for the examples shown. These may differ slightly from the examples shown in the live demonstration.
Example 1: Parameterized plots with ggplot2
See example
Let’s make a plot of how the life expectancy has changed in Asia over time. We start by filtering the data:
library(dplyr)
<- gapminder |>
gapminder_asia filter(continent == "Asia")
We then make a plot:
library(ggplot2)
ggplot(data = gapminder_asia) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
+
) labs(
x = NULL, y = "Life Expectancy (years)",
title = "Life Expectancy in Asia"
)
If we wanted to create the same plot but for Europe, what would we need to change?
- The value that the data is filtered on
- The text in the title
Let’s create a variable for the continent:
<- "Asia" choose_continent
and then use that value in our code:
# Filter data
<- gapminder |>
gapminder_continent filter(continent == choose_continent)
# Plot
library(glue)
ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
+
) labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {choose_continent}")
)
This makes it easier to turn it into a function:
<- function(choose_continent, data = gapminder) {
continent_plot # Filter data
<- data |>
gapminder_continent filter(continent == choose_continent)
# Plot
<- ggplot(data = gapminder_continent) +
g geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
+
) labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {choose_continent}")
)return(g)
}
Check it works:
continent_plot("Europe")
Example 2: Parameterized reports with Quarto
See example
Here’s a file, example.qmd
, we’ll use as a template for our report:
---
title: "Gapminder Report on Life Expectancy in Asia"
execute:
echo: false
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in Asia has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
# Edit the code below with your plot function!
plot(1:10, 1:10)
```
Let’s start by editing the example.qmd
file to add a document parameter for the continent.
---
title: "Gapminder Report on Life Expectancy in Asia"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in Asia has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
# Edit the code below with your plot function!
plot(1:10, 1:10)
```
Then, we add our plotting function, and pass in the parameter:
---
title: "Gapminder Report on Life Expectancy in Asia"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in Asia has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
continent_plot <- function(choose_continent, data = gapminder) {
# Filter data
gapminder_continent <- data |>
filter(continent == choose_continent)
# Plot
g <- ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
) +
labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {params$choose_continent}")
)
return(g)
}
continent_plot(params$choose_continent)
```
We can use the parameter multiple times e.g. to add a table
---
title: "Gapminder Report on Life Expectancy in Asia"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in Asia has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
continent_plot <- function(choose_continent, data = gapminder) {
# Filter data
gapminder_continent <- data |>
filter(continent == choose_continent)
# Plot
g <- ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
) +
labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {params$choose_continent}")
)
return(g)
}
continent_plot(params$choose_continent)
```
Adding a table of data:
```{r}
#| label: tbl-stats
#| tbl-cap: "Statistics in Asia"
library(tinytable)
gapminder |>
filter(continent == params$choose_continent) |>
group_by(year) |>
summarise(across(c(lifeExp, pop, gdpPercap), mean)) |>
tt()
```
Example 3: Parameterized text with Quarto
See example
Add inline code:
The number of rows is `r nrow(gapminder)`
.
which displays as:
The number of rows is 1704.
We can apply this to our document and use the parameters inline:
---
title: "Gapminder Report on Life Expectancy in Asia"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in `r params$choose_continent` has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
continent_plot <- function(choose_continent, data = gapminder) {
# Filter data
gapminder_continent <- data |>
filter(continent == choose_continent)
# Plot
g <- ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
) +
labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {params$choose_continent}")
)
return(g)
}
continent_plot(params$choose_continent)
```
Including in the title:
---
title: "Gapminder Report on Life Expectancy in `r params$choose_continent`"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in `r params$choose_continent` has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: "Life expectancy in Asia"
continent_plot <- function(choose_continent, data = gapminder) {
# Filter data
gapminder_continent <- data |>
filter(continent == choose_continent)
# Plot
g <- ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
) +
labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {params$choose_continent}")
)
return(g)
}
continent_plot(params$choose_continent)
```
We edit the figure caption slightly differently:
---
title: "Gapminder Report on Life Expectancy in `r params$choose_continent`"
execute:
echo: false
params:
choose_continent: "Europe"
---
```{r}
#| label: load-pkgs
#| message: false
library(gapminder)
library(dplyr)
library(ggplot2)
library(glue)
```
[Gapminder](https://www.gapminder.org/data/) data. @fig-life-exp shows a box plot of how the life expectancy in `r params$choose_continent` has changed over time.
This is a report using the
```{r}
#| label: fig-life-exp
#| fig-cap: !expr 'paste("Life expectancy in ", params$choose_continent)'
continent_plot <- function(choose_continent, data = gapminder) {
# Filter data
gapminder_continent <- data |>
filter(continent == choose_continent)
# Plot
g <- ggplot(data = gapminder_continent) +
geom_boxplot(
mapping = aes(x = factor(year), y = lifeExp)
) +
labs(
x = NULL, y = "Life Expectancy (years)",
title = glue("Life Expectancy in {params$choose_continent}")
)
return(g)
}
continent_plot(params$choose_continent)
```
Example 4: Rendering multiple reports
See example
Render one report using the quarto
R package:
library(quarto)
quarto_render(
input = "example.qmd",
execute_params = list(
continent = "Europe"
) )
Use the purrr
package to create multiple reports at the same time:
library(purrr)
walk(
.x = unique(gapminder$continent),
.f = ~quarto_render(
input = "example.qmd",
execute_params = list(
choose_continent = .x
)
) )
Why is there only one report? We need to give each report a different name, to stop it being overwritten. Let’s use glue()
again, this time for the file names.
walk(
.x = unique(gapminder$continent),
.f = ~quarto_render(
input = "example.qmd",
output_file = glue("report_{.x}.html"),
execute_params = list(
choose_continent = .x
)
) )
To ensure the images aren’t overwritten as well, you may need to add embed-resources: true
to your Quarto document YAML:
---
format:
html:
embed-resources: true
---
This keeps any plots created embedded within the HTML document, instead of in a separate folder.