class: inverse, left, bottom background-image: url(https://images.unsplash.com/photo-1622754280615-3992b7ab9da8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80) background-size: cover
??? Title slide
# This is the recommended set up for flipbooks # you might think about setting cache to TRUE as you gain practice --- building flipbooks from scratch can be time consuming knitr::opts_chunk$set(fig.width = 6, message = FALSE, warning = FALSE, comment = "", cache = T) library(flipbookr) library(tidyverse) theme_set(theme_minimal(base_size = 15))
```{css, eval = TRUE, echo = FALSE} .remark-code{line-height: 1.5; font-size: 110%}
@media print { .has-continuation { display: block; } }
code.r.hljs.remark-code{ position: relative; overflow-x: hidden; }
code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; }
<!-- # Step 00. prep some data, records and flat data frame --> ```r library(tidyverse) library(magrittr) theme_set(theme_gray(base_size = 15)) Titanic %>% data.frame() %>% uncount(weights = Freq) -> tidy_titanic ; tidy_titanic %>% head() Titanic %>% data.frame() -> flat_titanic ; flat_titanic %>% head()
--
--
-- (how functions work)
--
-- (dataframe/tibbles)
--
-- (stepwise work makes understanding easy)
--
--
r chunk_reveal("academic")
gapminder::gapminder %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4) + aes(color = continent) + aes(shape = continent) + facet_wrap(facets = vars(continent)) + scale_x_log10() + labs(x = "GDP Per Cap $US, Thousands") + labs(y = "Life Expectancy, years") + labs(color = NULL, shape = NULL) + labs(title = "I just described this plot, and it appeared!")
???
--
--
r chunk_reveal("pivot_chart", title = "## But a graphic may look like a table")
tidy_titanic %>% ggplot() + aes(x = Sex, y = Survived) + geom_jitter() + geom_count(color = "blue")
--
--
--
--
r chunk_reveal("slow", title = "# Contingency table")
tidy_titanic %>% count(Sex, Survived) %>% pivot_wider(names_from = Survived, values_from = n)
--
r chunk_reveal("proportionslow", title = "# proportions")
tidy_titanic %>% count(Sex, Survived) %>% group_by(Sex) %>% mutate(prop = n/sum(n)) %>% select(-n) %>% pivot_wider(values_from = prop, names_from = Sex)
--
class: inverse, center, middle
--
--
--
--
pivot_count_script <- readLines("../R/pivot_count.R")
pivot_calc_script <- readLines("../R/pivot_calc.R")
r chunk_reveal("examples")
# cols only tidy_titanic %>% pivot_count(rows = Survived) # rows and cols tidy_titanic %>% pivot_count(rows = Survived, cols = Sex) tidy_titanic %>% pivot_count( rows = c(Survived), cols = c(Sex, Class) )
pivot_prop_script <- readLines("../R/pivot_prop.R")
r chunk_reveal("propusage")
tidy_titanic %>% pivot_prop(rows = Survived, cols = Class, within = Class) tidy_titanic %>% pivot_prop(rows = c(Survived, Sex), cols = Class, within = Survived) tidy_titanic %>% pivot_prop(rows = c(Survived, Sex), cols = Class, within = c(Survived, Sex))
tidy_titanic %>% pivot_count(cols = Sex, rows = Survived, pivot = F)
r chunk_reveal("propgraph")
tidy_titanic %>% pivot_prop(rows = c(Survived, Sex), cols = Class, within = Survived, pivot = F) %>% ggplot() + aes(x = Class, y = Sex) + facet_grid(rows = vars(Survived)) + geom_tile() + aes(fill = prop) + geom_text(aes(label = prop %>% round(3)))
r chunk_reveal("janitor", title = "## janitor::tabyl() is great")
tidy_titanic %>% janitor::tabyl(Sex, Survived) %>% janitor::adorn_percentages( "row") %>% janitor::adorn_pct_formatting( digits = 2) %>% janitor::adorn_ns() -> t1 tidy_titanic %>% janitor::tabyl(Sex, Survived, Class)
--
It's awesome!
--
but 3-way grouping is not 'flat file'
--
doesn't return a data frame, so maybe not totally tidy
--
limited to 3 way grouping
--
no pivot = FALSE option
--
--
-- so you can focus on the findings!
--
--
--
and doing pivot = F output.
--
library(ggstamp) set.seed(128790) ggcanvas() + stamp_tile(x = rnorm(500)/2.5, y= rnorm(500)/2.5, fill = "darkslateblue", alpha = runif(500, .25, 1), width = rnorm(500)/3, height = rnorm(500)/3, color = alpha("black", .75)) + stamp_point(size = 8, color = "grey85") + stamp_spoke(angle = pi, size = 1.7, radius = .65, color = "grey85") + stamp_point(size = 5, color = "grey85") + stamp_point(size = 3, color = "grey55") + stamp_polygon_holes(y0 = .25) + stamp_text(label = "tidy", hjust = 1, color = "violetred", vjust = -.2, size = 17) + stamp_text(label = "pivot", hjust = -.1, angle = 0:25, color = "violetred", vjust = -.2, size = 17, alpha = c(0:25/400)) + stamp_spoke(angle = .45*0:25/25, size = 1.7, radius = .85, color = "grey55", alpha = c(0:25/400)) + stamp_spoke(angle = .45, size = 1.7, radius = .85, color = "grey55") + stamp_text(label = "pivot", hjust = -.1, angle = 25, color = "violetred", vjust = -.2, size = 17) + ggstamp::theme_void_fill(fill = "darkslateblue") + stamp_polygon(color = "lightslateblue", alpha = 0, y0 = .25, size = 4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.