Skip to contents

Kernel density estimation plot from numeric data, with optional grouping for multiple traces. A list input creates one density trace per vector when ungrouped, or one trace per variable/group combination when group is supplied. Curves use stats::density() with its bandwidth-scaled kernels and evaluation range extending three bandwidths beyond the data. Empty samples retain their identity without a curve. Singleton and constant samples require an explicit bandwidth; no spread is inferred for them. Missing groups are excluded, and infinite/nonnumeric observations rejected.

Usage

draw_density(
  x,
  group = NULL,
  n = 512,
  bw = "nrd0",
  na_rm = TRUE,
  palette = NULL,
  xlab = NULL,
  ylab = NULL,
  title = NULL,
  theme = NULL,
  margins = DEFAULT_MARGINS,
  width = NULL,
  height = NULL,
  verbosity = 1L,
  element_id = NULL,
  filename = NULL,
  legend_position = "top",
  legend_placement = "outside",
  bandwidth = NULL,
  kernel = "gaussian",
  adjust = 1,
  fill_alpha = 0.25,
  mode = "overlap",
  order = "input"
)

Arguments

x

Numeric or list: Values used for density estimation. An ungrouped list creates one density trace per element; with group, each list element is split by group into separate traces.

group

Optional Atomic vector or single-column data frame: Grouping variable for multiple density traces.

n

Integer [2, Inf): Number of density evaluation points.

bw

Character or Numeric: Bandwidth selector ("nrd0", "nrd", "ucv", "bcv", "SJ", "SJ-ste", "SJ-dpi"). Numeric input is normalized to bandwidth by the setup function.

na_rm

Logical: Whether to remove NA values before computing densities.

palette

Optional Character: Series colors, overriding the theme palette for this chart. NULL uses the theme's.

xlab

Optional Character: X-axis title.

ylab

Optional Character: Y-axis title.

title

Optional Character: Chart title.

theme

Optional Theme: Theme override.

margins

Optional Named numeric vector or named list: Plot margins in pixels (or percentage strings) for any of "top", "right", "bottom", "left". Unspecified sides keep echarts' default auto-sizing. See draw_line() for details.

width

Optional Character or Numeric: Widget width.

height

Optional Character or Numeric: Widget height.

verbosity

Integer [0, Inf): Verbosity level for removed-NA messages.

element_id

Optional Character: Explicit DOM element ID for the widget container. NULL lets htmlwidgets generate one.

filename

Optional Character: If provided, save the widget to this file via save_drawing().

legend_position

Character {"top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"}: Legend anchor. Top/bottom anchors use horizontal rows; left/right anchors use a vertical column. Corner anchors align within the top or bottom row.

legend_placement

Character {"outside", "inside"}: Relation to the plotting area. Outside placement reserves space for the complete legend; inside placement overlays the data. Neither setting adds a missing legend.

bandwidth

Optional Numeric (0, Inf): Explicit finite smoothing bandwidth in measurement units, overriding the selector.

kernel

Character: One of "gaussian", "epanechnikov", "rectangular", "triangular", "biweight", "cosine", or "optcosine".

adjust

Numeric (0, Inf): Finite bandwidth multiplier.

fill_alpha

Numeric [0, 1]: Distribution fill opacity.

mode

Character {"overlap", "ridge"}: Overlay samples or align rows on common x and y scales. Ridge rows use sample names in place of a legend. Increase figure height for many rows.

order

Character {"input", "mean", "median"}: Sample order. Summary orders are decreasing, retain ties in input order, and place empty samples last.

Value

htmlwidget: Widget object.

Examples

draw_density(iris[["Sepal.Length"]], xlab = "Sepal length")