Notes N

Pretty Documents: Tables with kableExtra, Figure Captions, Cross-referencing, and Bibliographies

```{r}
#LOAD PACKAGES
library(tidyverse)
```

kableExtra for pretty tables

By default, Quarto displays data frames and matrices as you’d see them in the console and it’s kind of ugly.

```{r}
cars %>% 
  head()
```
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

There are many different packages for making tables prettier including kableExtra, gt, longtable, huxtable, stargazer and others.

In this class, we are going to focus on kableExtra since it’s relatively simple and easy to use and it works well in many output formats (.html, .pdf, etc.)

```{r}
library(kableExtra)
```

A basic pretty looking table can be accessing by applying the kbl() function:

```{r}
cars %>% 
  head() %>% 
  kbl()
```
speed dist
4 2
4 10
7 4
7 22
8 16
9 10

We can customize this even further with the kable_styling() function:

```{r}
cars %>% 
  head() %>% 
  kbl() %>% 
  kable_styling()
```
speed dist
4 2
4 10
7 4
7 22
8 16
9 10

There are many different options for kable_styling(). Below are a few:

```{r}
cars %>% 
  head() %>% 
  kbl() %>% 
  kable_styling(font_size=2)
```
speed dist
4 2
4 10
7 4
7 22
8 16
9 10

A great guide for many different customizations of tables is available at https://haozhu233.github.io/kableExtra/awesome_table_in_html.html

Captions and Cross Referencing

Figure Captions

When we include figures: - we should have a figure caption (some text describing the figure) - we should have a label for the figure. Typically, we give a figure a descriptive figure label. - The chunk label is used to generate the file name of the graphic on disk, so naming your chunks makes it much easier to pick out plots and reuse in other circumstances (e.g., if you want to quickly drop a single plot into an email). - When we render Quarto will automatically go through and number the figures in order.

```{r}
#| label: fig-one-chart
#| fig-cap: "One Chart - Cars"

plot(cars)
```

Figure 1: One Chart - Cars

You can cross-reference a figure in the text by typing referencing @fig-label. You can type @fig-one-chart for it to replace with Figure 1.

Ex: There is a positive linear relationship between speed and distance shown in Figure 1.

You can also customize figure sizing with fig-width, fig-height, fig-asp, out-width and out-height.

Two graphs side-by-side?

```{r}
#| label: fig-charts
#| fig-cap: "Two Charts"
#| fig-subcap:
#|   - "Cars"
#|   - "Pressure"
#| layout-ncol: 2

plot(cars)
plot(pressure)
```

(a) Cars

(b) Pressure

Figure 2: Two Charts

Referencing Tables in Quarto

Make tables referenceable within the text, including computational tables, by using a tbl- prefix in labels.

```{r}
#| label: tbl-cars
#| tbl-cap: "Cars"

cars %>% 
  head() %>% 
  kbl()
```
Table 1: Cars
speed dist
4 2
4 10
7 4
7 22
8 16
9 10

The first few rows of the cars dataset can be seen in Table 1 (referenced using @tbl-cars)

Caption Locations

  • Default location is above the table and below a figure. This is pretty standard in most scientific publications and for this class, you don’t need to make any changes to the default caption locations.
  • Use tbl-cap-location option to change the location to top, bottom, or margin.

Bibliography using .bib files

  1. Create a bibliography.bib.bib file

We begin by creating a.bib-file called bibliography.bib which is then filled with BibTeX entries. BibTeX entries are built in the following format and contain enough information for citation and bibliography inclusion for each literature source (book, essay, etc.).

@book{Hemingway1952,
  title={The Old Man and the Sea},
  author={Hemingway, Ernest},
  year={1952},
  publisher={Charles Scribner's Sons}
}
  • You can usually download .bib information directly from the source (the journal or article). For example: At https://academic.oup.com/bioscience/article/69/1/40/5195956 click on “Cite”

  • You can use a .bib citation generator like Bibcitation to create the reference for you

  • You can use a citation manager (like Zotero, MyBiB, etc.) and download a bibliography.bib file for all your sources at once.

  1. Add the bibliography to your Quarto document

Add bibliography: bibliography.bib to your YAML header

---
title: "Blah Blah"
author: "EMW"
bibliography: bibliography.bib
output: html_document
---
  1. Reference within your document

For example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent enim urna, dapibus et bibendum vel, consectetur et turpis. Cras a molestie nulla. [@Hemingway1952]

This will create an inline citation to that source AND include that source in a bibliography at the end of your document.

Note that any references that are not referenced within your document (but are in your bibliography) will NOT be displayed in your bibliography by default.

Other general reminders

  • make sure that all warnings and messages are hidden. This should be done by default globally if you already have the following in your YAML header (see below). Otherwise, you can add these as chunk options.
execute: 
  message: FALSE
  warning: FALSE
  • Run spellcheck on your project (see the little ABC with the green check?)