Stepping Stones

Ideas

A new example structure for the Stepping Stones measures, based on Team 5’s suggestions from last week.

Example

Schedule updates

What we’ll be doing in class the next three weeks

  • 2/16: Deeper dive into R, ggplot (3:35-5:00), R markdown (5:10-5:50)
    • assignment for next week will include some individual practice with ggplot
    • team progress on stepping stones will include creating the first version of an R markdown file to present your measures
  • 2/23: Discussion of reading from 2/16 and 2/23 (3:30-5:00)1 and time to work with your teams (5:00-6:00)
    • assignment for next week will include individually completed peer review of another team’s presentation based on the R markdown file submitted on 2/23 (submitted by Tuesday, 2/28)
    • team updates on stepping stones will aim for a nearly complete markdown file presenting your measures and final scripts to acquire and prepare the data
  • 3/2: Presenting the nearly-final work to the class (3:30-4:30), with time to incorporate additional feedback or work through remaining challenges (4:30-6:00)

ggplot2: customization

ggplot breaks up the task of making a graph into a series of distinct tasks (layering); each task is carried out in code based on identifiable functions (geom_, scale_, labs, legends, and more).

Customizing scales

Scale functions control the mapping from data to aesthetics and every aesthetic has a default scale.

All scale functions have a common naming scheme: scale_name of aesthetic_name of scale

  • scale_y_continuous: assign breaks, labels, limit, transformations
  • scale_color_few: assign color palettes from pre-defined palete (few)
  • scale_fill_manual: assign identified color palettes to fill

Scale functions can use functions from the scales package.

  • e.g., label functions – label_date(), label_dollar(), label_percent(), label_comma(), label_wrap() – control how labels are printed on the axes

Colors

Great resources on colors in R

Themes

Themes control the appearance of a plot – background color, borders, gridlines, text size and font, placement of titles/legends, etc.

Annotations

Annotations add custom features to a plot, like text or shading

  • annotate() - not a geom (doesn’t accept mappings/things that vary) but can use geoms (text, point, segment, etc.) to add text or rectangles or arrows to a plot
  • geom_text() - adds text to a plot instead of, or in addition to, points or lines (e.g., for labeling point values)

R Markdown

R Markdown creates dynamic documents by combining markdown (an easy to write plain text format) with embedded R code chunks. When compiled, the code can be evaluated so that the code, its output, and your prose can be included in the final document to make reports reproducible.

  • R Markdown documents (.Rmd files) can be rendered to multiple formats including HTML and PDF.
  • The R code in an .Rmd document is processed by knitr, while the resulting .md file is rendered by pandoc to the final output formats (e.g. HTML or PDF).

R Markdown files

The .Rmd file contains

  • A YAML header (yet-another-markup-language), offset by ----
    • A good review of YAML headers
    • Should include: Title/subtitle, author, date
    • Additional arguments: (Floating) Table of contents and code folding
----
title: "An incredible project"
subtitle: "Of great significance"
author: "Don't you know who I am?"
date: "2023-02-16"
output:
  html_document:
    toc: true
    toc_depth: 4
    toc_float: true
    code_folding: hide
----
  • Text with markdown formatting
    • A markdown format cheatsheet
    • Common formatting: adding headers, adding emphasis, creating lists, creating links, etc.
  • Chunks of R code, offset by ``` (keyboard shortcut: Cmd/Ctrl + Alt + I)
    • knitr code chunk options
    • name (to identify code chunk)
    • echo = TRUE (show code in document)
    • message = FALSE, warning = FALSE (prevent messages, warnings from appearing)
    • fig.height = X, fig.width = X, fig.cap = "caption below the figure" (control size of generated figure, presence of figure caption)
    • for example
    ```{r codechunkname, warning = FALSE, fig.height = 10}
    # add code here
    ```
    • knitr::opts_chunk$set() (set global options, usually in first code chunk)

tabset

You can create tabs in an Rmarkdown document that will be rendered as an html file. This allows readers to view the content of different sections by clicking the tab titles instead of scrolling back and forth on the page.

To turn markdown sections into tabs, add {.tabset} on the right of a header. All of the content in subheaders below this header will be contained in separate tabs. That is, adding the .tabset attribute to a level-2 header (##) will convert all subsequent level-3 headers (###) to tabs.

R Markdown Resources

Let’s Play with R!

First go to slack and copy the practice script for today (week5script.R) into your weeklymaterials/scripts folder. Then open an RStudio session using the weeklymaterials.Rproj file.



  1. Which also means if you’d like to add or update annotations for the reading assigned for today, 2/16, you may do so until 2pm on Tuesday 2/23.↩︎