OHLC Charts in R
How to create OHLC charts in R.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Basic OHLC Chart
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
Click to copy
## [1] "AAPL"
Click to copy
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
fig <- df %>% plot_ly(x = ~Date, type="ohlc",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
fig <- fig %>% layout(title = "Basic OHLC Chart")
fig
Click to copy
OHLC Chart without Rangeslider
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
Click to copy
## [1] "AAPL"
Click to copy
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
fig <- df %>% plot_ly(x = ~Date, type="ohlc",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
fig <- fig %>% layout(title = "Basic OHLC Chart",
xaxis = list(rangeslider = list(visible = F)))
fig
Click to copy
Customise the Figure with Shapes and Annotations
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
Click to copy
## [1] "AAPL"
Click to copy
df <- data.frame(Date=index(AAPL),coredata(AAPL))
# annotation
a <- list(text = "Stock Split",
x = '2014-06-06',
y = 1.02,
xref = 'x',
yref = 'paper',
xanchor = 'left',
showarrow = FALSE
)
# use shapes to create a line
l <- list(type = line,
x0 = '2014-06-06',
x1 = '2014-06-06',
y0 = 0,
y1 = 1,
xref = 'x',
yref = 'paper',
line = list(color = 'black',
width = 0.5)
)
fig <- df %>% plot_ly(x = ~Date, type="ohlc",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low)
fig <- fig %>% layout(title = "Custom Colors",
annotations = a,
shapes = l)
fig
Click to copy
Custom OHLC Chart Colors
library(plotly)
library(quantmod)
getSymbols("AAPL",src='yahoo')
Click to copy
## [1] "AAPL"
Click to copy
# basic example of ohlc charts
df <- data.frame(Date=index(AAPL),coredata(AAPL))
df <- tail(df, 30)
# cutom colors
i <- list(line = list(color = '#FFD700'))
d <- list(line = list(color = '#0000ff'))
fig <- df %>% plot_ly(x = ~Date, type="ohlc",
open = ~AAPL.Open, close = ~AAPL.Close,
high = ~AAPL.High, low = ~AAPL.Low,
increasing = i, decreasing = d)
fig
Click to copy
Reference
See https://plotly.com/r/reference/#ohlc for more information and chart attribute options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)
Click to copy