block by timelyportfolio 863e8ea3e88c2d18a4caea71ed290489

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.

code.R