Visualization of yield curves is fairly relevant and the package fixedincome brings the usual plot function to create plots with the SpotRateCurve objects and also a ggplot2 set of methods that helps with the visualization of the term structures using all ggplot2 features.

Before the start let’s get the necessary packages loaded.

As the second step, the yield curve must be created and this is done with data from {rb3} package.

library(rb3)
refdate <- as.Date("2022-08-09")
fetch_marketdata("b3-reference-rates", refdate = refdate, curve_name = "PRE")
fetch_marketdata("b3-futures-settlement-prices", refdate = refdate)
yc_ss <- yc_brl_with_futures_get(refdate) |>
  filter(!is.na(biz_days))
yc <- bind_rows(
  yc_ss |> slice(1),
  yc_ss |> filter(!is.na(symbol))
) |>
  filter(!duplicated(biz_days))

base::plot

plot(curve)

plot(curve, show_forward = TRUE)

curve |>
  fixedincome::first("3 years") |>
  plot(curve, show_forward = TRUE)

curve_2y <- curve |> fixedincome::first("2 years")
interpolation(curve_2y) <- interp_naturalspline()
plot(curve_2y, use_interpolation = TRUE)

plot(curve_2y, use_interpolation = TRUE, show_forward = TRUE)

plot(curve_2y, use_interpolation = TRUE, show_forward = TRUE, legend_location = "bottomleft")

ggplot2

ggspotratecurveplot(curve,
  title = "DI1 spot rates", subtitle = format(refdate), caption = "Data from {rb3} package"
)
#> Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
#> ℹ Please use tidy evaluation idioms with `aes()`.
#> ℹ See also `vignette("ggplot2-in-packages")` for more information.
#> ℹ The deprecated feature was likely used in the fixedincome package.
#>   Please report the issue at
#>   <https://github.com/wilsonfreitas/R-fixedincome/issues>.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> ℹ The deprecated feature was likely used in the fixedincome package.
#>   Please report the issue at
#>   <https://github.com/wilsonfreitas/R-fixedincome/issues>.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> ℹ The deprecated feature was likely used in the fixedincome package.
#>   Please report the issue at
#>   <https://github.com/wilsonfreitas/R-fixedincome/issues>.
#> This warning is displayed once per session.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

ggspotratecurveplot(curve,
  title = "DI1 spot rates", subtitle = format(refdate), caption = "Data from {rb3} package",
  curve.x.axis = "terms"
)

ggspotratecurveplot(curve,
  title = "DI1 spot rates", subtitle = format(refdate), caption = "Data from {rb3} package"
) +
  autolayer(forwardrate(curve), size = 1)

ggspotratecurveplot(curve_2y,
  title = "DI1 spot rates", subtitle = format(refdate), caption = "Data from {rb3} package",
  curve.interpolation = TRUE
)

autoplot(curve_2y, curve.geom = "point") +
  autolayer(curve_2y, curve.geom = "line", curve.name = "Interpolation", curve.interpolation = TRUE)

terms_ <- do.call(seq, as.list(range(curve_2y@terms)))
curve_2y_interp <- curve_2y[[terms_]]
autoplot(curve_2y, curve.geom = "point") +
  autolayer(curve_2y_interp, curve.geom = "line") +
  autolayer(forwardrate(curve_2y_interp),
    curve.geom = "line", curve.name = "Forward Rate"
  )

Plotting Multiple Curves

refdate2 <- as.Date("2022-03-09")
fetch_marketdata("b3-reference-rates", refdate = refdate2, curve_name = "PRE")
fetch_marketdata("b3-futures-settlement-prices", refdate = refdate2)
yc_ss <- yc_brl_with_futures_get(refdate2) |>
  filter(!is.na(biz_days))
yc <- bind_rows(
  yc_ss |> slice(1),
  yc_ss |> filter(!is.na(symbol))
) |>
  filter(!duplicated(biz_days))
ggspotratecurveplot(curve,
  title = "DI1 spot rates", caption = "Data from {rb3} package",
  curve.x.axis = "terms"
) +
  autolayer(curve2, curve.x.axis = "terms", size = 1) +
  autolayer(curve2, curve.geom = "point", curve.x.axis = "terms", size = 2)

curve2_2y <- curve2 |> fixedincome::first("2 years")
ggspotratecurveplot(curve_2y,
  title = "DI1 spot rates", caption = "Data from {rb3} package",
  curve.x.axis = "terms"
) +
  autolayer(curve2_2y, curve.x.axis = "terms", size = 1) +
  autolayer(curve2_2y, curve.geom = "point", curve.x.axis = "terms", size = 2)