class: center, middle, inverse, title-slide # Exploded code ## Using flipbookr and xaringan ### Me --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 70%} @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; } </style> --- count: false .panel1-setup-auto[ ```r *knitr::opts_chunk$set(echo = TRUE) ``` ] .panel2-setup-auto[ ] --- count: false .panel1-setup-auto[ ```r knitr::opts_chunk$set(echo = TRUE) *library(ggplot2) ``` ] .panel2-setup-auto[ ] --- count: false .panel1-setup-auto[ ```r knitr::opts_chunk$set(echo = TRUE) library(ggplot2) *library(tidyverse) ``` ] .panel2-setup-auto[ ] <style> .panel1-setup-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-setup-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-setup-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 0: get it done with base ggplot2 --- count: false .panel1-step0-auto[ ```r *create_diamond <- function(x0, y0, width = 1, height = 1){ * data.frame( * x = c(x0 + width, x0, x0 - width, x0, x0 + width), * y = c(y0, y0 + height, y0, y0 - height , y0) * ) *} ``` ] .panel2-step0-auto[ ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } *cars ``` ] .panel2-step0-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% * .[1:5,] ``` ] .panel2-step0-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 ``` ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% * mutate(diamond = purrr::map2(speed, dist, create_diamond)) ``` ] .panel2-step0-auto[ ``` speed dist diamond 1 4 2 5, 4, 3, 4, 5, 2, 3, 2, 1, 2 2 4 10 5, 4, 3, 4, 5, 10, 11, 10, 9, 10 3 7 4 8, 7, 6, 7, 8, 4, 5, 4, 3, 4 4 7 22 8, 7, 6, 7, 8, 22, 23, 22, 21, 22 5 8 16 9, 8, 7, 8, 9, 16, 17, 16, 15, 16 ``` ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% * mutate(group = row_number()) ``` ] .panel2-step0-auto[ ``` speed dist diamond group 1 4 2 5, 4, 3, 4, 5, 2, 3, 2, 1, 2 1 2 4 10 5, 4, 3, 4, 5, 10, 11, 10, 9, 10 2 3 7 4 8, 7, 6, 7, 8, 4, 5, 4, 3, 4 3 4 7 22 8, 7, 6, 7, 8, 22, 23, 22, 21, 22 4 5 8 16 9, 8, 7, 8, 9, 16, 17, 16, 15, 16 5 ``` ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% mutate(group = row_number()) %>% * unnest() ``` ] .panel2-step0-auto[ ``` # A tibble: 25 × 5 speed dist x y group <dbl> <dbl> <dbl> <dbl> <int> 1 4 2 5 2 1 2 4 2 4 3 1 3 4 2 3 2 1 4 4 2 4 1 1 5 4 2 5 2 1 6 4 10 5 10 2 7 4 10 4 11 2 8 4 10 3 10 2 9 4 10 4 9 2 10 4 10 5 10 2 11 7 4 8 4 3 12 7 4 7 5 3 13 7 4 6 4 3 14 7 4 7 3 3 15 7 4 8 4 3 16 7 22 8 22 4 17 7 22 7 23 4 18 7 22 6 22 4 19 7 22 7 21 4 20 7 22 8 22 4 21 8 16 9 16 5 22 8 16 8 17 5 23 8 16 7 16 5 24 8 16 8 15 5 25 8 16 9 16 5 ``` ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% mutate(group = row_number()) %>% unnest() %>% * ggplot() ``` ] .panel2-step0-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/step0_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% mutate(group = row_number()) %>% unnest() %>% ggplot() + * geom_point(data = cars[1:5,], aes(x = speed, y = dist)) ``` ] .panel2-step0-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/step0_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% mutate(group = row_number()) %>% unnest() %>% ggplot() + geom_point(data = cars[1:5,], aes(x = speed, y = dist)) + * geom_path(aes(x = x, y = y, group = group)) ``` ] .panel2-step0-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/step0_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-step0-auto[ ```r create_diamond <- function(x0, y0, width = 1, height = 1){ data.frame( x = c(x0 + width, x0, x0 - width, x0, x0 + width), y = c(y0, y0 + height, y0, y0 - height , y0) ) } cars %>% .[1:5,] %>% mutate(diamond = purrr::map2(speed, dist, create_diamond)) %>% mutate(group = row_number()) %>% unnest() %>% ggplot() + geom_point(data = cars[1:5,], aes(x = speed, y = dist)) + geom_path(aes(x = x, y = y, group = group)) ``` ] .panel2-step0-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/step0_auto_10_output-1.png)<!-- --> ] <style> .panel1-step0-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-step0-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-step0-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 1a: write set up data function --- count: false .panel1-cars-auto[ ```r *setup_data_diamonds <- function(data, params) { * if (anyDuplicated(data$group)) { * data$group <- paste(data$group, seq_len(nrow(data)), sep = "-") * } * data * } ``` ] .panel2-cars-auto[ ] <style> .panel1-cars-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-cars-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-cars-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 1b: test set up data function --- count: false .panel1-step1b-auto[ ```r *cars ``` ] .panel2-step1b-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-step1b-auto[ ```r cars %>% * .[1:2,] ``` ] .panel2-step1b-auto[ ``` speed dist 1 4 2 2 4 10 ``` ] --- count: false .panel1-step1b-auto[ ```r cars %>% .[1:2,] %>% * mutate(group = row_number()) ``` ] .panel2-step1b-auto[ ``` speed dist group 1 4 2 1 2 4 10 2 ``` ] --- count: false .panel1-step1b-auto[ ```r cars %>% .[1:2,] %>% mutate(group = row_number()) %>% * setup_data_diamonds() ``` ] .panel2-step1b-auto[ ``` speed dist group 1 4 2 1 2 4 10 2 ``` ] <style> .panel1-step1b-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-step1b-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-step1b-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 2a: write compute panel function --- # step2a.01 write row processing function # in this case we write a function to process each row for the data --- count: false .panel1-step_2a-auto[ ```r *create_diamond <- function(x0, y0, width = 1, height = 1){ * data.frame( * x = c(x0 + width, x0, x0 - width, x0, x0 + width), * y = c(y0, y0 + height, y0, y0 - height , y0) * ) *} ``` ] .panel2-step_2a-auto[ ] <style> .panel1-step_2a-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-step_2a-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-step_2a-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step2a.02 test row processing function --- count: false .panel1-2a2-auto[ ```r *create_diamond(1, 2) ``` ] .panel2-2a2-auto[ ``` x y 1 2 2 2 1 3 3 0 2 4 1 1 5 2 2 ``` ] --- count: false .panel1-2a2-auto[ ```r create_diamond(1, 2) *create_diamond(4, 5) ``` ] .panel2-2a2-auto[ ``` x y 1 2 2 2 1 3 3 0 2 4 1 1 5 2 2 ``` ``` x y 1 5 5 2 4 6 3 3 5 4 4 4 5 5 5 ``` ] <style> .panel1-2a2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-2a2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-2a2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 2a.03 the compute panel function This function takes a full data frame, goes through each of the rows, and uses the row processing function on each, and returns a data frame --- count: false .panel1-2a3-auto[ ```r *compute_panel_diamonds <- function(data, scales) { * cols_to_keep <- setdiff(names(data), c("x0", "y0")) * diamonds <- lapply(seq_len(nrow(data)), function(i) { * diamonds_path <- create_diamond(data$x0[i], data$y0[i]) * cbind(diamonds_path, unclass(data[i, cols_to_keep])) * }) * do.call(rbind, diamonds) *} ``` ] .panel2-2a3-auto[ ] --- count: false .panel1-2a3-auto[ ```r compute_panel_diamonds <- function(data, scales) { cols_to_keep <- setdiff(names(data), c("x0", "y0")) diamonds <- lapply(seq_len(nrow(data)), function(i) { diamonds_path <- create_diamond(data$x0[i], data$y0[i]) cbind(diamonds_path, unclass(data[i, cols_to_keep])) }) do.call(rbind, diamonds) } *compute_panel_diamonds_purrr <- function(data, scales){ * cols_to_keep <- setdiff(names(data), c("x0", "y0")) * data %>% * select(x0, y0) %>% * mutate(diamond = purrr::map2(x0, y0, create_diamond)) %>% * mutate(group = row_number()) %>% * unnest() %>% * ungroup() %>% * data.frame() *} ``` ] .panel2-2a3-auto[ ] <style> .panel1-2a3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-2a3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-2a3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # step 2b: Test the compute_panel --- count: false .panel1-2b-auto[ ```r *cars ``` ] .panel2-2b-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% * .[1:2,] ``` ] .panel2-2b-auto[ ``` speed dist 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% * rename(x0 = speed) ``` ] .panel2-2b-auto[ ``` x0 dist 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% * rename(y0 = dist) ``` ] .panel2-2b-auto[ ``` x0 y0 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% * mutate(group = row_number()) ``` ] .panel2-2b-auto[ ``` x0 y0 group 1 4 2 1 2 4 10 2 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% * compute_panel_diamonds() ``` ] .panel2-2b-auto[ ``` x y unclass(data[i, cols_to_keep]) 1 5 2 1 2 4 3 1 3 3 2 1 4 4 1 1 5 5 2 1 6 5 10 2 7 4 11 2 8 3 10 2 9 4 9 2 10 5 10 2 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% * str() ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> *hide ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide *cars ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide cars %>% * .[1:2,] ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` speed dist 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide cars %>% .[1:2,] %>% * rename(x0 = speed) ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` x0 dist 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide cars %>% .[1:2,] %>% rename(x0 = speed) %>% * rename(y0 = dist) ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` x0 y0 1 4 2 2 4 10 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% * compute_panel_diamonds_purrr() ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` x0 y0 x y group 1 4 2 5 2 1 2 4 2 4 3 1 3 4 2 3 2 1 4 4 2 4 1 1 5 4 2 5 2 1 6 4 10 5 10 2 7 4 10 4 11 2 8 4 10 3 10 2 9 4 10 4 9 2 10 4 10 5 10 2 ``` ] --- count: false .panel1-2b-auto[ ```r cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% mutate(group = row_number()) %>% compute_panel_diamonds() %>% str() -> hide cars %>% .[1:2,] %>% rename(x0 = speed) %>% rename(y0 = dist) %>% compute_panel_diamonds_purrr() %>% * str() ``` ] .panel2-2b-auto[ ``` 'data.frame': 10 obs. of 3 variables: $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ unclass(data[i, cols_to_keep]): int 1 1 1 1 1 2 2 2 2 2 ``` ``` 'data.frame': 10 obs. of 5 variables: $ x0 : num 4 4 4 4 4 4 4 4 4 4 $ y0 : num 2 2 2 2 2 10 10 10 10 10 $ x : num 5 4 3 4 5 5 4 3 4 5 $ y : num 2 3 2 1 2 10 11 10 9 10 $ group: int 1 1 1 1 1 2 2 2 2 2 ``` ] <style> .panel1-2b-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-2b-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-2b-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Step 3: write ggproto --- count: false .panel1-3-auto[ ```r *StatDiamond <- ggproto("StatDiamond", Stat, * setup_data = setup_data_diamonds, * compute_panel = compute_panel_diamonds, * required_aes = c("x0", "y0") *) ``` ] .panel2-3-auto[ ] --- count: false .panel1-3-auto[ ```r StatDiamond <- ggproto("StatDiamond", Stat, setup_data = setup_data_diamonds, compute_panel = compute_panel_diamonds, required_aes = c("x0", "y0") ) *StatDiamondpurrr <- ggproto("StatDiamond", Stat, * setup_data = setup_data_diamonds, * compute_panel = compute_panel_diamonds_purrr, * required_aes = c("x0", "y0") *) ``` ] .panel2-3-auto[ ] <style> .panel1-3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Step 4: Write function --- --- # a variant where compute group uses purrr --- # Step 5: Enjoy! --- count: false .panel1-5-auto[ ```r *set.seed(1244) ``` ] .panel2-5-auto[ ] --- count: false .panel1-5-auto[ ```r set.seed(1244) *cars ``` ] .panel2-5-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% * .[1:5,] ``` ] .panel2-5-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 ``` ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% * data.frame() ``` ] .panel2-5-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 ``` ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% * ggplot() ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + * aes(x = speed, y = dist) ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + * geom_point() ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + * aes(x0 = speed, y0 = dist) ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + * geom_diamond(alpha = .2) ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + geom_diamond(alpha = .2) + * aes(fill = speed > 6) ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-5-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + geom_diamond(alpha = .2) + aes(fill = speed > 6) + * geom_diamond(height = 2, alpha = 0, color = "black") ``` ] .panel2-5-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/5_auto_11_output-1.png)<!-- --> ] <style> .panel1-5-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-5-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-5-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## But Purrr version doesn't work? Why? --- count: false .panel1-6-auto[ ```r *set.seed(1244) ``` ] .panel2-6-auto[ ] --- count: false .panel1-6-auto[ ```r set.seed(1244) *cars ``` ] .panel2-6-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 6 9 10 7 10 18 8 10 26 9 10 34 10 11 17 11 11 28 12 12 14 13 12 20 14 12 24 15 12 28 16 13 26 17 13 34 18 13 34 19 13 46 20 14 26 21 14 36 22 14 60 23 14 80 24 15 20 25 15 26 26 15 54 27 16 32 28 16 40 29 17 32 30 17 40 31 17 50 32 18 42 33 18 56 34 18 76 35 18 84 36 19 36 37 19 46 38 19 68 39 20 32 40 20 48 41 20 52 42 20 56 43 20 64 44 22 66 45 23 54 46 24 70 47 24 92 48 24 93 49 24 120 50 25 85 ``` ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% * .[1:5,] ``` ] .panel2-6-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 ``` ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% * data.frame() ``` ] .panel2-6-auto[ ``` speed dist 1 4 2 2 4 10 3 7 4 4 7 22 5 8 16 ``` ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% * ggplot() ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + * aes(x = speed, y = dist) ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + * geom_point() ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + * aes(x0 = speed, y0 = dist) ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + * geom_diamond_purrr(alpha = .2) ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + geom_diamond_purrr(alpha = .2) + * aes(fill = speed > 6) ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + geom_diamond_purrr(alpha = .2) + aes(fill = speed > 6) + * geom_diamond_purrr(height = 2, alpha = 0, color = "black") ``` ] .panel2-6-auto[ ![](geom_diamonds_recipe_flipbook_files/figure-html/6_auto_11_output-1.png)<!-- --> ] --- count: false .panel1-6-auto[ ```r set.seed(1244) cars %>% .[1:5,] %>% data.frame() %>% ggplot() + aes(x = speed, y = dist) + geom_point() + aes(x0 = speed, y0 = dist) + geom_diamond_purrr(alpha = .2) + aes(fill = speed > 6) + geom_diamond_purrr(height = 2, alpha = 0, color = "black") -> *purrr_diamonds_plot ``` ] .panel2-6-auto[ ] <style> .panel1-6-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-6-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-6-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Step 6: Post Mordem using layer_data --- count: false .panel1-7-auto[ ```r *layer_data(last_plot(), 2) ``` ] .panel2-7-auto[ ``` PANEL x x0 y y0 group colour fill size linetype alpha 1 1 5 4 2 2 1 NA grey20 0.5 1 0.2 2 1 4 4 3 2 1 NA grey20 0.5 1 0.2 3 1 3 4 2 2 1 NA grey20 0.5 1 0.2 4 1 4 4 1 2 1 NA grey20 0.5 1 0.2 5 1 5 4 2 2 1 NA grey20 0.5 1 0.2 6 1 5 4 10 10 2 NA grey20 0.5 1 0.2 7 1 4 4 11 10 2 NA grey20 0.5 1 0.2 8 1 3 4 10 10 2 NA grey20 0.5 1 0.2 9 1 4 4 9 10 2 NA grey20 0.5 1 0.2 10 1 5 4 10 10 2 NA grey20 0.5 1 0.2 11 1 8 7 4 4 3 NA grey20 0.5 1 0.2 12 1 7 7 5 4 3 NA grey20 0.5 1 0.2 13 1 6 7 4 4 3 NA grey20 0.5 1 0.2 14 1 7 7 3 4 3 NA grey20 0.5 1 0.2 15 1 8 7 4 4 3 NA grey20 0.5 1 0.2 16 1 8 7 22 22 4 NA grey20 0.5 1 0.2 17 1 7 7 23 22 4 NA grey20 0.5 1 0.2 18 1 6 7 22 22 4 NA grey20 0.5 1 0.2 19 1 7 7 21 22 4 NA grey20 0.5 1 0.2 20 1 8 7 22 22 4 NA grey20 0.5 1 0.2 21 1 9 8 16 16 5 NA grey20 0.5 1 0.2 22 1 8 8 17 16 5 NA grey20 0.5 1 0.2 23 1 7 8 16 16 5 NA grey20 0.5 1 0.2 24 1 8 8 15 16 5 NA grey20 0.5 1 0.2 25 1 9 8 16 16 5 NA grey20 0.5 1 0.2 ``` ] --- count: false .panel1-7-auto[ ```r layer_data(last_plot(), 2) *layer_data(last_plot(), 3) ``` ] .panel2-7-auto[ ``` PANEL x x0 y y0 group colour fill size linetype alpha 1 1 5 4 2 2 1 NA grey20 0.5 1 0.2 2 1 4 4 3 2 1 NA grey20 0.5 1 0.2 3 1 3 4 2 2 1 NA grey20 0.5 1 0.2 4 1 4 4 1 2 1 NA grey20 0.5 1 0.2 5 1 5 4 2 2 1 NA grey20 0.5 1 0.2 6 1 5 4 10 10 2 NA grey20 0.5 1 0.2 7 1 4 4 11 10 2 NA grey20 0.5 1 0.2 8 1 3 4 10 10 2 NA grey20 0.5 1 0.2 9 1 4 4 9 10 2 NA grey20 0.5 1 0.2 10 1 5 4 10 10 2 NA grey20 0.5 1 0.2 11 1 8 7 4 4 3 NA grey20 0.5 1 0.2 12 1 7 7 5 4 3 NA grey20 0.5 1 0.2 13 1 6 7 4 4 3 NA grey20 0.5 1 0.2 14 1 7 7 3 4 3 NA grey20 0.5 1 0.2 15 1 8 7 4 4 3 NA grey20 0.5 1 0.2 16 1 8 7 22 22 4 NA grey20 0.5 1 0.2 17 1 7 7 23 22 4 NA grey20 0.5 1 0.2 18 1 6 7 22 22 4 NA grey20 0.5 1 0.2 19 1 7 7 21 22 4 NA grey20 0.5 1 0.2 20 1 8 7 22 22 4 NA grey20 0.5 1 0.2 21 1 9 8 16 16 5 NA grey20 0.5 1 0.2 22 1 8 8 17 16 5 NA grey20 0.5 1 0.2 23 1 7 8 16 16 5 NA grey20 0.5 1 0.2 24 1 8 8 15 16 5 NA grey20 0.5 1 0.2 25 1 9 8 16 16 5 NA grey20 0.5 1 0.2 ``` ``` PANEL x x0 y y0 group colour fill size linetype alpha 1 1 5 4 2 2 1 black grey20 0.5 1 0 2 1 4 4 3 2 1 black grey20 0.5 1 0 3 1 3 4 2 2 1 black grey20 0.5 1 0 4 1 4 4 1 2 1 black grey20 0.5 1 0 5 1 5 4 2 2 1 black grey20 0.5 1 0 6 1 5 4 10 10 2 black grey20 0.5 1 0 7 1 4 4 11 10 2 black grey20 0.5 1 0 8 1 3 4 10 10 2 black grey20 0.5 1 0 9 1 4 4 9 10 2 black grey20 0.5 1 0 10 1 5 4 10 10 2 black grey20 0.5 1 0 11 1 8 7 4 4 3 black grey20 0.5 1 0 12 1 7 7 5 4 3 black grey20 0.5 1 0 13 1 6 7 4 4 3 black grey20 0.5 1 0 14 1 7 7 3 4 3 black grey20 0.5 1 0 15 1 8 7 4 4 3 black grey20 0.5 1 0 16 1 8 7 22 22 4 black grey20 0.5 1 0 17 1 7 7 23 22 4 black grey20 0.5 1 0 18 1 6 7 22 22 4 black grey20 0.5 1 0 19 1 7 7 21 22 4 black grey20 0.5 1 0 20 1 8 7 22 22 4 black grey20 0.5 1 0 21 1 9 8 16 16 5 black grey20 0.5 1 0 22 1 8 8 17 16 5 black grey20 0.5 1 0 23 1 7 8 16 16 5 black grey20 0.5 1 0 24 1 8 8 15 16 5 black grey20 0.5 1 0 25 1 9 8 16 16 5 black grey20 0.5 1 0 ``` ] --- count: false .panel1-7-auto[ ```r layer_data(last_plot(), 2) layer_data(last_plot(), 3) ``` ] .panel2-7-auto[ ``` PANEL x x0 y y0 group colour fill size linetype alpha 1 1 5 4 2 2 1 NA grey20 0.5 1 0.2 2 1 4 4 3 2 1 NA grey20 0.5 1 0.2 3 1 3 4 2 2 1 NA grey20 0.5 1 0.2 4 1 4 4 1 2 1 NA grey20 0.5 1 0.2 5 1 5 4 2 2 1 NA grey20 0.5 1 0.2 6 1 5 4 10 10 2 NA grey20 0.5 1 0.2 7 1 4 4 11 10 2 NA grey20 0.5 1 0.2 8 1 3 4 10 10 2 NA grey20 0.5 1 0.2 9 1 4 4 9 10 2 NA grey20 0.5 1 0.2 10 1 5 4 10 10 2 NA grey20 0.5 1 0.2 11 1 8 7 4 4 3 NA grey20 0.5 1 0.2 12 1 7 7 5 4 3 NA grey20 0.5 1 0.2 13 1 6 7 4 4 3 NA grey20 0.5 1 0.2 14 1 7 7 3 4 3 NA grey20 0.5 1 0.2 15 1 8 7 4 4 3 NA grey20 0.5 1 0.2 16 1 8 7 22 22 4 NA grey20 0.5 1 0.2 17 1 7 7 23 22 4 NA grey20 0.5 1 0.2 18 1 6 7 22 22 4 NA grey20 0.5 1 0.2 19 1 7 7 21 22 4 NA grey20 0.5 1 0.2 20 1 8 7 22 22 4 NA grey20 0.5 1 0.2 21 1 9 8 16 16 5 NA grey20 0.5 1 0.2 22 1 8 8 17 16 5 NA grey20 0.5 1 0.2 23 1 7 8 16 16 5 NA grey20 0.5 1 0.2 24 1 8 8 15 16 5 NA grey20 0.5 1 0.2 25 1 9 8 16 16 5 NA grey20 0.5 1 0.2 ``` ``` PANEL x x0 y y0 group colour fill size linetype alpha 1 1 5 4 2 2 1 black grey20 0.5 1 0 2 1 4 4 3 2 1 black grey20 0.5 1 0 3 1 3 4 2 2 1 black grey20 0.5 1 0 4 1 4 4 1 2 1 black grey20 0.5 1 0 5 1 5 4 2 2 1 black grey20 0.5 1 0 6 1 5 4 10 10 2 black grey20 0.5 1 0 7 1 4 4 11 10 2 black grey20 0.5 1 0 8 1 3 4 10 10 2 black grey20 0.5 1 0 9 1 4 4 9 10 2 black grey20 0.5 1 0 10 1 5 4 10 10 2 black grey20 0.5 1 0 11 1 8 7 4 4 3 black grey20 0.5 1 0 12 1 7 7 5 4 3 black grey20 0.5 1 0 13 1 6 7 4 4 3 black grey20 0.5 1 0 14 1 7 7 3 4 3 black grey20 0.5 1 0 15 1 8 7 4 4 3 black grey20 0.5 1 0 16 1 8 7 22 22 4 black grey20 0.5 1 0 17 1 7 7 23 22 4 black grey20 0.5 1 0 18 1 6 7 22 22 4 black grey20 0.5 1 0 19 1 7 7 21 22 4 black grey20 0.5 1 0 20 1 8 7 22 22 4 black grey20 0.5 1 0 21 1 9 8 16 16 5 black grey20 0.5 1 0 22 1 8 8 17 16 5 black grey20 0.5 1 0 23 1 7 8 16 16 5 black grey20 0.5 1 0 24 1 8 8 15 16 5 black grey20 0.5 1 0 25 1 9 8 16 16 5 black grey20 0.5 1 0 ``` ] <style> .panel1-7-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-7-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-7-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>