Project tips

STA 199

Bulletin

  • this ae is not due for grade.
  • exam 2 released today

Getting started

Clone your ae24-username repo from the GitHub organization.

Today

By the end of today you will practice a few quarto/markdown tricks to polish your report and simplify your presentation. Specifically we will discuss:

  • code chunk settings
  • citations
  • kable() tables
  • quarto presentations

Project logistics

  • Presentations are a required component of the final project and it’s expected all group members will be there.

  • The final project is a website.

    • You can keep or delete the “Proposal” page.
    • You can add additional pages if you wish, e.g. an “Abstract” page
    • The Presentation, Report and About pages are required.
    • You will turn in your slides, report (whole website) by pushing to GitHub
    • You will additionally turn in your report via Gradescope.

Code chunk settings

Some options available for customizing output (see quarto documenation for more detail).

Option Description
eval Evaluate the code chunk (if false, just echos the code into the output)
echo Include the source code in output
warning Include warnings in the output
message Whether to preserve messages emitted by message() (similar to the option warning)
include Catch all for preventing any output (code or results) from being included (e.g. include: false suppresses all output from the code block)

These options can be applied globally (the whole document) or locally (a specific code chunk). Global settings are controlled in the YAML (see the top of the document) while local code chunk options can be applied with #| (see example below).

Exercise 1

In the code chunk below:

  • set warning to false
  • set echo to false

and re-render.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
✔ ggplot2 3.3.5      ✔ purrr   1.0.1 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.1.4      ✔ stringr 1.4.0 
✔ readr   2.1.1      ✔ forcats 0.5.1 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(knitr)

In addition to code chunks, figures have settings as well.

We can set captions and an alt attributes using #| fig-cap: and #| fig-alt: respectively. alt captions specify “alternate text” for an image. Alternative text appears if an image cannot be displayed and is also read by screen-readers.

Additional figure options include

Option Description
fig-width figure width in inches
fig-height figure height in inches
fig.align e.g. fig.align: center centers figure alignment
fig.asp changes figure height based on aspect ratio with width
out.width sets figure width relative to text (1000 = 100% text width), e.g. out.width: 1000

In all cases above, we can again set options locally or globally. Note: local options override global options.

Exercise 2

Add a figure caption to the figure below. Next, change the output width to be 50% of the text. Finally, align the figure with the center of the page.

starwars %>%
  ggplot(aes(x = height)) +
  geom_density() +
  labs(x = "Height (cm)", y = "Density") +
  theme_bw()
Warning: Removed 6 rows containing non-finite values (stat_density).

Project specific notes

For the project, you will set the option echo: FALSE and warning: FALSE to hide all code and warnings in your final report.

Suggestion: make your figures consistently themed, e.g. use similar figure size/aspect ratio and color scheme throughout your report. Change the default gray background, see themes.

Exercise 3

Change the global code chunk settings so the document is formatted as your final project will be. Render and take a look at the updated PDF.

Citations

Your report will include citations, e.g. the data source, previous research, and other sources as needed. At a minimum, you should have a citation for the data source.

All of your bibliography entries will be stored in a .bib file. The entries of the bibliography are stored using BibTex, i.e., a format to store citations in LaTeX. Let’s take a look at references.bib.

In addition to the .bib file:

  • Include bibliography: references.bib in the YAML.
  • At the end of the report, include ## References. This will list all of the references at the end of the document.

Citation examples

  1. In Wickham, Chang, and Wickham (2016), the authors focus present the grammar of graphics package ggplot2 for R.

  2. Within the grammar of graphics, ggplot() is the first layer of any plot (Wickham, Chang, and Wickham 2016).

Exercise 4

  • Add a citation for tidytuesday to this document. Hint: check out the tidytuesday GitHub page.

Neat kable table

  • Calculate the mean, median, and standard deviation of mass. Display the results.

Exercise 5

# code here
  • Let’s neatly display the results using the kable function from the knitr package. We will
    • Display results to 2 decimal places
    • Customize column names
    • Add a caption
## add code

Presentations (demo)

Wickham, Hadley, Winston Chang, and Maintainer Hadley Wickham. 2016. “Package ‘Ggplot2’.” Create Elegant Data Visualisations Using the Grammar of Graphics. Version 2 (1): 1–189.