Skip to main content

5  Density plots

library(rtemis.draw)

draw_density() draws kernel density estimates of one or more numeric variables, optionally by group, as overlaid curves or as ridgeline rows.

5.1 Single or Multiple Numeric Vectors

draw_density(
  x = penguins$body_mass
)
2026-11-18 17:29:12 Removed 2 NA values from x [distribution_samples]
draw_density(
  list(
    `Flipper Length` = penguins$flipper_len,
    `Bill Length` = penguins$bill_len
  )
)
2026-11-18 17:29:12 Removed 2 NA values from Flipper Length [distribution_samples]
2026-11-18 17:29:12 Removed 2 NA values from Bill Length [distribution_samples]

5.2 Grouped Single or Multiple Numeric Vectors

draw_density(
  x = penguins$body_mass,
  group = penguins$species
)
2026-11-18 17:29:12 Removed 2 NA values from x [distribution_samples]
draw_density(
  list(`Body Mass` = penguins$body_mass),
  group = penguins$species
)
2026-11-18 17:29:12 Removed 2 NA values from Body Mass [distribution_samples]
draw_density(
  list(
    `Bill Length` = penguins$bill_len,
    `Bill Depth` = penguins$bill_dep
  ),
  group = penguins$sex
)
2026-11-18 17:29:12 Removed 2 NA values from Bill Length [distribution_samples]
2026-11-18 17:29:12 Removed 2 NA values from Bill Depth [distribution_samples]

5.3 Control smoothing

Use bandwidth to choose a smoothing scale in measurement units, or bw to choose an R bandwidth selector such as "SJ". adjust multiplies the selected bandwidth. The same settings apply to histogram density overlays and saved DensityConfig or HistogramConfig objects.

draw_density(
  penguins$bill_len,
  group = penguins$species,
  bandwidth = 2,
  kernel = "epanechnikov",
  xlab = "Bill length (mm)",
  ylab = "Density (per mm)"
)
2026-11-18 17:29:12 Removed 2 NA values from x [distribution_samples]

The default kernel is Gaussian. Estimated tails may extend beyond the observed range; they are a property of smoothing, not additional observations.

5.4 Ridgelines

Ridgelines use separate rows with a shared numeric scale. Ordering by mean places the group with the largest mean first:

draw_density(penguins$bill_len, group = penguins$species,
             mode = "ridge", order = "mean", xlab = "Bill length (mm)")
2026-11-18 17:29:12 Removed 2 NA values from x [distribution_samples]

5.5 Configuration

setup_DensityConfig() describes a density plot by column names:

config <- setup_DensityConfig(x = "bill_len", group = "species",
                              xlab = "Bill length (mm)")
draw(config, data = penguins)
2026-11-18 17:29:12 Removed 2 NA values from x [distribution_samples]
© 2026 E.D. Gennatas