Single numbers and parts of a whole

Materials for class on Tuesday, September 12, 2017

Contents

Slides

Download the slides from today’s lecture

First slide

Making a bar chart in Excel

Salt Lake City does a fairly good job of providing open data for public use. The Sunlight Foundation and Code for America have a database of municipal data and rate cities by how accessible and available their data is. Their US City Open Data Census is a fantastic resource.

We’ll create this chart in Excel, based on publicly available data from Salt Lake City.

Things to download:

SLC PD traffic stops example

Introduction to R

Introduction to Markdown and R Markdown

Bonus extended example: Creating a production-quality SLCPD chart in R

Salt Lake City Police Department badge

Previously, we used R ggplot2 to create analysis-ready graphics. We can create production-ready graphics using the same tools. Here’s an example of a typical workflow:

You can also type View(crimes) to look at the data in RStudio.

First, check that the data loaded. You can (and should) also click on the crimes object in the Environment panel in RStudio.

## Observations: 61,295
## Variables: 8
## $ CASE                  <chr> "SL2012221534", "SL201246361", "SL201215...
## $ `OFFENSE CODE`        <chr> "3605-0", "3601-0", "5707-0", "2305-0", ...
## $ `OFFENSE DESCRIPTION` <chr> "SEXUAL OFFENSE", "SEXUAL OFFENSE", "INV...
## $ `REPORT DATE`         <chr> "12/31/2012 03:37:00 PM", "02/06/2012 10...
## $ `OCC DATE`            <chr> "01/01/2012 12:00:00 AM", "01/01/2012 12...
## $ `DAY OF WEEK`         <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ LOCATION              <chr> "1500 W 800 S", "200 N 200 W", "1300 S S...
## $ COUNCIL               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...

In Excel, we used a PivotTable to summarize the data by adding how many crimes of each type happened throughout the year. We can do the same with R, using the dplyr package (which is loaded automatically when you run library(tidyverse)). Note that OFFENSE DESCRIPTION is between backticks. Ordinarily, you don’t need to do this when using variable names, but in this case, there’s a space in the name and that breaks everything. The `s are R’s way of quoting variable names.

Also in Excel, we only graphed the observations where the count was greater than 1,000. We can filter the data the same way here with filter(). View the crimes_types object when you’re done.

## # A tibble: 13 x 3
##    `OFFENSE DESCRIPTION` number percent
##    <chr>                  <int>   <dbl>
##  1 ASSAULT                 4203  0.0686
##  2 BURGLARY                1868  0.0305
##  3 DAMAGED PROP            3141  0.0512
##  4 DRUGS                   2392  0.0390
##  5 ESCAPE                  3717  0.0606
##  6 FRAUD                   1164  0.0190
##  7 INV OF PRIVACY          2880  0.0470
##  8 LARCENY                11394  0.186 
##  9 LIQUOR                  2023  0.0330
## 10 PUBLIC ORDER            9294  0.152 
## 11 PUBLIC PEACE            4906  0.0800
## 12 STOLEN VEHICLE          2273  0.0371
## 13 TRAFFIC                 8541  0.139

This data is sorted alphabetically by default, since we grouped the data by OFFENSE DESCRIPTION, but sorting it by the count or percent is easy. Note the final line with fct_inorder—this creates a new column called crime that is a factor, or a categorical variable. If we plot the OFFENSE DESCRIPTION variable, R will order it alphabetically. Transforming it into an ordered factor will make R plot the categories in the correct order.

## # A tibble: 13 x 4
##    `OFFENSE DESCRIPTION` number percent crime         
##    <chr>                  <int>   <dbl> <ord>         
##  1 LARCENY                11394  0.186  LARCENY       
##  2 PUBLIC ORDER            9294  0.152  PUBLIC ORDER  
##  3 TRAFFIC                 8541  0.139  TRAFFIC       
##  4 PUBLIC PEACE            4906  0.0800 PUBLIC PEACE  
##  5 ASSAULT                 4203  0.0686 ASSAULT       
##  6 ESCAPE                  3717  0.0606 ESCAPE        
##  7 DAMAGED PROP            3141  0.0512 DAMAGED PROP  
##  8 INV OF PRIVACY          2880  0.0470 INV OF PRIVACY
##  9 DRUGS                   2392  0.0390 DRUGS         
## 10 STOLEN VEHICLE          2273  0.0371 STOLEN VEHICLE
## 11 LIQUOR                  2023  0.0330 LIQUOR        
## 12 BURGLARY                1868  0.0305 BURGLARY      
## 13 FRAUD                   1164  0.0190 FRAUD
##  [1] LARCENY        PUBLIC ORDER   TRAFFIC        PUBLIC PEACE  
##  [5] ASSAULT        ESCAPE         DAMAGED PROP   INV OF PRIVACY
##  [9] DRUGS          STOLEN VEHICLE LIQUOR         BURGLARY      
## [13] FRAUD         
## 13 Levels: LARCENY < PUBLIC ORDER < TRAFFIC < PUBLIC PEACE < ... < FRAUD

With the data summarized and sorted, we can finally plot it. We’ll go over each of the pieces of this in class, so don’t worry if it looks intimidating—this is the final product. The basic gist of what’s happening is that we take a ggplot() object, map data to aesthetics (in this case x and y mapped to the percent variable and the crime variable), and then add a sequence of layers that determine how that data is plotted.

Highlighting the traffic bar is a little tricky, since we can’t select it with the mouse and change the color by hand. The easiest way to do it is to create a new variable to map onto the fill color of each bar. We then remove the legend and modify the colors with guides() and scale_fill_manual():

Finally, adding the label is a little tricky too, since we can’t manually type into the plot. Here, we add yet another variable to the data frame, this time for the text that will show up in the label. We only want to label the bars where highlight == TRUE, so we use an ifelse() statement to do that. We also save the plot to a variable instead of just plotting directly (i.e. instead of running ggplot(), we assign it to plot_crimes)

Finally, we can save this plot as a file using the ggsave() function. The plot should save just fine as a PNG file. Saving as a PDF is slightly trickier, though, since PDFs have embedded fonts. R’s default PDF writer can only embed a handful of PDF fonts, but R has a second PDF writer based on the Cairo graphics library that can embed fonts just fine. idk why ¯\_(ツ)_/¯

R is just weird sometimes. You definitely don’t need to understand the details of how Cairo works—I don’t. All that matters is that when you use the Cairo PDF library, fonts are embedded properly and everything works.

You just have to specify the plotting device in ggsave() with device = cairo_pdf.

Using the Cairo library for PNGs can also be helpful. R sometimes generates wonky PNGs in Windows, and Cairo PNGs work better in PowerPoint and Word. Saving Cairo-based PNGs with ggsave() has a slightly different syntax (use type = "cairo" instead of device = cairo_pdf):

Feedback for today

Go to this form and answer these three questions (anonymously if you want):

  1. What new thing did you learn today?
  2. What was the most unclear thing about today?
  3. What was the most exciting thing you learned today?