Basic OHLC Chart
library(plotly)library(quantmod)getSymbols("AAPL",src='yahoo')## [1] "AAPL"
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")figOHLC Chart without Rangeslider
library(plotly)library(quantmod)getSymbols("AAPL",src='yahoo')## [1] "AAPL"
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)))figCustomise the Figure with Shapes and Annotations
library(plotly)library(quantmod)getSymbols("AAPL",src='yahoo')## [1] "AAPL"
df<-data.frame(Date=index(AAPL),coredata(AAPL))# annotationa<-list(text="Stock Split",x='2014-06-06',y=1.02,xref='x',yref='paper',xanchor='left',showarrow=FALSE)# use shapes to create a linel<-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)figCustom OHLC Chart Colors
library(plotly)library(quantmod)getSymbols("AAPL",src='yahoo')## [1] "AAPL"
# basic example of ohlc chartsdf<-data.frame(Date=index(AAPL),coredata(AAPL))df<-tail(df,30)# cutom colorsi<-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)figReference
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)