- Pivoting data with
gather()andspread() - ggplot2 syntax and aesthetics functionality
- How to build bar plots, scatterplots, line plots
October 29, 2020
gather() and spread()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')
library(tidyverse)
covid <- read_csv('https://rb.gy/lzlylj')
crime <- read_csv('https://rb.gy/5zuayh')
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)
mutate() called occurred_time_ampm that tells whether an incident occurred in the AM or PM.NA values in occurred_time_ampm and precinctgroup_by(), summarise(), and n()occurred_time_ampm in the fill arguments in aes()Help
- Go to the next slide to see a version of the plot without seeing the solution
- Refer to slides from the previous class
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')
officerofficer allows you to create and manipulate/update .pptxs
knitr.officer makes PowerPoint deck creation easy with pre-built functions
<-) to create a pptx data objectread_pptx() creates a pptx data object in Rpres <- read_pptx()
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
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
layout_properties() gives you a more detailed view of layout options for slidestype, id, info about element placement and size on slidepres %>% 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
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()
add_slide() to add slides to preslayout and master values<- or not as you build your presentation
<-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')
exerciseexerciseexercise <- exercise %>% add_slide(layout = 'Title and Content', master = 'Office Theme')
length(exercise) ## [1] 1
add_slide() functionsadd_slide() adds a new slide to your pptx filespres <- 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')
add_slide() functionsadd_slide() adds a new slide to your pptx filespres <- 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
print()getwd() to double check locationpres %>% print(target = 'my_presentation.pptx')
print()getwd() to double check locationread_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')
ph_with() adds text to slides
value argument is the contentlocation argument indicates where and what type of text to placeph_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!')
ph_with() adds text to slides
value argument is the contentlocation argument indicates where and what type of text to placeph_with() follows add_slide() or on_slide()pres <- pres %>% on_slide(1) %>% ph_with(value = 'Whoa! What a title!')
ph_with() adds text to slides
value argument is the contentlocation argument indicates where and what type of text to placeph_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!')
ph_with() adds text to slides
value argument is the contentlocation argument indicates where and what type of text to placelocation argument to ph_with()
ph_location_type() is a nested function and takes the argument typetype 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')
)
ph_with() adds text to slides
value argument is the contentlocation argument indicates where and what type of text to placelocation argument to ph_with()
ph_location_type() is a nested function and takes the argument typetype 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
slide_summarypres %>% 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!
add_slide() functions, you can chain together ph_with() functionspres <- 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')
)
fp_text()ph_with()ph_add_text()help(fp_text) text_prop <- fp_text(color = 'red', underlined = TRUE)
fp_text()ph_with()ph_add_text()ph_with(value = '')ph_add_text takes different arguments
str, style, typetext_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 slideexercise <- 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 slideexercise <- 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 slideexercise %>% 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.
ph_with()c()fpar() and ftext() to print in paragraph formatph_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'))
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')
ph_with()value and location argumentsvalue refers to a data visualization you created, saved as a data objectph_with()value and location argumentsvalue refers to a data visualization you created, saved as a data objectlocation
ph_location_fullsize()ph_location_left()ph_location_right()ph_location()value and location argumentsvalue refers to a data visualization you created, saved as a data objectlocation
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())
ph_location() accepts width, height, top, and left argumentswidth and height are too largepres <- 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')
exerciseexerciseplot <- 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')
exerciseexercise %>%
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')