Part 54 Changing the stat

In the process of connecting data -> mapping, there is some statistical computation. In many cases, this computation is identity(). For example, with geom_point(), it plots the points directly based on x and y values. Other times, there is more of a computation.

  • stat_count()
  • stat_bin()
  • stat_density()

geom_*() functions usually come with a reasonable default stat_*() and subsequent mapping. For example, geom_histogram() does stat_bin() to bin the continuous values, then aes() maps y to the count in each bin.

But sometimes you might want to change this mapping after the stat. Do that with after_stat() inside aes().

bw <- .3

h1 <- ggplot(mpg) +
  aes(x = displ) +
  geom_histogram(binwidth = bw, fill = "skyblue")

## histogram with density
h2 <- ggplot(mpg) +
  aes(x = displ) +
  geom_histogram(aes(y = after_stat(density)),
                 binwidth = bw, fill = "skyblue")

## histogram with proportion
h3 <- ggplot(mpg) +
  aes(x = displ) +
  geom_histogram(aes(y = after_stat(count / sum(count))),
                 binwidth = bw, fill = "skyblue")

h1 + h2 + h3

## histogram with density curve
ggplot(mpg) +
  aes(displ) +
    geom_histogram(aes(y = after_stat(count)), 
                   binwidth = bw, fill = "skyblue") +
    geom_density(aes(y = after_stat(count) * bw)) +
    theme_minimal()