breaks | R Documentation |
Methods for determining breaks (bins) in histograms, as used in the breaks
argument to density_histogram()
.
Supports automatic partial function application.
breaks_fixed(x, weights = NULL, width = 1)
breaks_Sturges(x, weights = NULL)
breaks_Scott(x, weights = NULL)
breaks_FD(x, weights = NULL, digits = 5)
breaks_quantiles(x, weights = NULL, max_n = "Scott", min_width = 0.5)
x |
A numeric vector giving a sample. |
weights |
A numeric vector of |
width |
For |
digits |
For |
max_n |
For |
min_width |
For |
These functions take a sample and its weights and return a value suitable for
the breaks
argument to density_histogram()
that will determine the histogram
breaks.
breaks_fixed()
allows you to manually specify a fixed bin width.
breaks_Sturges()
, breaks_Scott()
, and breaks_FD()
implement weighted
versions of their corresponding base functions. They return a scalar
numeric giving the number of bins. See nclass.Sturges()
, nclass.scott()
,
and nclass.FD()
.
breaks_quantiles()
constructs irregularly-sized bins using max_n + 1
(possibly weighted) quantiles of x
. The final number of bins is
at most max_n
, as small bins (ones whose bin width is less than half
the range of the data divided by max_n
times min_width
) will be merged
into adjacent bins.
Either a single number (giving the number of bins) or a vector giving the edges between bins.
density_histogram()
, align
library(ggplot2)
set.seed(1234)
x = rnorm(200, 1, 2)
# Let's compare the different break-selection algorithms on this data:
ggplot(data.frame(x), aes(x)) +
stat_slab(
aes(y = "breaks_fixed(width = 0.5)"),
density = "histogram",
breaks = breaks_fixed(width = 0.5),
outline_bars = TRUE,
color = "black",
) +
stat_slab(
aes(y = "breaks_Sturges()\nor 'Sturges'"),
density = "histogram",
breaks = "Sturges",
outline_bars = TRUE,
color = "black",
) +
stat_slab(
aes(y = "breaks_Scott()\nor 'Scott'"),
density = "histogram",
breaks = "Scott",
outline_bars = TRUE,
color = "black",
) +
stat_slab(
aes(y = "breaks_FD()\nor 'FD'"),
density = "histogram",
breaks = "FD",
outline_bars = TRUE,
color = "black",
) +
geom_point(aes(y = 0.7), alpha = 0.5) +
labs(
subtitle = "ggdist::stat_slab(density = 'histogram', ...)",
y = "breaks =",
x = NULL
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.