Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active November 22, 2016 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/863e8ea3e88c2d18a4caea71ed290489 to your computer and use it in GitHub Desktop.
Save timelyportfolio/863e8ea3e88c2d18a4caea71ed290489 to your computer and use it in GitHub Desktop.
Fama/French Factors in 3d with Plotly R htmlwidget

Building on the Plotly 3d yield curve example, let's see how few lines of R code we need to create a Plotly 3d chart of rolling returns on French/Fama US Factors.

# create 3d plot in Plotly
# with Quandl Kenneth French Fama/French factors
# http://www.quandl.com/KFRENCH/FACTORS_D

library(Quandl)
library(quantmod)
library(plotly)
library(pipeR)

f <- Quandl("KFRENCH/FACTORS_M", type="xts")

f %>>%
  # divide by 100
  (./100) %>>%  
  # get the cumulative product
  (cumprod(1+.)) %>>%
  # rolling 36-month ROC
  ROC(36, type="discrete") %>>%
  na.omit() %>>% 
  (
    plot_ly(
      x=colnames(.),
      y=as.Date(index(.)),
      z=data.matrix(.),
      type="surface"
    )
  ) %>%
  plotly::layout(
    title="Fama/French US Factors in Plotly 3d",
    scene=list(
      xaxis=list(title="Fama/French"),
      yaxis=list(title="Date"),
      zaxis=list(title="3 year Rolling")
    )
  )

This small project was incredibly easy thanks to the efforts of many, many generous folks. Thanks to everyone behind the R packages, JavaScript libraries, and the Fama/French data.

# create 3d plot in Plotly
# with Quandl Kenneth French Fama/French factors
# http://www.quandl.com/KFRENCH/FACTORS_D
library(Quandl)
library(quantmod)
library(plotly)
library(pipeR)
f <- Quandl("KFRENCH/FACTORS_M", type="xts")
f %>>%
# divide by 100
(./100) %>>%
# get the cumulative product
(cumprod(1+.)) %>>%
# rolling 36-month ROC
ROC(36, type="discrete") %>>%
na.omit() %>>%
(
plot_ly(
x=colnames(.),
y=as.Date(index(.)),
z=data.matrix(.),
type="surface"
)
) %>%
plotly::layout(
title="Fama/French US Factors in Plotly 3d",
scene=list(
xaxis=list(title="Fama/French"),
yaxis=list(title="Date"),
zaxis=list(title="3 year Rolling")
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment