```{r}
# label: fighistogram
# figcap: "An embedded figure"
# figalt: "A histogram of plant heights made with ggplot2"
# message: false
# warning: false
< ggplot(trait_height, aes(x = Value)) +
p geom_histogram(boundary = 0) +
labs(x = "Height cm")
# remember to print the plot
p ```
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.
4.1.1 Figure code block options
There are several useful block options for figures, including:

figcap
figure caption. 
figalt
alternate text to improve accessibility 
figheight
figure height in inches (1 inch = 25.4 mm) 
figwidth
figure width in inches
4.2 Embedding external images
In the visual editor, photographs and other figures that have been prepared outside of R can be included with the insert tool by typing “/” on a blank line and choosing “Figure/Image”. This will open an menu to get the path to the image and set the caption etc. Once you close the menu, you can set the figure size. This will generate a bit of markdown that looks like.
![Marine diatoms](Pics/Marine_diatoms_SEM2.jpg){figalt="SEM photograph of marine diatoms" width="491"}
Alternatively, you can use knitr::include_graphics()
in a regular code block.
```{r}
# label: figincludefigure
# outwidth: "491px"
# figcap: "An embedded figure of diatoms"
# figalt: "An embedded figure of diatoms"
::include_graphics("Pics/Marine_diatoms_SEM2.jpg")
knitr```
Use the outwidth
and outheight
chunk options to set the display size of the figure.
4.3 Tables
You can make tables in markdown by hand (the / insert tool helps a lot), but it often 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.
The block label need to start with tbl
if the main caption and table number are to be shown.
4.3.1 kable
```{r kable}
# label: tblkable
# tblcap: Simple tables with `kable()`
< trait_height >
trait_height_summary group_by(Taxon) >
summarise(mean = mean(Value), sd = sd(Value), n = n()) >
filter(n > 10)
::kable(trait_height_summary) # show first 5 rows
knitr```
Taxon  mean  sd  n 

bistorta vivipara  3.749351  1.813518  77 
carex rupestris  3.713636  1.344228  22 
cassiope tetragona  8.093750  2.450978  16 
dryas octopetala  2.762222  1.540704  45 
equisetum arvense  4.068421  4.706184  38 
equisetum scirpoides  1.791667  1.454425  60 
festuca rubra  7.042105  2.094256  19 
poa arctica  7.867647  2.326360  34 
salix polaris  2.041667  1.102590  72 
4.3.2 gt
The gt
package can make more elaborate tables than knitr::kable()
.
```{r}
# label: tblgt
# echo: fenced
# tblcap: Tables with `gt()`
# tblsubcap:
#  "Basic `gt()` table"
#  "`gt()` table with more features"
library(gt)
>
trait_height_summary gt()
>
trait_height_summarygt() >
cols_label(mean = "Mean", # Display column headers
sd = "Standard deviation",
n = "Number") >
fmt_number(columns = c(mean, sd)) >
tab_spanner(label = "Height", columns = c(mean, sd))
```
Taxon  mean  sd  n 

bistorta vivipara  3.749351  1.813518  77 
carex rupestris  3.713636  1.344228  22 
cassiope tetragona  8.093750  2.450978  16 
dryas octopetala  2.762222  1.540704  45 
equisetum arvense  4.068421  4.706184  38 
equisetum scirpoides  1.791667  1.454425  60 
festuca rubra  7.042105  2.094256  19 
poa arctica  7.867647  2.326360  34 
salix polaris  2.041667  1.102590  72 
Taxon  Height  Number  

Mean  Standard deviation  
bistorta vivipara  3.75  1.81  77 
carex rupestris  3.71  1.34  22 
cassiope tetragona  8.09  2.45  16 
dryas octopetala  2.76  1.54  45 
equisetum arvense  4.07  4.71  38 
equisetum scirpoides  1.79  1.45  60 
festuca rubra  7.04  2.09  19 
poa arctica  7.87  2.33  34 
salix polaris  2.04  1.10  72 
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 

Lowercase Greek letters  $\sigma$ 
\(\sigma\) 
UpperCase 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}}{n1}} $$
\[ SD = \sqrt{\frac{\sum_{i=1}^{n}{(x_i  \bar{x})^2}}{n1}} \]
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
LaTeX 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}}\]