plot_series_comparison: Compare multiple financial/time series indices

View source: R/plot_series_comparison.R

plot_series_comparisonR Documentation

Compare multiple financial/time series indices

Description

Plots multiple time series on the same chart for comparison.

Usage

plot_series_comparison(
  data_list,
  y_vars,
  date_vars,
  language = "eng",
  scale_type = c("none", "index", "percent_change"),
  title = NULL,
  subtitle = NULL,
  y_label = NULL,
  caption = NULL,
  colors = NULL,
  line_types = NULL,
  show_legend = TRUE,
  legend_position = "bottom",
  dual_axis = FALSE
)

Arguments

data_list

Named list of data frames, each returned by a get_* function

y_vars

Vector of column names containing the values to plot from each data frame

date_vars

Vector of column names containing dates from each data frame

language

Language for labels: "pt" (Portuguese) or "eng" (English)

scale_type

Scaling applied to the series:

"none"

Plots raw values as provided

"index"

Indexes all series to 100 at the first observation

"percent_change"

Plots percentage change relative to the first observation

title

Plot title

subtitle

Plot subtitle

y_label

Y-axis label

caption

Plot caption

colors

Vector of colors for each series

line_types

Vector of line types for each series

show_legend

Whether to show the legend (default: TRUE)

legend_position

Position of legend ("bottom", "top", "left", "right", or "none")

dual_axis

Logical. If TRUE, plots exactly 2 series on separate y-axes (a primary axis on the left for the first series, a secondary axis on the right for the second) instead of overlaying them on one scale. Useful for comparing series with very different units, e.g. SELIC (%) vs. the exchange rate (R$/US$). Requires length(data_list) == 2 and is incompatible with scale_type != "none", since the whole point of a dual axis is to preserve each series' native scale.

Value

A ggplot2 object, or NULL (invisibly, with a message) if any series has no rows

Examples


# Example comparing multiple series
selic <- get_selic_rate(2020, 2024)
ipca <- get_ipca_from_target(2020, 2024)

comparison_plot <- plot_series_comparison(
  data_list = list(SELIC = selic, IPCA = ipca),
  y_vars = c("value", "ipca_12m"),
  date_vars = c("date", "date"),
  scale_type = "index",
  title = "Comparison of Brazilian Economic Indicators",
  y_label = "Index (2020-01 = 100)",
  language = "eng"
)
print(comparison_plot)

# Same two series, but on their native scales via a dual axis
dual_plot <- plot_series_comparison(
  data_list = list(SELIC = selic, `Exchange Rate` = get_exchange_rate(2020, 2024)),
  y_vars = c("value", "value"),
  date_vars = c("date", "date"),
  dual_axis = TRUE
)
print(dual_plot)



brfinance documentation built on Nov. 14, 2026, 5:09 p.m.