Experiment
# regulat histogram...
cars |>
ggplot() +
aes(x = dist) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

# bricky histogram...
last_plot() +
aes(group = 1:nrow(cars),
color = I("grey"))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

library(ggplot2)
compute_group_histbricks = function(data, scales, binwidth = NULL, bins = NULL, center = NULL,
boundary = NULL, closed = c("right", "left"), pad = FALSE,
breaks = NULL, flipped_aes = FALSE, drop = "none") {
data |>
StatBin$compute_group(
scales = scales, binwidth = binwidth, bins = bins,
center = center, boundary = boundary, closed = closed,
pad = pad, breaks = breaks, flipped_aes = flipped_aes, drop = drop
) |>
tidyr::uncount(count) |>
dplyr::mutate(count = 1)
}
StatBinBricks <- ggproto("StatBinBricks",
StatBin,
compute_group = compute_group_histbricks)
ggplot(cars) +
aes(x = dist) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

ggplot(cars) +
aes(x = dist) +
statexpress::qlayer(geom = GeomBar,
stat = StatBinBricks,
position = "stack",
color = "lightgrey")
## `stat_bin_bricks()` using `bins = 30`. Pick better value `binwidth`.

ggplot(cars) +
aes(x = dist) +
statexpress::qlayer(geom = GeomBar,
stat = StatBinBricks,
position = "stack",
color = "lightgrey", drop = FALSE)
## `stat_bin_bricks()` using `bins = 30`. Pick better value `binwidth`.

last_plot() +
aes(fill = speed > 15)
## `stat_bin_bricks()` using `bins = 30`. Pick better value `binwidth`.

last_plot() +
facet_wrap(facets = vars(dist > 50),
ncol = 1)
## `stat_bin_bricks()` using `bins = 30`. Pick better value `binwidth`.
