Skip to contents

Creates area charts of time series with Insper's visual identity. Supports single, stacked, and overlapping 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.

x

<data-masked> Time variable (numeric, Date, or POSIXct).

y

<data-masked> Value variable.

fill

Fill aesthetic. Accepts one of the options below.

  • A quoted color string, used as a static color (e.g., "#3ACC9F", "grey40").

  • A bare discrete column, used for grouping (e.g., category).

  • A bare continuous column, mapped to a gradient (e.g., intensity).

  • NULL (default), which uses Insper turquesa.

The fill color also sets the line color when add_line = TRUE.

palette

Character. Palette for mapped variables. If NULL (default), uses "main".

stacked

Logical or NULL. Whether to stack areas. If NULL (default), stacks only when fill maps a variable. See Details.

area_alpha

Numeric. Area opacity, from 0 to 1. Default is 0.9.

fill_color

Character. Area color, used only when fill is NULL. Default is turquesa_3. Prefer fill = "<color>", which colors both the area and the line.

add_line

Logical. If TRUE (default), draws a line on top of each area.

line_color

Character. Line color, used only when fill is NULL. Default is turquesa_2, a lighter step of the turquesa ramp.

line_width

Numeric. Line width. Default is 0.8.

line_alpha

Numeric. Line opacity, from 0 to 1. Default is 1.

zero

Logical. If TRUE, draws a horizontal line at y = 0. Default is FALSE.

...

Additional arguments passed to ggplot2::geom_area().

Value

A ggplot object.

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)

# Single area: coal consumption since 1900
coal_data <- subset(fossil_fuel, fuel == "Coal" & year >= 1900)
insper_area(coal_data, x = year, y = consumption)

# Mapping fill to a variable stacks the areas
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"
  )

# Overlapping areas, to compare trajectories
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"
  )
}