October 29, 2020

What we learned in the previous class

  • Pivoting data with gather() and spread()
  • ggplot2 syntax and aesthetics functionality
  • How to build bar plots, scatterplots, line plots

What we’ll do today

  • Course assignment description
  • Test ability to create .pptx
  • Warm up: data visualization
  • Develop skills to create and format .pptx in R
  • 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.
    • Build plots with data from another class or research project if you would like
    • Submit the 3-slide .pptx to me by email on or before November 6, 2020

Test ability to create .pptx

  • Make sure you have installed PowerPoint
    • Mac users, make sure you have downloaded and installed XQuartz as well
  • Open and save a new R or Rmd file
  • Set your working directory
  • Paste the 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')

# install.packages('officer')
library(officer)

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

Wrapping up data visualization

But first, read your data

library(tidyverse)
covid <- read_csv('https://rb.gy/lzlylj')
crime <- read_csv('https://rb.gy/5zuayh') 

Exercise - 15 minutes

  • Create a stacked bar plot from the crime dataset that shows the number of incidents by precinct and a time of day variable that indicates if the incident occurred in the AM or PM (first half or second half of the day)
    • Create a new variable with mutate() called occurred_time_ampm that tells whether an incident occurred in the AM or PM.
    • Exclude observations with NA values in occurred_time_ampm and precinct
    • Use group_by(), summarise(), and n()
    • Make sure your axes are labeled correctly
    • Give your chart a title
    • Use occurred_time_ampm in the fill arguments in aes()
    • Use a pre-built theme
    • Lastly, create a new data object storing the visualization you build (you’ll need it later)

Help
- Go to the next slide to see a version of the plot without seeing the solution
- Refer to slides from the previous class

Exercise - 15 minutes

Exercise - 15 minutes

crime %>% 
  mutate(occurred_time_ampm = ifelse(occurred_time >= 1200, 'PM', 'AM')) %>% 
  filter(
    ! occurred_time_ampm %in% NA & 
    ! precinct %in% NA
    ) %>%
  group_by(precinct, occurred_time_ampm) %>%
  summarise(n = n()) %>%
  ungroup %>%
  ggplot(aes(reorder(precinct, n), n, fill = occurred_time_ampm)) + 
  geom_bar(stat = 'identity') + 
  coord_flip() + 
  theme_wsj() + 
  scale_fill_wsj() + 
  labs(title = 'Most incidents occur\nin the PM', 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

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()
# This
pres <- pres %>% add_slide(layout = 'Title Slide', master = 'Office Theme')

# vs. 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 a nested function and takes the argument type
    • type in ph_location_type(): 'body', 'title', 'ctrTitle', 'subTitle', 'dt', 'ftr', 'sldNum'
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 a nested function and takes the argument type
    • type in ph_location_type(): 'body', 'title', 'ctrTitle', 'subTitle', 'dt', 'ftr', 'sldNum'
    • 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 0.75 2.329861 8.5 1.607639 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
  • 3 step process
    • Create a text properties data object with fp_text()
    • Create a ‘shape type’ with ph_with()
    • Add text with ph_add_text()
help(fp_text)

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

Adding text to slides

  • 3 step process
    • Create a text properties data object with fp_text()
    • Create a ‘shape type’ with ph_with()
    • Add text with ph_add_text()
  • ph_with(value = '')
  • ph_add_text takes different arguments
    • str, style, type
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, type = 'ctrTitle') %>%
  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 0.500000 0.3003478 9.000000 1.2500000
## 2   ftr  3 Footer Placeholder 4 3.416667 6.9513889 3.166667 0.3993056
##                                       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')

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_left()
    • ph_location_right()
    • ph_location()

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_left()
    • ph_location_right()
    • ph_location()
my_data_viz <- crime %>% 
  filter(! precinct %in% NA) %>%
  ggplot(aes(reported_date, occurred_date, color = precinct)) + 
  geom_point() + 
  ggthemes::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 large
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 & 
    ! precinct %in% NA
    ) %>%
  group_by(precinct, occurred_time_ampm) %>%
  summarise(n = n()) %>%
  ungroup %>%
  ggplot(aes(reorder(precinct, n), n, fill = occurred_time_ampm)) + 
  geom_bar(stat = 'identity') + 
  coord_flip() + 
  theme_wsj() + 
  scale_fill_wsj() + 
  labs(title = 'Most incidents occur in the PM', 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')

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.
    • Build plots with data from another class or research project if you would like
    • Submit the 3-slide .pptx to me by email on or before November 6, 2020