library(rtemis.draw)5 Density plots
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
)draw_density(
list(
`Flipper Length` = penguins$flipper_len,
`Bill Length` = penguins$bill_len
)
)5.2 Grouped Single or Multiple Numeric Vectors
draw_density(
x = penguins$body_mass,
group = penguins$species
)draw_density(
list(`Body Mass` = penguins$body_mass),
group = penguins$species
)draw_density(
list(
`Bill Length` = penguins$bill_len,
`Bill Depth` = penguins$bill_dep
),
group = penguins$sex
)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)"
)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)")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)