October 26, 2019

Class overview

  • Complete data visualization module with a couple of exercises
  • Test ability to create PPTX
  • Develop skills to create and format 3-slide PPTX
  • Work on the R workshop assignment

Your R workshop assignment

  • Create a 3-slide PPTX
    • Include a title slide
    • Include two other slides. One slide should have a chart. The other slide can have a chart or text.
    • Submit the 3-slide PPTX to me by email on or before October 28, 2019

Wrapping up data visualization

But first, read your data

library(tidyverse)
crime <- read_csv('https://bit.ly/2mcZLq4') %>% as.data.frame()
contr <- read_csv('https://bit.ly/2lQySrQ') %>% as.data.frame()

Exercise - 5 minutes

  • Create a scatter plot using the crime dataset
  • Your x- and y-axes should be reported_date and occurred_date
  • The point colors should represent precinct
  • Exclude all NA values from precinct
  • Use a theme
crime %>% 
  filter(! precinct %in% NA) %>%
  ggplot(aes(reported_date, occurred_date, color = precinct)) + 
  geom_point() + 
  theme_hc()

Exercise - 5 minutes

Exercise - 12 minutes

  • Create a chart from the crime dataset that shows at minimum the distribution of crimes by a categorical variable (crime_subcategory, primary_offense_description, neighborhood, precinct) and whether or crime occurred in the AM or PM (first half or second half of the day)
  • You can create a bar, line or point plot
  • Create a new variable with mutate() called occurred_time_ampm that tells whether an incident occurred in the AM or PM.
  • Make sure your axes are labeled correctly
  • Give your chart a title
  • Use occurred_time_ampm in the color or fill arguments in aes()
  • Use a pre-built theme

Exercise - 12 minutes

Exercise - 12 minutes

crime %>% 
  mutate(occurred_time_ampm = ifelse(occurred_time >= 1200, 'PM', 'AM')) %>% 
  filter(
    ! occurred_time_ampm %in% NA & 
    ! crime_subcategory %in% NA
    ) %>%
  group_by(crime_subcategory, occurred_time_ampm) %>%
  summarise(n = n()) %>%
  ungroup %>%
  group_by(crime_subcategory) %>%
  mutate(total = sum(n, na.rm = TRUE)) %>%
  filter(total >= 100) %>%
  ggplot(aes(reorder(crime_subcategory, n), n, fill = occurred_time_ampm)) + 
  geom_bar(stat = 'identity') + 
  coord_flip() + 
  theme_wsj() + 
  scale_colour_wsj() + 
  labs(title = 'Most incidence occur\nin the AM', fill = 'Time of Day')

Reporting with officer

Brief overview of officer

  • officer allows you to create and manipulate/update PPTXs
    • Creating and manipulating Word documents is also possible
  • Historically, R-Microsoft Office integration has been poor. In previous iterations of this course, students created PDFs using R Markdown and knitr.
  • officer makes PowerPoint deck creation easy with pre-built functions
    • Functions gives you lots of control over the deck and the ability to create different slide types and write headers, body elements, and footers
    • Functions make it possible to add charts and tables to slides
    • Easy to import PPTX templates
  • Resources

Test ability to create PPTX

  • Open a new R Notebook (File > New File > R Notebook)
  • Save the file
  • ‘Clean’ the file (remove the superfluous stuff)
  • Set your working directory
  • Paste the forthcoming code into a code chunk and run
getwd()

# Mac users: command + i, copy path
# PC users: ctrl+shift+right click, copy path
setwd('/Users/graham/Dropbox/r_course_evans_school') 

Test ability to create PPTX

  • Open a new R Notebook (File > New File > R Notebook)
  • Save the file
  • ‘Clean’ the file (remove the superfluous stuff)
  • Set your working directory
  • Paste the forthcoming code into a code chunk and run
# install.packages(c('officer', 'magrittr'))
library(officer)

read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>% 
  print(target = 'my_presentation.pptx')

Creating pptx deck and slides

Creating pptx deck and slides

  • pptx objects are stored like any other data object in R
  • Use the assignment operator (<-) to create a pptx data object
  • read_pptx() creates a pptx data object in R
pres <- read_pptx()

Creating pptx deck and slides

  • The pptx data object contains many types of information
    • Number of slides
    • Layout options for new slide types
pres
## pptx document with 0 slide(s)
## Available layouts and their associated master(s) are:
##              layout       master
## 1       Title Slide Office Theme
## 2 Title and Content Office Theme
## 3    Section Header Office Theme
## 4       Two Content Office Theme
## 5        Comparison Office Theme
## 6        Title Only Office Theme
## 7             Blank Office Theme

Creating pptx deck and slides

  • The pptx data object contains many types of information
    • Number of slides
    • Layout options for new slide types
layout_summary(pres)
##              layout       master
## 1       Title Slide Office Theme
## 2 Title and Content Office Theme
## 3    Section Header Office Theme
## 4       Two Content Office Theme
## 5        Comparison Office Theme
## 6        Title Only Office Theme
## 7             Blank Office Theme
length(pres)
## [1] 0

Creating pptx deck and slides

  • layout_properties() gives you a more detailed view of layout options for slides
  • Multiple lines per slide layout
  • 1 line per layout element
    • type, id, info about element placement and size on slide
pres %>% layout_properties(layout = "Title Slide", master = "Office Theme") %>% 
  head(3) %>% select(-ph)
##     master_name        name   type id                   ph_label     offx
## 9  Office Theme Title Slide     dt  4         Date Placeholder 3 0.500000
## 17 Office Theme Title Slide    ftr  5       Footer Placeholder 4 3.416667
## 27 Office Theme Title Slide sldNum  6 Slide Number Placeholder 5 7.166667
##        offy       cx        cy
## 9  6.951389 2.333333 0.3993056
## 17 6.951389 3.166667 0.3993056
## 27 6.951389 2.333333 0.3993056

Creating pptx deck and slides

You already created pres. Now create a data object called exercise. We’ll refer back to exercise a few times in class.

exercise <- read_pptx()

Creating pptx deck and slides

  • Use add_slide() to add slides to pres
  • Indicate layout and master values
  • You can use <- or not as you build your presentation
    • As a rule, use <-
    • Exception: if you’re chaining together functions from read_pptx() to print()
# Do this
pres <- pres %>% add_slide(layout = "Title Slide", master = "Office Theme")

# Not this
pres %>% add_slide(layout = "Title Slide", master = "Office Theme")

Exercise - 2 minutes

  • Add a ‘Title and Content’ slide to exercise
  • Confirm that you successfully added a slide to exercise
exercise <- exercise %>% 
  add_slide(layout = "Title and Content", master = "Office Theme")
length(exercise)
## [1] 1

Creating pptx deck and slides

  • You can chain together add_slide() functions
  • Every add_slide() adds a new slide to your pptx files
pres <- read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  add_slide(layout = "Title Only", master = "Office Theme")

Creating pptx deck and slides

  • You can chain together add_slide() functions
  • Every add_slide() adds a new slide to your pptx files
pres <- read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  add_slide(layout = "Title Only", master = "Office Theme")

pres
## pptx document with 4 slide(s)
## Available layouts and their associated master(s) are:
##              layout       master
## 1       Title Slide Office Theme
## 2 Title and Content Office Theme
## 3    Section Header Office Theme
## 4       Two Content Office Theme
## 5        Comparison Office Theme
## 6        Title Only Office Theme
## 7             Blank Office Theme

Creating pptx deck and slides

  • We’ve built a deck skeleton
  • Let’s export it with print()
  • File will export to your working directory
    • Use getwd() to double check location
pres %>% 
  print(target = 'my_presentation.pptx')

Creating pptx deck and slides

  • We’ve built a deck skeleton
  • Let’s export it with print()
  • File will export to your working directory
    • Use getwd() to double check location
read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  add_slide(layout = "Title Only", master = "Office Theme") %>% 
  print(target = 'my_presentation.pptx')

Adding slide content

Adding text to slides

  • ph_with() adds text to slides
    • The value argument is the content
    • The location argument indicates where and what type of text to place
  • ph_with() follows add_slide() or on_slide()
pres <- read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>%
  ph_with(value = 'Whoa! What a title!')

Adding text to slides

  • ph_with() adds text to slides
    • The value argument is the content
    • The location argument indicates where and what type of text to place
  • ph_with() follows add_slide() or on_slide()
pres <- pres %>% 
  on_slide(1) %>%
  ph_with(value = 'Whoa! What a title!')

Adding text to slides

  • ph_with() adds text to slides
    • The value argument is the content
    • The location argument indicates where and what type of text to place
  • ph_with() follows add_slide() or on_slide()

This code will error out: argument ‘expr’ is missing

pres <- pres %>% 
  on_slide(1) %>%
  ph_with(value = 'Whoa! What a title!')

Adding text to slides

  • ph_with() adds text to slides
    • The value argument is the content
    • The location argument indicates where and what type of text to place
  • Add location argument to ph_with()
    • ph_location_type() is easiest when writing text
    • ph_location_type() is a nested function and takes the argument type
pres <- pres %>% 
  on_slide(1) %>% 
  ph_with(
    value = 'Whoa! What a title!'
    , location = ph_location_type(type = 'ctrTitle')
    )

Adding text to slides

  • ph_with() adds text to slides
    • The value argument is the content
    • The location argument indicates where and what type of text to place
  • Add location argument to ph_with()
    • ph_location_type() is easiest when writing text
    • ph_location_type() is a nested function and takes the argument type
  • type values come from layout_properties()
pres %>% 
  layout_properties(layout = "Title Slide", master = "Office Theme") %>%
  select(type, ph_label)
##        type                   ph_label
## 9        dt         Date Placeholder 3
## 17      ftr       Footer Placeholder 4
## 27   sldNum Slide Number Placeholder 5
## 35 ctrTitle                    Title 1
## 36 subTitle                 Subtitle 2

Adding text to slides

  • You can review slide content with slide_summary
pres %>% 
  slide_summary()
##       type id ph_label offx offy cx cy                text
## 1 ctrTitle  2  Title 1   NA   NA NA NA Whoa! What a title!

Adding text to slides

  • Just like you can chain together add_slide() functions, you can chain together ph_with() functions
pres <- read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>% 
  ph_with(
    value = 'Whoa! What a title!'
    , location = ph_location_type(type = 'ctrTitle')
    ) %>%
  ph_with(
    value = 'This is an important footnote'
    , location = ph_location_type(type = 'ftr')
    )

Adding text to slides

  • You can change text style (color, size, bold, etc.)
  • …but it’s a little hacky
  • 2 step process
    • Create a text properties data object with fp_text()
    • Add text with ph_add_text()
help(fp_text)

text_prop <- fp_text(color = 'red', underlined = TRUE)

Adding text to slides

  • 2 step process
    • Create a text properties data object with fp_text()
    • Add text with ph_add_text()
  • ph_with(value = '')text_prop <- fp_text(color = ‘red’, bold = TRUE, font.size = 40)
  • ph_add_text takes different arguments
    • str, style, ph_label
text_prop <- fp_text(color = 'red', bold = TRUE, font.size = 40)

read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>% 
  ph_with(
    value = ''
    , location = ph_location_type(type = 'ctrTitle')
    ) %>%
  ph_add_text(str = 'Whoa! What a title!', style = text_prop, ph_label = "Title 1" ) %>%
  print(target = 'pres_with_red_text.pptx')

Exercise - 5 minutes

  • Add a title and a footer to the exercise slide
  • Change the text properties if you would like
  • Confirm that you successfully added both
exercise <- exercise %>% 
  on_slide(1) %>% 
  ph_with(
    value = 'Whoa! What another great title!'
    , location = ph_location_type(type = 'title')
    ) %>%
  ph_with(
    value = 'Here is a note at the foot of the slide.'
    , location = ph_location_type(type = 'ftr')
    )

Exercise - 5 minutes

  • Add a title and a footer to the exercise slide
  • Change the text properties if you would like
  • Confirm that you successfully added both
exercise <- read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(
    value = 'Whoa! What another great title!'
    , location = ph_location_type(type = 'title')
    ) %>%
  ph_with(
    value = 'Here is a note at the foot of the slide.'
    , location = ph_location_type(type = 'ftr')
    )

Exercise - 5 minutes

  • Add a title and a footer to the exercise slide
  • Change the text properties if you would like
  • Confirm that you successfully added both
exercise %>%
  slide_summary()
##    type id             ph_label offx offy cx cy
## 1 title  2              Title 1   NA   NA NA NA
## 2   ftr  3 Footer Placeholder 4   NA   NA NA NA
##                                       text
## 1          Whoa! What another great title!
## 2 Here is a note at the foot of the slide.

Adding text to slides

  • You can write text to the body of the slide with ph_with()
  • Body text defaults as bullet points
    • Comma separate the text for each bullet point
    • c()
    • Use fpar() and ftext() to print in paragraph format
ph_with('Bullet Point 1', location = ph_location_type(type = "body"))
ph_with(c('Bullet Point 1', 'Bullet Point 2'), location = ph_location_type(type = "body"))
bullet_pts <- c('Bullet Point 1', 'Bullet Point 2')

ph_with(bullet_pts, location = ph_location_type(type = "body"))

Exercise 7 minutes

  • Create and print a 2-slide PowerPoint deck
  • Create and write text to a ‘Title Slide’
  • Create and write text to one other slide of your choice

Exercise 7 minutes

  • Create and print a 2-slide PowerPoint deck
  • Create and write text to a ‘Title Slide’
  • Create and write text to one other slide of your choice
bullet_pts <- c('Bullet Point 1', 'Bullet Point 2', 'Bullet Point 3')

read_pptx() %>% 
  add_slide(layout = "Title Slide", master = "Office Theme") %>%
  ph_with(
    value = 'My Presentation'
    , location = ph_location_type(type = 'ctrTitle')
    ) %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(
    value = 'Whoa! What another great title!'
    , location = ph_location_type(type = 'title')
    )%>%
  ph_with(
    bullet_pts
    , location = ph_location_type(type = "body")
    ) %>% 
  print(target = 'my_2_slide_presentation.pptx')
## [1] "/Users/graham/Dropbox/r_course_evans_school/my_2_slide_presentation.pptx"

Adding data visualizations to slides

Adding data visualizations to slides

  • Adding data visualizations works like adding text
  • Use ph_with()
  • You need value and location arguments
  • value refers to a data visualization you created, saved as a data object

Adding data visualizations to slides

  • Adding data visualizations works like adding text
  • Use ph_with()
  • You need value and location arguments
  • value refers to a data visualization you created, saved as a data object
  • location
    • ph_location_fullsize()
    • ph_location()
    • ph_location_left()
    • ph_location_right()

Adding data visualizations to slides

  • You need value and location arguments
  • value refers to a data visualization you created, saved as a data object
  • location
    • ph_location_fullsize()
    • ph_location()
    • ph_location_left()
    • ph_location_right()
my_data_viz <- crime %>% 
  filter(! precinct %in% NA) %>%
  ggplot(aes(reported_date, occurred_date, color = precinct)) + 
  geom_point() + 
  theme_hc()
pres <- read_pptx() %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(value = my_data_viz, location = ph_location_fullsize())

Adding data visualizations to slides

  • ph_location() accepts width, height, top, and left arguments
  • Inputs are in inches
  • This gives you a bit more control
  • Note: R will trim your plot if width and height are too small
pres <- read_pptx() %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(
  value = my_data_viz
  , location = ph_location(width = 9, height = 7, top = .21, left = .43))
  
pres %>%
  print(target = 'scatterplot_pres.pptx')

Exercise - 5 minutes

  • Add a new slide to exercise
  • Add the data visualization created at the beginning of the class to the slide
  • Export a pptx file

Exercise - 5 minutes

  • Add a new slide to exercise
  • Add the data visualization created at the beginning of the class to the slide
  • Export a pptx file
plot <- crime %>% 
  mutate(occurred_time_ampm = ifelse(occurred_time >= 1200, 'PM', 'AM')) %>% 
  filter(
    ! occurred_time_ampm %in% NA & 
    ! crime_subcategory %in% NA
    ) %>%
  group_by(crime_subcategory, occurred_time_ampm) %>%
  summarise(n = n()) %>%
  ungroup %>%
  group_by(crime_subcategory) %>%
  mutate(total = sum(n, na.rm = TRUE)) %>%
  filter(total >= 100) %>%
  ggplot(aes(reorder(crime_subcategory, n), n, fill = occurred_time_ampm)) + 
  geom_bar(stat = 'identity') + 
  coord_flip() + 
  theme_wsj() + 
  scale_colour_wsj() + 
  labs(title = 'Most incidence occur\nin the AM', fill = 'Time of Day')

Exercise - 5 minutes

  • Add a new slide to exercise
  • Add the data visualization created at the beginning of the class to the slide
  • Export a pptx file
exercise %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(
    value = plot
    , location = ph_location(width = 9, height = 7, top = .21, left = .43)
    ) %>% 
  print(target = 'my_pres_with_viz.pptx')
## [1] "/Users/graham/Dropbox/r_course_evans_school/my_pres_with_viz.pptx"

Your R workshop assignment

  • Create a 3-slide PPTX
    • Include a title slide
    • Include two other slides. One slide should have a chart. The other slide can have a chart or text.
    • Submit the 3-slide PPTX to me by email on or before October 28, 2019