4 Figures, tables and equations

4.1 Figures made in R

Plots can be included with a chunk that makes a figure with either base plot or ggplot.

If you make the plot with ggplot, remember to print it.

```{r histogram,  fig.cap = "An embedded figure"}
p <- ggplot(penguins, aes(x = bill_length_mm)) + 
  geom_histogram()
p # remember to print the plot
```

4.1.1 Figure chunk options

There are several useful chunk options for figures, including:

  • fig.cap figure caption.
  • fig.height figure height in inches (1 inch = 25.4 mm)
  • fig.width figure width in inches

4.2 Embedding external images

Photographs and other figures that have been prepared outside of R can be included with a the path or URL of the file.

![Alt description](image.jpg)

But you have much more control with knitr::include_graphics() which lets you set a caption, adjust the size, and more.

```{r include-figure, out.width = "100%", fig.cap = "An embedded figure"}
knitr::include_graphics("my_image.png")
```

Use the out.width and out.height chunk options to set the size of the figure.

Exercise

Write a chunk to make a figure showing the annual precipitation cycle.

Hint lubridate::month() might be useful for extracting the month from the date. The date and times tutorial in biostats.tutorials gives an example of this function.

Make a second chunk to make a figure that shows how the summer of 2021 compares to previous years.

Give the figures captions.

4.3 Tables

You can make tables in markdown by hand, but it is so much easier to use R.

Simple tables can be made with the function knitr::kable. Several packages, including kableExtra and gt can make beautiful tables.

4.3.1 kable

knitr::kable(x = slice(penguins, 1:5), #the data for the table
             caption = "The top penguins" # the caption
)
Table 4.1: The top penguins
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

4.3.2 kableExtra

kableExtra extends knitr::kable, letting you add extra rows of headers, repeating headers if the table spans multiple pages and many other features.

library(kableExtra)
penguins |> 
  #rename some columns
  rename(length_mm = bill_length_mm, depth_mm = bill_depth_mm) |> 
  #replace "_" with spaces
  rename_with(str_replace_all,  pattern = "_", replacement = " ") |> 
  #capitalise first letter
  rename_with(str_to_sentence) |> 
  #get first five rows
  slice(1:5) |> 
  kbl(booktabs = TRUE, caption = "The top penguins") |> 
  kable_styling(full_width = TRUE) |>
  add_header_above(c(" ", " ", "Bill" = 2, "Body" = 2, " ", " "))
Table 4.2: The top penguins
Bill
Body
Species Island Length mm Depth mm Flipper length mm Body mass g Sex Year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

Further reading on kableExtra.

4.3.3 gt

gt is an alternative to kableExtra.

library(gt)
penguins |>
  slice(1:5) |> 
  gt(caption = "Basic gt table")
Table 4.3: Basic gt table
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007
penguins |>
  slice(1:5) |> 
  rename_with(str_to_sentence) |>
  gt(caption = "gt table with more features") |> 
  cols_label(Bill_length_mm = "Length mm",
             Bill_depth_mm  = "Depth mm",
             Flipper_length_mm = "Flipper length mm", 
             Body_mass_g = "Body mass g") |> 
  tab_spanner(label = "Bill", columns = c(Bill_length_mm, Bill_depth_mm)) |> 
  tab_spanner(label = "Body", columns = c(Flipper_length_mm, Body_mass_g)) 
Table 4.3: gt table with more features
Species Island Bill Body Sex Year
Length mm Depth mm Flipper length mm Body mass g
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007
Adelie Torgersen 36.7 19.3 193 3450 female 2007

Exercise

Write a chunk to make a table showing the mean precipitation and its standard deviation for each month.

Hint group_by() and summarise() then use any of the table making functions.

4.4 Equations

Equations are embedded in a pair of dollar symbols. RStudio will show a preview of the equation as you type it. Equations are written with LaTeX notation.

What How Output
Lower-case Greek letters $\sigma$ \(\sigma\)
Upper-Case Greek Letters $\Sigma$ \(\Sigma\)
Subscript $\beta_{0}$ \(\beta_{0}\)
Superscript $\chi^{2}$ \(\chi^{2}\)
Fractions $\frac{1}{2}$ \(\frac{1}{2}\)
Roots $\sqrt{4} = 2$ \(\sqrt{4} = 2\)


Here is an example of using an inline equation.

The $\delta^{13}C$ value ...

The \(\delta^{13}C\) value …

A double dollar enclosure gives the equation its own line. For example, this is the equation of a standard deviation that uses several different elements.

$$SD = \sqrt{\frac{\sum_{i=1}^{n}{(x_i - \bar{x})^2}}{n-1}}$$

\[SD = \sqrt{\frac{\sum_{i=1}^{n}{(x_i - \bar{x})^2}}{n-1}}\]

When making a complex formula, build one element at a time, often starting in the middle, rather than trying to get it all working at once.

4.5 Chemistry

Equations are printed in an italic font, which is not great for chemical formulae. We can fix this with the \mathrm command which forces roman typeface.

Sulphate $\mathrm{SO_4^{2-}}$

Sulphate \(\mathrm{SO_4^{2-}}\)

$$\mathrm{CO_3^{2-} + H^+ \rightleftharpoons HCO_3^{2-}}$$

\[\mathrm{CO_3^{2-} + H^+ \rightleftharpoons HCO_3^{2-}}\]