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("turquesa_3"),
add_line = TRUE,
line_color = get_insper_colors("turquesa_2"),
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., `"#3ACC9F"`, `"grey40"`) 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 turquesa. 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: "main", "muted", "turquesa", "vermelho", etc. If NULL (default), uses "main". 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. Defaults to `turquesa_3`. (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. Defaults to `turquesa_2`, a lighter step of the turquesa ramp than the area fill. (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
## Stacking
Under the default `stacked = NULL`, areas stack when `fill` maps a variable and overlap when `fill` is missing or a static color, where stacking would have no effect anyway. Set `stacked = TRUE` or `stacked = FALSE` to choose directly. Overlapping areas suit comparing group trajectories; stacked areas suit part-to-whole.
## Line Overlay
A line is drawn on top of each area by default (`add_line = TRUE`), in the fill color. Set `add_line = FALSE` to drop it, which helps when many groups are stacked.
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"
)
}
