Skip to contents

Create density plots to visualize distributions using Insper's visual identity. Supports grouped densities with automatic color assignment.

Usage

insper_density(
  data,
  x,
  fill = NULL,
  palette = NULL,
  fill_color = get_insper_colors("teals1"),
  line_color = get_insper_colors("teals3"),
  alpha = 0.6,
  bw = NULL,
  adjust = 1,
  kernel = "gaussian",
  ...
)

Arguments

data

A data frame containing the data to plot

x

Variable for x-axis (numeric)

fill

Fill aesthetic. Can be:

  • A quoted color name/hex (e.g., `"purple"`, `"#9B59B6"`) for static color

  • A bare column name (e.g., `factor(cyl)`) for discrete grouping

  • A continuous variable (e.g., `gear`) for gradient coloring (rare for density)

If `NULL` (default), uses Insper teal. When a variable is mapped, it applies to both density fill and line color.

palette

Character. Color palette name for variable mappings. Options: "categorical", "main", "bright", "reds", "teals", etc. If NULL (default), uses "categorical". Only applies to variable mappings.

fill_color

Character. Hex color for density area when not using fill aesthetic. Default is Insper teal. (Deprecated: use `fill = "color"` instead)

line_color

Character. Color for density line. Default is darker teal. (Deprecated: use in combination with `fill = "color"`)

alpha

Numeric. Transparency of density area (0-1). Default is 0.6

bw

Numeric or character. Bandwidth for density estimation. Can be a numeric value or a bandwidth selector name ("nrd0", "nrd", "ucv", "bcv", "SJ"). Default is NULL which uses ggplot2's default ("nrd0").

adjust

Numeric. Adjustment multiplier for bandwidth. Default is 1.

kernel

Character. Kernel for density estimation. Default is "gaussian".

...

Additional arguments passed to ggplot2::geom_density()

Value

A ggplot2 object

Examples

if (FALSE) { # has_insper_fonts()
# Simple density plot (default teal)
insper_density(macro_series, x = ipca)

# Static color
insper_density(macro_series, x = ipca, fill = "purple")

# Grouped density plot (discrete variable)
insper_density(iris, x = Sepal.Length, fill = Species)
}