- 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
October 26, 2019
library(tidyverse)
crime <- read_csv('https://bit.ly/2mcZLq4') %>% as.data.frame()
contr <- read_csv('https://bit.ly/2lQySrQ') %>% as.data.frame()
crime datasetreported_date and occurred_dateprecinctNA values from precinctcrime %>% filter(! precinct %in% NA) %>% ggplot(aes(reported_date, occurred_date, color = precinct)) + geom_point() + theme_hc()
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)mutate() called occurred_time_ampm that tells whether an incident occurred in the AM or PM.occurred_time_ampm in the color or fill arguments in aes()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')
officerofficer allows you to create and manipulate/update PPTXs
knitr.officer makes PowerPoint deck creation easy with pre-built functions
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(c('officer', 'magrittr'))
library(officer)
read_pptx() %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
print(target = 'my_presentation.pptx')
<-) 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()# Do this pres <- pres %>% add_slide(layout = "Title Slide", master = "Office Theme") # Not 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 easiest when writing textph_location_type() is a nested function and takes the argument typepres <- 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 easiest when writing textph_location_type() is a nested function and takes the argument typetype 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 NA NA NA NA 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_add_text()help(fp_text) text_prop <- fp_text(color = 'red', underlined = TRUE)
fp_text()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_labeltext_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 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 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.
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')
## [1] "/Users/graham/Dropbox/r_course_evans_school/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()ph_location_left()ph_location_right()value and location argumentsvalue refers to a data visualization you created, saved as a data objectlocation
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())
ph_location() accepts width, height, top, and left argumentswidth and height are too smallpres <- 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 &
! 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')
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')
## [1] "/Users/graham/Dropbox/r_course_evans_school/my_pres_with_viz.pptx"