class: inverse, left, bottom background-image: url(https://images.unsplash.com/photo-1603779702200-95b2785f2083?q=80&w=1587&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D) background-size: cover # .Large[# Making pies with ggplot2] ## .small[featuring `coord_polar(theta = "y")`, `geom_bar(position = "fill")`, `stage()`, and {ggtrace}] #### .tiny[Gina Reynolds | 2023-10-25 |Image credit: Nathan Dumlao, Upsplash] ??? Title --- count: false .panel1-feature-auto[ ```r *library(tidyverse) ``` ] .panel2-feature-auto[ ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) *diamonds ``` ] .panel2-feature-auto[ ``` ## # A tibble: 53,940 × 10 ## carat cut color clarity depth table price x y z ## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl> ## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 ## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 ## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 ## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63 ## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 ## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 ## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47 ## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53 ## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49 ## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39 ## # ℹ 53,930 more rows ``` ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% *ggplot() ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + * aes(x = 0) ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + * geom_bar(position = "fill") ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + * aes(fill = cut) ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + aes(fill = cut) + * stat_count( * geom = "text", position = "fill", * color = "white", size = 8, * aes(x = .25, * label = after_stat( * count*100/sum(count)) |> * round(1) |> * paste0("%"))) ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + aes(fill = cut) + stat_count( geom = "text", position = "fill", color = "white", size = 8, aes(x = .25, label = after_stat( count*100/sum(count)) |> round(1) |> paste0("%"))) + # usually would include this in above function * aes(y = stage(start = NULL, * after_stat = count, * after_scale = (ymin + * ymax)/2)) ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + aes(fill = cut) + stat_count( geom = "text", position = "fill", color = "white", size = 8, aes(x = .25, label = after_stat( count*100/sum(count)) |> round(1) |> paste0("%"))) + # usually would include this in above function aes(y = stage(start = NULL, after_stat = count, after_scale = (ymin + ymax)/2)) + * coord_polar(theta = "y") ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + aes(fill = cut) + stat_count( geom = "text", position = "fill", color = "white", size = 8, aes(x = .25, label = after_stat( count*100/sum(count)) |> round(1) |> paste0("%"))) + # usually would include this in above function aes(y = stage(start = NULL, after_stat = count, after_scale = (ymin + ymax)/2)) + coord_polar(theta = "y") + * theme_void() ``` ] .panel2-feature-auto[ ![](ggplot-pie-charts_files/figure-html/feature_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) diamonds %>% ggplot() + aes(x = 0) + geom_bar(position = "fill") + aes(fill = cut) + stat_count( geom = "text", position = "fill", color = "white", size = 8, aes(x = .25, label = after_stat( count*100/sum(count)) |> round(1) |> paste0("%"))) + # usually would include this in above function aes(y = stage(start = NULL, after_stat = count, after_scale = (ymin + ymax)/2)) + coord_polar(theta = "y") + theme_void() -> *classic_pie ``` ] .panel2-feature-auto[ ] <style> .panel1-feature-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-feature-auto { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-feature-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- # ggtrace may help clarify strategy? ```r ggtrace::layer_before_stat(classic_pie, i = 2) ``` ``` ## # A tibble: 53,940 × 4 ## x fill PANEL group ## <dbl> <ord> <fct> <int> ## 1 0.25 Ideal 1 5 ## 2 0.25 Premium 1 4 ## 3 0.25 Good 1 2 ## 4 0.25 Premium 1 4 ## 5 0.25 Good 1 2 ## 6 0.25 Very Good 1 3 ## 7 0.25 Very Good 1 3 ## 8 0.25 Very Good 1 3 ## 9 0.25 Fair 1 1 ## 10 0.25 Very Good 1 3 ## # ℹ 53,930 more rows ``` --- # ggtrace may help clarify strategy? ```r ggtrace::layer_after_stat(classic_pie,i = 2) ``` ``` ## # A tibble: 5 × 8 ## count prop x width flipped_aes fill PANEL group ## <dbl> <dbl> <dbl> <dbl> <lgl> <ord> <fct> <int> ## 1 1610 1 0.25 0.9 FALSE Fair 1 1 ## 2 4906 1 0.25 0.9 FALSE Good 1 2 ## 3 12082 1 0.25 0.9 FALSE Very Good 1 3 ## 4 13791 1 0.25 0.9 FALSE Premium 1 4 ## 5 21551 1 0.25 0.9 FALSE Ideal 1 5 ``` --- # ggtrace may help clarify strategy? ```r ggtrace::layer_after_scale(classic_pie, i = 2) %>% data.frame() ``` ``` ## y fill label count prop x width flipped_aes PANEL group ## 1 0.9850760 #440154FF 3% 1610 1 0.25 0.9 FALSE 1 1 ## 2 0.9246756 #3B528BFF 9.1% 4906 1 0.25 0.9 FALSE 1 2 ## 3 0.7672043 #21908CFF 22.4% 12082 1 0.25 0.9 FALSE 1 3 ## 4 0.5273730 #5DC863FF 25.6% 13791 1 0.25 0.9 FALSE 1 4 ## 5 0.1997683 #FDE725FF 40% 21551 1 0.25 0.9 FALSE 1 5 ## ymax xmin xmax ymin colour size angle hjust vjust alpha family ## 1 1.0000000 0.25 0.25 0.9701520 white 8 0 0.5 0.5 NA ## 2 0.9701520 0.25 0.25 0.8791991 white 8 0 0.5 0.5 NA ## 3 0.8791991 0.25 0.25 0.6552095 white 8 0 0.5 0.5 NA ## 4 0.6552095 0.25 0.25 0.3995365 white 8 0 0.5 0.5 NA ## 5 0.3995365 0.25 0.25 0.0000000 white 8 0 0.5 0.5 NA ## fontface lineheight ## 1 1 1.2 ## 2 1 1.2 ## 3 1 1.2 ## 4 1 1.2 ## 5 1 1.2 ``` --- ### Contribute - https://yjunechoe.github.io/ggtrace/ --- ### Check out flipbookr, used to build this featurette - https://github.com/EvaMaeRey/flipbookr - discussion: https://github.com/EvaMaeRey/flipbookr/blob/master/docs/draft_jasa_submission.pdf --- ### Check out more featurettes - https://EvaMaeRey.github.io/featurette