gf_squareplot()gf_squareplot() creates histograms where individual data
points are visible as stacked unit rectangles. Instead of abstract bars,
each observation becomes a countable square, making sample size and
distribution shape tangible.
This is particularly useful for teaching statistical concepts like sampling distributions and hypothesis testing, where students benefit from seeing that “n = 47” means 47 actual squares.
Pass a formula and data frame, just like other gf_*
functions:
The bars parameter controls how the histogram is
displayed:
"none" (default): Individual squares only"outline": Squares with bar outlines around each
bin"solid": Traditional filled barsYou can customize fill color, binwidth, and axis
limits:
For integer-valued data with a small range,
gf_squareplot() automatically selects a
binwidth of 1, so each integer gets its own column:
int_data <- data.frame(rolls = sample(1:6, 30, replace = TRUE))
gf_squareplot(~rolls, data = int_data)When any bin has more than 75 observations, the function
automatically switches to solid bars to keep the display readable. You
can opt into subdivision instead with
auto_subdivide = TRUE, which splits wide bins into
sub-columns so rectangles remain countable:
Show a dashed line at the sample mean:
The show_dgp = TRUE option adds a teaching overlay for
hypothesis testing contexts. It shows:
set.seed(42)
samp_dist <- do(100) * b1(Thumb ~ Height, data = sample(Fingers, 30))
gf_squareplot(~b1, data = samp_dist,
show_dgp = TRUE,
show_mean = TRUE,
xrange = c(-0.5, 1.5),
xbreaks = seq(-0.5, 1.5, by = 0.25))