Intro Thoughts

Status Quo

library(tidyverse)

Titanic %>% 
  data.frame() %>% 
  uncount(weights = Freq) ->
titanic

Experiment

titanic %>% 
  ggplot() +
  aes(x = Sex) + 
  geom_bar()

last_plot() + 
  facet_grid(~Age)

geom_bar
## function (mapping = NULL, data = NULL, stat = "count", position = "stack", 
##     ..., just = 0.5, width = NULL, na.rm = FALSE, orientation = NA, 
##     show.legend = NA, inherit.aes = TRUE) 
## {
##     layer(data = data, mapping = mapping, stat = stat, geom = GeomBar, 
##         position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
##         params = list2(just = just, width = width, na.rm = na.rm, 
##             orientation = orientation, ...))
## }
## <bytecode: 0x7fdb8a517120>
## <environment: namespace:ggplot2>
ggplot2::StatCount
## <ggproto object: Class StatCount, Stat, gg>
##     aesthetics: function
##     compute_group: function
##     compute_layer: function
##     compute_panel: function
##     default_aes: uneval
##     dropped_aes: weight
##     extra_params: na.rm orientation
##     finish_layer: function
##     non_missing_aes: 
##     optional_aes: 
##     parameters: function
##     required_aes: x|y
##     retransform: TRUE
##     setup_data: function
##     setup_params: function
##     super:  <ggproto object: Class Stat, gg>
ggplot2::StatCount$compute_group
## <ggproto method>
##   <Wrapper function>
##     function (...) 
## compute_group(..., self = self)
## 
##   <Inner function (f)>
##     function (self, data, scales, width = NULL, flipped_aes = FALSE) 
## {
##     data <- flip_data(data, flipped_aes)
##     x <- data$x
##     weight <- data$weight %||% rep(1, length(x))
##     count <- as.numeric(tapply(weight, x, sum, na.rm = TRUE))
##     count[is.na(count)] <- 0
##     bars <- data_frame0(count = count, prop = count/sum(abs(count)), 
##         x = sort(unique0(x)), width = width, flipped_aes = flipped_aes, 
##         .size = length(count))
##     flip_data(bars, flipped_aes)
## }
titanic %>% 
  rename(x = Sex) %>% 
  ggplot2::StatCount$compute_group()
##   count      prop      x flipped_aes
## 1  1731 0.7864607   Male       FALSE
## 2   470 0.2135393 Female       FALSE
titanic %>% 
  rename(x = Sex, fill = Age) %>% 
  ggplot2::StatCount$compute_group()
##   count      prop      x flipped_aes
## 1  1731 0.7864607   Male       FALSE
## 2   470 0.2135393 Female       FALSE
titanic %>% 
  rename(x = Sex) %>% 
  ggplot2::StatCount$compute_group()
##   count      prop      x flipped_aes
## 1  1731 0.7864607   Male       FALSE
## 2   470 0.2135393 Female       FALSE
titanic %>% 
  ggplot() +
  aes(x = Sex) + 
  geom_bar() + 
  geom_text(stat = StatCount, 
            aes(label = after_stat(count)),
            vjust = -.15)

uh-oh… saw different behavior w/ mtcars.

titanic %>% 
  ggplot() +
  aes(x = Sex) + 
  geom_bar() + 
  geom_text(stat = StatCount, 
            aes(label = after_stat(100*prop)),
            vjust = -.15)

Closing remarks, Other Relevant Work, Caveats