Skip to content

Instantly share code, notes, and snippets.

@perthr
Forked from timelyportfolio/Readme.md
Created March 30, 2020 06:10
Show Gist options
  • Save perthr/0451f12bd2de9231d77dd2037ee33c2a to your computer and use it in GitHub Desktop.
Save perthr/0451f12bd2de9231d77dd2037ee33c2a to your computer and use it in GitHub Desktop.
3d yield curve with Plotly in R

Nowhere near as spectacular as the Upshot/New York Times 3d yield curve by Amanda Cox and Gregor Aisch, but not bad at all for a couple of lines of R code with the plotly htmlwidget.

library(plotly)
library(dplyr)
library(tidyr)
library(purrr)
library(quantmod)
library(magrittr)


# get yields from St. Louis Fed FRED
yield_curve <- list("DTB3", "DGS2", "DGS5", "DGS10", "DGS30") %>%
  map(
    ~getSymbols(.x, auto.assign=FALSE, src="FRED")
  ) %>%
  do.call(merge,.)

# create our 3d surface yield curve
yield_curve["1980::"] %>%
  # convert to numeric matrix
  data.matrix() %>% 
  # transpose
  t() %>%
  # draw our Plotly 3d surface
  plot_ly(
    x=as.Date(index(yield_curve["1980::"])),
    y=c(0.25,2,5,10,30),
    z=.,
    type="surface"
  ) %>%
  plotly::layout(
    scene=list(
      xaxis=list(title="date"),
      yaxis=list(title="term"),
      zaxis=list(title="yield")
    )
  )

  
# 3d scatter chart
yield_curve_tidy <- yield_curve %>%
  data.frame() %>%
  add_rownames(var="date") %>%
  gather(symbol,yield,-date) %>%
  mutate(term=c(0.25,5,10,30)[match(symbol,paste0(c("IRX","FVX","TNX","TYX"),".Close"))])

yield_curve_tidy %>%
  plot_ly(
    x=date, y=yield, z=term,
    group=symbol, type="scatter3d"
  )
library(plotly)
library(dplyr)
library(tidyr)
library(purrr)
library(quantmod)
library(magrittr)
# get yields from St. Louis Fed FRED
yield_curve <- list("DTB3", "DGS2", "DGS5", "DGS10", "DGS30") %>%
map(
~getSymbols(.x, auto.assign=FALSE, src="FRED")
) %>%
do.call(merge,.)
# create our 3d surface yield curve
yield_curve["1980::"] %>%
# convert to numeric matrix
data.matrix() %>%
# transpose
t() %>%
# draw our Plotly 3d surface
plot_ly(
x=as.Date(index(yield_curve["1980::"])),
y=c(0.25,2,5,10,30),
z=.,
type="surface"
) %>%
plotly::layout(
scene=list(
xaxis=list(title="date"),
yaxis=list(title="term"),
zaxis=list(title="yield")
)
)
# 3d scatter chart
yield_curve_tidy <- yield_curve %>%
data.frame() %>%
add_rownames(var="date") %>%
gather(symbol,yield,-date) %>%
mutate(term=c(0.25,5,10,30)[match(symbol,paste0(c("IRX","FVX","TNX","TYX"),".Close"))])
yield_curve_tidy %>%
plot_ly(
x=date, y=yield, z=term,
group=symbol, type="scatter3d"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment