density_histogram | R Documentation |
Histogram density estimator.
Supports automatic partial function application.
density_histogram(
x,
weights = NULL,
breaks = "Scott",
align = "none",
outline_bars = FALSE,
na.rm = FALSE,
...,
range_only = FALSE
)
x |
numeric vector containing a sample to compute a density estimate for. |
weights |
optional numeric vector of weights to apply to |
breaks |
Determines the breakpoints defining bins. Defaults to
For example, |
align |
Determines how to align the breakpoints defining bins. Default
(
For example, |
outline_bars |
Should outlines in between the bars (i.e. density values of 0) be included? |
na.rm |
Should missing ( |
... |
Additional arguments (ignored). |
range_only |
If |
An object of class "density"
, mimicking the output format of
stats::density()
, with the following components:
x
: The grid of points at which the density was estimated.
y
: The estimated density values.
bw
: The bandwidth.
n
: The sample size of the x
input argument.
call
: The call used to produce the result, as a quoted expression.
data.name
: The deparsed name of the x
input argument.
has.na
: Always FALSE
(for compatibility).
cdf
: Values of the (possibly weighted) empirical cumulative distribution
function at x
. See weighted_ecdf()
.
This allows existing methods for density objects, like print()
and plot()
, to work if desired.
This output format (and in particular, the x
and y
components) is also
the format expected by the density
argument of the stat_slabinterval()
and the smooth_
family of functions.
Other density estimators:
density_bounded()
,
density_unbounded()
library(distributional)
library(dplyr)
library(ggplot2)
# For compatibility with existing code, the return type of density_unbounded()
# is the same as stats::density(), ...
set.seed(123)
x = rbeta(5000, 1, 3)
d = density_histogram(x)
d
# ... thus, while designed for use with the `density` argument of
# stat_slabinterval(), output from density_histogram() can also be used with
# base::plot():
plot(d)
# here we'll use the same data as above with stat_slab():
data.frame(x) %>%
ggplot() +
stat_slab(
aes(xdist = dist), data = data.frame(dist = dist_beta(1, 3)),
alpha = 0.25
) +
stat_slab(aes(x), density = "histogram", fill = NA, color = "#d95f02", alpha = 0.5) +
scale_thickness_shared() +
theme_ggdist()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.