Customizing Quarto reports- SUGGESTED ANSWERS

Packages

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Attaching package: 'kableExtra'

The following object is masked from 'package:dplyr':

    group_rows
Loading required package: usethis
library(grid)

We Love Penguins

We are going to use the famous penguins data set to practice fixing non-data aestetics + practice with patchwork.

Below, make the following three plots with appropriate labels. Name them p1, p2, and p3.

  • Scatterplot between bill length and flipper length

  • Side-by-side boxplot between Species and bill length

  • Overlapping histogram of flipper length by sex

p1 <- penguins |>
  ggplot(
    aes(x = bill_length_mm, y = flipper_length_mm)
  ) + 
  geom_point()

p2 <- penguins |>
  ggplot(
    aes(x = species, y = bill_length_mm)
  ) + 
  geom_boxplot()

p3 <- penguins |>
ggplot(
  aes(x = flipper_length_mm , fill = sex)
) + 
  geom_histogram()

On p1, use theme to change the color of the text of the x-axis grey (“grey20) and increase the size of the text.

p1 + theme(axis.title.x = element_text(color = "blue", angle = 60 , vjust = 0.5, face = "bold" , size = 20 ))

Now, on p1 play around with the angle, hjust, vjust, and face arguments.

# insert code here

Now, let’s use the functions within patchwork to help manipulate the format of these plots in our document. Let’s start by adding p1 and p2 together in the chunk below. Let’s render the document and see what happens.

p1 + p2 
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).

What if we want to add some really important text next to our plot? We can do that using textGrob within the grid package. Add some text next to p1

p1 + textGrob("Some Text")
Warning: Removed 2 rows containing missing values (`geom_point()`).

textGrob("Some Text") + p1
NULL

We can use wrap_elements to put the text on the left side!

wrap_elements(textGrob("Text on the left")) + p1
Warning: Removed 2 rows containing missing values (`geom_point()`).

Stacking Plots

We used the + sign above to put plots next to each other. We can also use the | sign. We can use the / sign to stack plots. Let’s create a grid of our plots that has p1 on top and the other two on the bottom.

patchwork <- p1 / (p2 + p3)

Assume now that you want to add theme_bw to each plot. p1, p2, and p3 are still treated as ggplot objects. However, instead of adding theme_bw to each plot, let’s save our above code as patchwork and add theme_minimal to that!

patchwork & theme_dark() 
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 2 rows containing non-finite values (`stat_bin()`).

Tagging

Often, especially in scientific literature, multiple plots are collected in a single figure and referred to by a tag. We can use plot_annotation and tag_levels to do this. See the following code below:

patchwork + plot_annotation(tag_levels = 'A') & 
  theme(plot.tag = element_text(size = 8))
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 2 rows containing non-finite values (`stat_bin()`).

We know how to add titles to each plot individually. What if we want to title the group of plots we created?

patchwork + 
  plot_annotation(title = 'Crazy Penguins')
Warning: Removed 2 rows containing missing values (`geom_point()`).
Warning: Removed 2 rows containing non-finite values (`stat_boxplot()`).
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 2 rows containing non-finite values (`stat_bin()`).

This lesson was inspired by: https://patchwork.data-imaginist.com/