0.1 Announcements

0.2 Today’s Lessons

Today’s lessons are:

0.3 Resources

Concepts from today’s class (and next class) are closely mirrored by the following resources, which introduce ggplot2, although are organized in different ways for each.

The following are good walk-throughs that introduce ggplot2:

Here are some other resources you might find useful:

0.4 Participation

To get participation points for today, we’ll be filling out the cm006-exercise.Rmd file, and adding it to your participation repo.

Let’s get set up:

  1. To download the file the easy way, just click the upper-right drop-down menu of the html version of the file.
  2. Download the .Rmd version to your local participation repo.
  3. Open the .Rmd file, and change the output type to html_document, keeping the .md file, too.
  4. Optional, but recommended: Stage and commit the Rmd file (you can do this through RStudio).
    • You can still get participation marks by uploading the final files to GitHub.

1 Orientation to statistical graphics

1.1 Learning Objectives

By the end of this lesson, students are expected to be able to:

  • Identify the plotting framework available in R
  • Have a sense of why we’re learning the ggplot2 tool
  • Have a sense of the importance of statistical graphics in communicating information

1.2 Discussion

There are three main ways you can produce graphics in R. In order of inception, they are

  • base R
  • lattice (an R package)
  • ggplot2 (an R package)
    • Part of the tidyverse

Why ggplot2?

  • once fluent, can make most plots up to publication quality standard very quickly.
  • has theoretical underpinning in the “layered grammar of graphics”, which is described in the book by Leland Wilkinson.

Stackoverflow was my main source of learning. Google what you’re trying to do, and persevere. You can do it.

Jenny Bryan on statistical graphics:

  • A picture is worth 1000 words
  • More philosophy on graphics
  • Comparing base R/lattice/ggplot2
  • The learning curve

2 The grammar of graphics

2.1 Learning Objectives

By the end of this lesson, students are expected to be able to:

  • Identify the seven components of the grammar of graphics underlying ggplot2
  • Have a sense of what the seven components are

2.2 Discussion

Leland Wilkinson lays out the grammar of graphics in his book.

They define the “space of statistical graphics”.

The grammar components, adapted to ggplot2 (gg = grammar of graphics), where the bold ones are necessary to specify for every plot:

  • Data
  • Aesthetic mappings
  • Geometric objects
  • Scales
  • Statistical transformations
  • Coordinate system
  • Facet specification

3 Plotting with x and y aesthetics

3.1 Learning Objectives

This live-coding-based lesson focusses on:

  • using the x and y aesthetic mappings, while
  • using different geometric objects to explore various plot “types”.

By the end of this lesson, students are expected to be able to:

  • Create a variety of “plot types” using ggplot2 under the following situations:
    • two numeric variables
    • one numeric variable
    • one numeric, one categorical variable

3.2 Demonstration

Note: We’ll be jumping straight into the ggplot() function, instead of the more limited qplot() function.

Follow along with the cm006-exercise.Rmd file we downloaded earlier.

LS0tCnRpdGxlOiAiU1RBVCA1NDUgQ2xhc3MgTWVldGluZyAwNjogSW50cm9kdWN0aW9uIHRvIHBsb3R0aW5nIHdpdGggYGdncGxvdDJgIgpvdXRwdXQ6CiAgICBodG1sX25vdGVib29rOgogICAgICAgIHRvYzogdHJ1ZQogICAgICAgIHRoZW1lOiBjZXJ1bGVhbgogICAgICAgIG51bWJlcl9zZWN0aW9uczogdHJ1ZQotLS0KCmBgYHtyLCBlY2hvPUZBTFNFfQpzdXBwcmVzc1BhY2thZ2VTdGFydHVwTWVzc2FnZXMobGlicmFyeSh0aWR5dmVyc2UpKQpzdXBwcmVzc1BhY2thZ2VTdGFydHVwTWVzc2FnZXMobGlicmFyeShnYXBtaW5kZXIpKQprbml0cjo6b3B0c19jaHVuayRzZXQoZWNobz1GQUxTRSwgZmlnLndpZHRoPTUsIGZpZy5oZWlnaHQ9MywgZmlnLmFsaWduPSdjZW50ZXInKQpgYGAKCiMjIEFubm91bmNlbWVudHMKCi0gTm8gYnJlYWsgdG9kYXkuCgojIyBUb2RheSdzIExlc3NvbnMKClRvZGF5J3MgbGVzc29ucyBhcmU6CgotIE9yaWVudGF0aW9uIHRvIHN0YXRpc3RpY2FsIGdyYXBoaWNzCi0gVGhlIGdyYW1tYXIgb2YgZ3JhcGhpY3MKLSBQbG90dGluZyB3aXRoIGB4YCBhbmQgYHlgIGFlc3RoZXRpY3MKCiMjIFJlc291cmNlcwoKQ29uY2VwdHMgZnJvbSB0b2RheSdzIGNsYXNzIChhbmQgbmV4dCBjbGFzcykgYXJlIGNsb3NlbHkgbWlycm9yZWQgYnkgdGhlIGZvbGxvd2luZyByZXNvdXJjZXMsIHdoaWNoIGludHJvZH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