Chapter 5 Financial Charts
5.1 Time Series and Date Axess
5.1.1 Time Series using Axes of type date
library(plotly)
<- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
stock
<- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
fig add_trace(x = ~Date, y = ~AAPL.High)%>%
layout(showlegend = F)
<- fig %>%
fig layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6', width = 900)
fig
5.1.2 Configuring Tick Labels
library(tidyquant)
library(plotly)
= c("GOOG", "AAPL", "AMZN", "META", "NFLX", "MSFT")
tickers for (i in tickers){
getSymbols(i,
from = "2020-01-01",
to = "2021-12-31")}
<- data.frame(GOOG$GOOG.Adjusted,
stock $AAPL.Adjusted,
AAPL$AMZN.Adjusted,
AMZN$META.Adjusted,
META$NFLX.Adjusted,
NFLX$MSFT.Adjusted)
MSFT$GOOG.Adjusted <- stock$GOOG.Adjusted/stock$GOOG.Adjusted[1]
stock$AAPL.Adjusted <- stock$AAPL.Adjusted/stock$AAPL.Adjusted[1]
stock$AMZN.Adjusted <- stock$AMZN.Adjusted/stock$AMZN.Adjusted[1]
stock$META.Adjusted <- stock$META.Adjusted/stock$META.Adjusted[1]
stock$NFLX.Adjusted <- stock$NFLX.Adjusted/stock$NFLX.Adjusted[1]
stock$MSFT.Adjusted <- stock$MSFT.Adjusted/stock$MSFT.Adjusted[1]
stock<- data.frame(stock,rownames(stock))
stock colnames(stock) <- append(tickers,'Dates')
<- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
fig add_trace(x = ~Dates, y = ~GOOG, name = 'GOOG')%>%
add_trace(x = ~Dates, y = ~AAPL, name = 'AAPL')%>%
add_trace(x = ~Dates, y = ~AMZN, name = 'AMZN')%>%
add_trace(x = ~Dates, y = ~META, name = 'META')%>%
add_trace(x = ~Dates, y = ~NFLX, name = 'NFLX')%>%
add_trace(x = ~Dates, y = ~MSFT, name = 'MSFT')%>%
layout(title = 'custom tick labels',legend=list(title=list(text='variable')),
xaxis = list(dtick = "M1", tickformat="%b<br>%Y"), width = 1000)
options(warn = -1)
<- fig %>%
fig layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6')
fig
5.2 Candlestick Charts
5.2.1 Basic Candlestick
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
# basic example of ohlc charts
<- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
df
<- df %>% plot_ly(x = ~Date, type="candlestick",
fig open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
<- fig %>% layout(title = "Basic Candlestick Chart")
fig
fig
## [1] "AAPL"
5.2.2 Customise the fig ure with Shapes and Annotations
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
<- data.frame(Date=index(AAPL),coredata(AAPL))
df
# annotation
<- list(text = "Stock Split",
a x = '2014-06-06',
y = 1.02,
xref = 'x',
yref = 'paper',
xanchor = 'left',
showarrow = FALSE
)
# use shapes to create a line
<- list(type = line,
l x0 = '2014-06-06',
x1 = '2014-06-06',
y0 = 0,
y1 = 1,
xref = 'x',
yref = 'paper',
line = list(color = 'black',
width = 0.5)
)
<- df %>% plot_ly(x = ~Date, type="candlestick",
fig open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
<- fig %>% layout(title = "Apple Stock",
fig annotations = a,
shapes = l)
fig
## [1] "AAPL"
5.3 OHLC Charts
5.3.1 Basic OHLC Chart
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
<- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
df
<- df %>% plot_ly(x = ~Date, type="ohlc",
fig open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
<- fig %>% layout(title = "Basic OHLC Chart",
fig xaxis = list(rangeslider = list(visible = F)))
fig
## [1] "AAPL"
5.3.2 Customise the Figure with Shapes and Annotations
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
<- data.frame(Date=index(AAPL),coredata(AAPL))
df
# annotation
<- list(text = "Stock Split",
a x = '2014-06-06',
y = 1.02,
xref = 'x',
yref = 'paper',
xanchor = 'left',
showarrow = FALSE
)
# use shapes to create a line
<- list(type = line,
l x0 = '2014-06-06',
x1 = '2014-06-06',
y0 = 0,
y1 = 1,
xref = 'x',
yref = 'paper',
line = list(color = 'black',
width = 0.5)
)
<- df %>% plot_ly(x = ~Date, type="ohlc",
fig open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
<- fig %>% layout(title = "Custom Colors",
fig annotations = a,
shapes = l)
fig
## [1] "AAPL"
5.4 Waterfall Charts
5.4.1 Basic Waterfall Chart
library(plotly)
= list("Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax")
x= c("relative", "relative", "total", "relative", "relative", "total")
measure= c("+60", "+80", "", "-40", "-20", "Total")
text= c(60, 80, 0, -40, -20, 0)
y= data.frame(x=factor(x,levels=x),measure,text,y)
data
<- plot_ly(
fig name = "20", type = "waterfall", measure = ~measure,
data, x = ~x, textposition = "outside", y= ~y, text =~text,
connector = list(line = list(color= "rgb(63, 63, 63)")))
<- fig %>%
fig layout(title = "Profit and loss statement 2018",
xaxis = list(title = ""),
yaxis = list(title = ""),
autosize = TRUE,
showlegend = TRUE)
fig
5.5 Funnel Charts
# Need to install plotly from Github to get funnel plots
::install_github("ropensci/plotly") devtools
5.5.1 Basic Funnel Plot
library(plotly)
<- plot_ly()
fig <- fig %>%
fig add_trace(
type = "funnel",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"),
x = c(39, 27.4, 20.6, 11, 2))
<- fig %>%
fig layout(yaxis = list(categoryarray = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent")))
fig
5.5.2 Stacked Funnel Plot
library(plotly)
<- plot_ly(
fig type = "funnel",
name = 'Montreal',
y = c("Website visit", "Downloads", "Potential customers", "Requested price"),
x = c(120, 60, 30, 20),
textinfo = "value+percent initial")
<- fig %>%
fig add_trace(
type = "funnel",
name = 'Toronto',
orientation = "h",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"),
x = c(100, 60, 40, 30, 20),
textposition = "inside",
textinfo = "value+percent previous")
<- fig %>%
fig add_trace(
type = "funnel",
name = 'Vancouver',
orientation = "h",
y = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent", "Finalized"),
x = c(90, 70, 50, 30, 10, 5),
textposition = "outside",
textinfo = "value+percent total")
<- fig %>%
fig layout(yaxis = list(categoryarray = c("Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent", "Finalized")))
fig