class: center, middle, inverse, title-slide .title[ # after_stat, after_scale ] .subtitle[ ## A flipbook and layer_data treatment ] --- Thanks for joining! This flipbook will look at the topic of fine-tuned aesthetic evaluation using after_stat, after_scale, and stage. Are you the type of person that learned ggplot2 before these mechanisms were put together, and just ignore them (that's me!) -- This book is for you! --- Are you the kind of person that can copy and paste after_stat, after_scale, and stage code but really has no idea what's going on? (that'd be me if I actually used the mechanisms) -- This book is for you! --- Are you the kind of person that admires really beautifully and thoughtfully crafted data vizualizations, and have noticed the authors use of after_stat, after_scale, and stage, but have thought, I'm not sure it's worth all the fuss. -- This book is for you! The source material is basically an expansion of https://ggplot2.tidyverse.org/reference/aes_eval.html --- count: false .panel1-start-auto[ ```r *ggplot(faithful, aes(x = waiting)) ``` ] .panel2-start-auto[ ![](after_stat_etc_files/figure-html/start_auto_01_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r ggplot(faithful, aes(x = waiting)) + * geom_rug() ``` ] .panel2-start-auto[ ![](after_stat_etc_files/figure-html/start_auto_02_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r ggplot(faithful, aes(x = waiting)) + geom_rug() + * geom_histogram(bins = 12) ``` ] .panel2-start-auto[ ![](after_stat_etc_files/figure-html/start_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-start-auto[ ```r ggplot(faithful, aes(x = waiting)) + geom_rug() + geom_histogram(bins = 12) *layer_data(last_plot(), 2) ``` ] .panel2-start-auto[ ![](after_stat_etc_files/figure-html/start_auto_04_output-1.png)<!-- --> ``` y count x xmin xmax density ncount ndensity 1 4 4 43.36364 40.95455 45.77273 0.003052164 0.07017544 0.07017544 2 22 22 48.18182 45.77273 50.59091 0.016786903 0.38596491 0.38596491 3 33 33 53.00000 50.59091 55.40909 0.025180355 0.57894737 0.57894737 4 24 24 57.81818 55.40909 60.22727 0.018312986 0.42105263 0.42105263 5 14 14 62.63636 60.22727 65.04545 0.010682575 0.24561404 0.24561404 6 6 6 67.45455 65.04545 69.86364 0.004578246 0.10526316 0.10526316 7 23 23 72.27273 69.86364 74.68182 0.017549945 0.40350877 0.40350877 8 54 54 77.09091 74.68182 79.50000 0.041204218 0.94736842 0.94736842 9 57 57 81.90909 79.50000 84.31818 0.043493341 1.00000000 1.00000000 10 23 23 86.72727 84.31818 89.13636 0.017549945 0.40350877 0.40350877 11 10 10 91.54545 89.13636 93.95455 0.007630411 0.17543860 0.17543860 12 2 2 96.36364 93.95455 98.77273 0.001526082 0.03508772 0.03508772 flipped_aes PANEL group ymin ymax colour fill linewidth linetype alpha 1 FALSE 1 -1 0 4 NA grey35 0.5 1 NA 2 FALSE 1 -1 0 22 NA grey35 0.5 1 NA 3 FALSE 1 -1 0 33 NA grey35 0.5 1 NA 4 FALSE 1 -1 0 24 NA grey35 0.5 1 NA 5 FALSE 1 -1 0 14 NA grey35 0.5 1 NA 6 FALSE 1 -1 0 6 NA grey35 0.5 1 NA 7 FALSE 1 -1 0 23 NA grey35 0.5 1 NA 8 FALSE 1 -1 0 54 NA grey35 0.5 1 NA 9 FALSE 1 -1 0 57 NA grey35 0.5 1 NA 10 FALSE 1 -1 0 23 NA grey35 0.5 1 NA 11 FALSE 1 -1 0 10 NA grey35 0.5 1 NA 12 FALSE 1 -1 0 2 NA grey35 0.5 1 NA ``` ] --- count: false .panel1-start-auto[ ```r ggplot(faithful, aes(x = waiting)) + geom_rug() + geom_histogram(bins = 12) layer_data(last_plot(), 2) %>% * .[1:6] ``` ] .panel2-start-auto[ ![](after_stat_etc_files/figure-html/start_auto_05_output-1.png)<!-- --> ``` y count x xmin xmax density 1 4 4 43.36364 40.95455 45.77273 0.003052164 2 22 22 48.18182 45.77273 50.59091 0.016786903 3 33 33 53.00000 50.59091 55.40909 0.025180355 4 24 24 57.81818 55.40909 60.22727 0.018312986 5 14 14 62.63636 60.22727 65.04545 0.010682575 6 6 6 67.45455 65.04545 69.86364 0.004578246 7 23 23 72.27273 69.86364 74.68182 0.017549945 8 54 54 77.09091 74.68182 79.50000 0.041204218 9 57 57 81.90909 79.50000 84.31818 0.043493341 10 23 23 86.72727 84.31818 89.13636 0.017549945 11 10 10 91.54545 89.13636 93.95455 0.007630411 12 2 2 96.36364 93.95455 98.77273 0.001526082 ``` ] <style> .panel1-start-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-start-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-start-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-after-1[ ```r ggplot(faithful, aes(x = waiting)) + geom_rug() + geom_histogram(bins = 12, aes(y = after_stat(density)) ) layer_data(last_plot(), 2) %>% .[1:6] ``` ] .panel2-after-1[ ![](after_stat_etc_files/figure-html/after_1_01_output-1.png)<!-- --> ``` y count x xmin xmax density 1 0.003052164 4 43.36364 40.95455 45.77273 0.003052164 2 0.016786903 22 48.18182 45.77273 50.59091 0.016786903 3 0.025180355 33 53.00000 50.59091 55.40909 0.025180355 4 0.018312986 24 57.81818 55.40909 60.22727 0.018312986 5 0.010682575 14 62.63636 60.22727 65.04545 0.010682575 6 0.004578246 6 67.45455 65.04545 69.86364 0.004578246 7 0.017549945 23 72.27273 69.86364 74.68182 0.017549945 8 0.041204218 54 77.09091 74.68182 79.50000 0.041204218 9 0.043493341 57 81.90909 79.50000 84.31818 0.043493341 10 0.017549945 23 86.72727 84.31818 89.13636 0.017549945 11 0.007630411 10 91.54545 89.13636 93.95455 0.007630411 12 0.001526082 2 96.36364 93.95455 98.77273 0.001526082 ``` ] <style> .panel1-after-1 { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-after-1 { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-after-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-after2-1[ ```r ggplot(faithful, aes(x = waiting)) + geom_rug() + geom_histogram(bins = 12, aes(y = ..density..) ) layer_data(last_plot(), 2) %>% .[1:6] ``` ] .panel2-after2-1[ ![](after_stat_etc_files/figure-html/after2_1_01_output-1.png)<!-- --> ``` y count x xmin xmax density 1 0.003052164 4 43.36364 40.95455 45.77273 0.003052164 2 0.016786903 22 48.18182 45.77273 50.59091 0.016786903 3 0.025180355 33 53.00000 50.59091 55.40909 0.025180355 4 0.018312986 24 57.81818 55.40909 60.22727 0.018312986 5 0.010682575 14 62.63636 60.22727 65.04545 0.010682575 6 0.004578246 6 67.45455 65.04545 69.86364 0.004578246 7 0.017549945 23 72.27273 69.86364 74.68182 0.017549945 8 0.041204218 54 77.09091 74.68182 79.50000 0.041204218 9 0.043493341 57 81.90909 79.50000 84.31818 0.043493341 10 0.017549945 23 86.72727 84.31818 89.13636 0.017549945 11 0.007630411 10 91.54545 89.13636 93.95455 0.007630411 12 0.001526082 2 96.36364 93.95455 98.77273 0.001526082 ``` ] <style> .panel1-after2-1 { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-after2-1 { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-after2-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ```r # faithful %>% # rename(x = waiting, y) ``` --- count: false .panel1-ok-1[ ```r # The exact colour is known after scale transformation ggplot(mpg, aes(cty, colour = factor(cyl))) + geom_density() layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok-1[ ![](after_stat_etc_files/figure-html/ok_1_01_output-1.png)<!-- --> ``` colour y x 1 #F8766D 8.805984e-14 9.000000 2 #F8766D 1.334346e-13 9.050881 3 #F8766D 2.041311e-13 9.101761 4 #F8766D 3.176278e-13 9.152642 5 #F8766D 4.876455e-13 9.203523 ``` ] <style> .panel1-ok-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok2-1[ ```r # We re-use colour properties for the fill without a separate fill scale ggplot(mpg, aes(cty, colour = factor(cyl))) + geom_density(aes( fill = after_scale(alpha(colour, 0.3)) )) layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok2-1[ ![](after_stat_etc_files/figure-html/ok2_1_01_output-1.png)<!-- --> ``` fill colour y 1 #F8766D4C #F8766D 8.805984e-14 2 #F8766D4C #F8766D 1.334346e-13 3 #F8766D4C #F8766D 2.041311e-13 4 #F8766D4C #F8766D 3.176278e-13 5 #F8766D4C #F8766D 4.876455e-13 ``` ] <style> .panel1-ok2-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok2-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok2-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok222-1[ ```r # We re-use colour properties for the fill without a separate fill scale ggplot(mpg, aes(class, hwy)) + geom_boxplot( aes(fill = class) ) layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok222-1[ ![](after_stat_etc_files/figure-html/ok222_1_01_output-1.png)<!-- --> ``` fill ymin lower 1 #F8766D 23 24 2 #C49A00 23 26 3 #53B400 23 26 4 #00C094 21 22 5 #00B6EB 15 16 ``` ] <style> .panel1-ok222-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok222-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok222-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok223-1[ ```r # We re-use colour properties for the fill without a separate fill scale ggplot(mpg, aes(class, hwy)) + geom_boxplot( aes(fill = class), alpha = 0.4 ) layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok223-1[ ![](after_stat_etc_files/figure-html/ok223_1_01_output-1.png)<!-- --> ``` fill ymin lower 1 #F8766D 23 24 2 #C49A00 23 26 3 #53B400 23 26 4 #00C094 21 22 5 #00B6EB 15 16 ``` ] <style> .panel1-ok223-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok223-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok223-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok300-1[ ```r # The exact colour is known after scale transformation ggplot(mpg, aes(class, hwy)) + geom_boxplot( aes(fill = stage(class, after_scale = alpha(fill, 0.4))) ) layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok300-1[ ![](after_stat_etc_files/figure-html/ok300_1_01_output-1.png)<!-- --> ``` fill ymin lower 1 #F8766D66 23 24 2 #C49A0066 23 26 3 #53B40066 23 26 4 #00C09466 21 22 5 #00B6EB66 15 16 ``` ] <style> .panel1-ok300-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok300-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok300-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok38-1[ ```r # The exact colour is known after scale transformation ggplot(mpg, aes(class, hwy)) + geom_boxplot( aes(fill = stage(class, after_scale = alpha(fill, 0.4))) ) layer_data(last_plot(), 1)[1:5, 1:3] ``` ] .panel2-ok38-1[ ![](after_stat_etc_files/figure-html/ok38_1_01_output-1.png)<!-- --> ``` fill ymin lower 1 #F8766D66 23 24 2 #C49A0066 23 26 3 #53B40066 23 26 4 #00C09466 21 22 5 #00B6EB66 15 16 ``` ] <style> .panel1-ok38-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok38-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok38-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-ok232-1[ ```r # We re-use colour properties for the fill without a separate fill scale ggplot(mpg) + aes(x = class, y = displ) + geom_violin() + stat_summary( # layer is text geom = "text", # compute variables on-the-fly fun.data = ~ round(data.frame(my_mean = mean(.x), my_sd = sd(.x)), 2), # use those vars in plot mapping = aes(y = stage(displ, after_stat = 8), label = after_stat( paste(my_mean, "±", my_sd) ) ) ) layer_data(last_plot(), 2)[1:5, 1:7] ``` ] .panel2-ok232-1[ ![](after_stat_etc_files/figure-html/ok232_1_01_output-1.png)<!-- --> ``` y label x group my_mean my_sd PANEL 1 8 6.16 ± 0.53 1 1 6.16 0.53 1 2 8 2.33 ± 0.45 2 2 2.33 0.45 1 3 8 2.92 ± 0.72 3 3 2.92 0.72 1 4 8 3.39 ± 0.45 4 4 3.39 0.45 1 5 8 4.42 ± 0.83 5 5 4.42 0.83 1 ``` ] <style> .panel1-ok232-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-ok232-1 { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-ok232-1 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>