Create area charts for time series data using Insper's visual identity. Supports both single and grouped/stacked areas.
Usage
insper_area(
data,
x,
y,
fill = NULL,
palette = NULL,
stacked = NULL,
area_alpha = 0.9,
fill_color = get_insper_colors("teals1"),
add_line = TRUE,
line_color = get_insper_colors("teals3"),
line_width = 0.8,
line_alpha = 1,
zero = FALSE,
...
)Arguments
- data
A data frame containing the data to plot
- x
Time variable (numeric, Date, or POSIXct)
- y
Value variable
- fill
Fill aesthetic. Can be:
A quoted color name/hex (e.g., `"teal"`, `"#00BFFF"`) for static color
A bare column name (e.g., `category`) for discrete grouping
A continuous variable (e.g., `intensity`) for gradient coloring
If `NULL` (default), uses Insper teal. When a variable is mapped, it applies to both area fill and line color (if `add_line = TRUE`).
- 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.
- stacked
Logical. If TRUE and fill is provided, creates stacked areas. If NULL (default), automatically detects: stacks when `fill` is a variable mapping, otherwise uses overlapping areas. Set explicitly to FALSE to force overlapping areas even with fill mappings
- area_alpha
Numeric. Transparency of areas (0-1). Default is 0.9
- fill_color
Character. Hex color code for area when not using fill aesthetic. Default is Insper teal. (Deprecated: use `fill = "color"` instead)
- add_line
Logical. If TRUE, adds line on top of area. Default is TRUE
- line_color
Character. Hex color code for line when not using fill aesthetic. Default is darker Insper teal. (Deprecated: use in combination with `fill = "color"`)
- line_width
Numeric. Width of line. Default is 0.8
- line_alpha
Numeric. Transparency of line (0-1). Default is 1
- zero
Logical. If TRUE, adds a horizontal line at y = 0. Default is FALSE
- ...
Additional arguments passed to
ggplot2::geom_area()
Details
## Smart Stacking Behavior
By default (`stacked = NULL`), the function automatically detects whether to stack areas based on context:
When `fill` is a variable (e.g., `fill = category`): automatically creates stacked areas to show part-to-whole relationships
When `fill` is missing or a static color: uses overlapping areas (stacking has no effect)
You can override this behavior by explicitly setting `stacked = TRUE` (force stacking) or `stacked = FALSE` (force overlapping, useful for comparing distributions of different groups).
## Line Overlay
By default, a line is drawn on top of each area (`add_line = TRUE`). This helps emphasize trends and makes the areas more visually distinct. The line color matches the fill color. Set `add_line = FALSE` for cleaner appearance when comparing many groups.
Examples
if (FALSE) { # has_insper_fonts()
library(ggplot2)
# Simple area plot - Coal consumption since 1900
coal_data <- subset(fossil_fuel, fuel == "Coal" & year >= 1900)
insper_area(coal_data, x = year, y = consumption)
# Stacked area chart showing all fuels (automatically stacked when fill is provided)
recent_data <- subset(fossil_fuel, year >= 1950)
recent_data$fuel <- factor(recent_data$fuel, levels = c("Oil", "Gas", "Coal"))
insper_area(recent_data, x = year, y = consumption, fill = fuel) +
labs(
title = "Global Fossil Fuel Consumption",
subtitle = "Primary energy consumption by fuel type (1950-present)",
x = "Year",
y = "Consumption (TWh)",
fill = "Fuel Type"
)
# Force overlapping areas (comparing distributions)
insper_area(recent_data, x = year, y = consumption,
fill = fuel, stacked = FALSE) +
labs(
title = "Comparing Fuel Consumption Trends",
subtitle = "Overlapping areas show individual trajectories",
x = "Year",
y = "Consumption (TWh)",
fill = "Fuel Type"
)
}
