class: inverse, left, bottom background-image: url(https://images.unsplash.com/photo-1631013636761-c533d81e96a4?auto=format&fit=crop&q=60&w=500&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NHx8ZGlhbW9uZHN8ZW58MHx8MHx8fDA%3D) background-size: cover # .Large[# ggplot2::diamonds bar chart featurette] ## .small[featuring ggplot2::stat_count()] #### .tiny[Gina Reynolds | 2023-10-28 |Image credit: Tahlia Doyle, Upsplash] ??? Title --- count: false .panel1-feature-auto[ ```r *library(tidyverse) ``` ] .panel2-feature-auto[ ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) *ggplot2::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) ggplot2::diamonds %>% * ggplot() ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_03_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + * aes(y = cut) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_04_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + * geom_bar(width = .5, * color = alpha("black", 0 )) # ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_05_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # * aes(fill = after_stat(count == max(count))) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_06_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + * scale_fill_manual(values = * c("darkgrey", "midnightblue")) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_07_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + * stat_count(geom = "text", * aes(label = after_stat(count)), * size = 6) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_08_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + * aes(hjust = * after_stat( * ifelse(count/sum(count) > .05, 1.2, -.2))) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_09_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + aes(hjust = after_stat( ifelse(count/sum(count) > .05, 1.2, -.2))) + * aes(color = after_stat(count/sum(count) > .05)) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_10_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + aes(hjust = after_stat( ifelse(count/sum(count) > .05, 1.2, -.2))) + aes(color = after_stat(count/sum(count) > .05)) + * scale_color_manual(values = c("grey25", * "grey95")) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_11_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + aes(hjust = after_stat( ifelse(count/sum(count) > .05, 1.2, -.2))) + aes(color = after_stat(count/sum(count) > .05)) + scale_color_manual(values = c("grey25", "grey95")) + * stat_count(geom = "text", * aes(label = cut), * x = 0, * color = "black", * size = 7, * hjust = 0, * vjust = -2.25) ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_12_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + aes(hjust = after_stat( ifelse(count/sum(count) > .05, 1.2, -.2))) + aes(color = after_stat(count/sum(count) > .05)) + scale_color_manual(values = c("grey25", "grey95")) + stat_count(geom = "text", aes(label = cut), x = 0, color = "black", size = 7, hjust = 0, vjust = -2.25) + * theme_void() ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_13_output-1.png)<!-- --> ] --- count: false .panel1-feature-auto[ ```r library(tidyverse) ggplot2::diamonds %>% ggplot() + aes(y = cut) + geom_bar(width = .5, color = alpha("black", 0 )) + # aes(fill = after_stat(count == max(count))) + scale_fill_manual(values = c("darkgrey", "midnightblue")) + stat_count(geom = "text", aes(label = after_stat(count)), size = 6) + aes(hjust = after_stat( ifelse(count/sum(count) > .05, 1.2, -.2))) + aes(color = after_stat(count/sum(count) > .05)) + scale_color_manual(values = c("grey25", "grey95")) + stat_count(geom = "text", aes(label = cut), x = 0, color = "black", size = 7, hjust = 0, vjust = -2.25) + theme_void() + * theme(legend.position = "none") ``` ] .panel2-feature-auto[ ![](diamonds-barlabs_files/figure-html/feature_auto_14_output-1.png)<!-- --> ] <style> .panel1-feature-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-feature-auto { color: black; width: 39.2%; 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> --- --- ### Contribute - https://github.com/everyday-analytics/ggbarlabs --- ### 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