HW3: Ggplot2
Download and Instructions
Here’s your third set of koans. This set teaches ggplot2, the tidyverse package for drawing plots. Where dplyr answers a question with a table, ggplot2 answers it with a picture, and this homework is about learning when each one is the better answer.
There are five koans. The first is a dplyr review built around one question: a new student joins the class, and you have to guess their major from their other survey answers. The middle three teach the four plots you’ll use all term (bar plots, histograms, box plots, and scatter plots), how to add a third variable with color or facets, and how to label a plot so that someone else can read it. The last koan asks the first koan’s question again, with plots instead of tables, and has you keep score: which version answers faster?
Getting the data
As in HW2, the class survey is not inside the zip file. Each koan reads it from the web, so you need an internet connection while you work. The URL is posted on Canvas, in the Homework 3: Ggplot2 assignment (it’s the same URL as HW2). The first koan tells you exactly where to paste it. The reason the data lives behind a link only this class has is the one we discussed in HW2: anonymous is not the same as unidentifiable. Please don’t share the URL, and don’t post the data anywhere.
Setting up
First, download koans3.zip by clicking the link in the first paragraph. Unzip it, and then rename the unzipped folder so that it has your name in it, for example koans3_lastname. Do this before you open anything. Your Downloads folder already holds the earlier koans folders and their zip files, and they all look alike. Renaming right away means you always know which folder you are editing and, when you finish, which folder to zip and upload. The original koans3.zip stays in Downloads with the unedited koans in it, so if you skip this step it is easy to upload the wrong one.
Then open the project by double-clicking koans3.Rproj inside your renamed folder (not the individual .R files). If nothing happens when you double-click koans3.Rproj, open RStudio and navigate to File > Open Project, and then find koans3.Rproj inside koans3_lastname in your Downloads folder.
You set up the keyboard shortcut for running tests in HW1. If you’re on a new computer, redo that step:
Macs: Tools > Modify keyboard shortcuts > Run a test file > Cmd Shift T > Apply
Windows: Tools > Modify keyboard shortcuts > Run a test file > Ctrl Shift T > Apply
Now open koan_01_predicting_major.R in the R/ folder and paste the URL from Canvas into read_csv(), exactly as you did in HW2. Run the file top to bottom before you run any tests. The other four koans each begin with the line source("R/koan_01_predicting_major.R"), which re-runs the first koan, so whenever you restart R, that one line brings the data back.
Then work through the remaining koans in order: koan_02_ggplot_basics.R, koan_03_two_variables.R, koan_04_labels_themes.R, and koan_05_major_with_plots.R. They work the same way as HW2: each file teaches with live examples that you should run as you read, and under each question header you’ll find a copy of the most recent example, which you edit into your answer. Run your code in the console (Ctrl/Cmd + Enter) and look at the plot that appears in the Plots pane, save your edits (Ctrl/Cmd + S), and press Ctrl+Shift+T (Cmd+Shift+T on Mac) to run the tests.
The tests for a plot don’t compare pictures. They look inside the plot object you built: which geom you used, which variables are mapped to which aesthetics, whether the plot is faceted, whether it has a title. So a failing test tells you specifically what to change, and the plot still has to actually draw: a misspelled column name is reported as a failure with R’s error message. Two cautions carry over from HW2. A question whose plot you haven’t edited yet is reported as skipped, and the tests read the file as it is saved on disk. And the tests load every koan file before they run, so a syntax error in one file stops the tests for all five, with a message that points at the line.
Turning it in
Before you zip anything, run the tests one last time and read the output. Every koan must be at 100% passing, with no errors. Do not upload a submission that stops with an error, and do not upload one with failing or skipped tests. My grader runs the same tests you do, so if the tests stop on your computer they stop on mine too, and questions the tests cannot reach get no credit even if the code underneath them is right. If the tests will not run, that is the thing to fix, not something to upload and hope about. Also check that your edits are saved: the tests read the files on disk, so if the test output does not match what you see in the editor, you have unsaved changes.
When every test passes, go to your Downloads folder and find the folder you renamed, koans3_lastname. Compress that folder into a zip file (not the original koans3.zip you downloaded, and not one of the earlier homework folders). Finally, upload that zip file to Canvas for me to grade. The koans are due before class on Monday 10/19. If you get stuck, come to office hours or email to make an appointment.
Ggplot2 Notes
Check Your Understanding
The thirty multiple choice questions below cover the third set of koans: choosing a plot, bar plots and histograms, box plots and scatter plots, aesthetic mappings, and labels and themes. They don’t use the survey. Instead, each quiz works from a tiny tibble called students (four rows, five columns) that is printed at the top of every section, so you can answer every question from what’s on the screen.
