plotly Candlestick Chart in R (4 Examples)
Hi! This tutorial will show you how to build a plotly candlestick plot in the R programming language. It is very simple to do, as you will soon see.
First, though, here is a quick overview of this tutorial:
Let’s get into the R code!
Install & Load R plotly & dplyr Libraries
If you do not have plotly and dplyr libraries already installed in your R programming environment, then in either R Studio or in any other preferred R code editor, run the lines of code below to install and load plotly and dplyr; otherwise, you can skip to the next section in this tutorial.
# install plotly and dplyr install.packages(c("plotly", "dplyr")) # load plotly and dplyr library(plotly) library(dplyr)
The R dplyr library is the foremost library for data manipulation and analysis in R programming. It also enables the use of the pipe operator to pipe lines of code together so that they can be run as a chunk.
Load Sample Dataset
In this tutorial, we will make use of data from Yahoo Finance via its API. Thankfully, there is an R package that makes it easy to fetch the data from the Yahoo Finance API. However, you can use any other dataset of your choice to follow along with this tutorial.
In your R IDE, run the code below to install and load the quantmod package.
# install quantmod install.packages("quantmod") # load quantmod library(quantmod)
Having installed and loaded quantmod, we will now fetch Google’s financial data from the Yahoo Finance API and put it into a data frame like so:
getSymbols("GOOG",src = "yahoo") df = data.frame(Date = index(GOOG), coredata(GOOG))
Now, since we want the most recent financial data, we will use the tail() function to get the last 30 rows of the data frame.
df <- tail(df,30)
If you like, you can take a look at the first 10 rows of the data frame to have an idea regarding the data.
print(head(df,10)) # Date GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted #4023 2022-12-22 88.930 89.180 86.940 88.26 23656100 88.26 #4024 2022-12-23 87.620 90.100 87.620 89.81 17815000 89.81 #4025 2022-12-27 89.310 89.500 87.535 87.93 15470900 87.93 #4026 2022-12-28 87.500 88.520 86.370 86.46 17879600 86.46 #4027 2022-12-29 87.030 89.365 86.990 88.95 18280700 88.95 #4028 2022-12-30 87.365 88.830 87.030 88.73 19179300 88.73 #4029 2023-01-03 89.830 91.550 89.020 89.70 20738500 89.70 #4030 2023-01-04 91.010 91.240 87.800 88.71 27046500 88.71 #4031 2023-01-05 88.070 88.210 86.560 86.77 23136100 86.77 #4032 2023-01-06 87.360 88.470 85.570 88.16 26604400 88.16
Let’s now move on with building a simple candlestick plot!
Example 1: Build Simple Candlestick Plot
In this first example, we will build a simple candlestick plot, where we will plot Google’s opening, closing, high, and low data over time. Run the code chunk below to build the visualization.
fig <- df |> plot_ly(x = ~Date, type = "candlestick", open = ~GOOG.Open, close = ~GOOG.Close, high = ~GOOG.High, low = ~GOOG.Low) |> layout(title = "Simple Candlestick Plot") fig
plot_ly() function, we indicated the type of plot we wanted to create, by parsing “candlestick” to the
type = argument. Then, we also parsed Google’s opening, closing, high, and low data to their respective arguments in the function as well.
By default, the candlestick plot includes a range slider at the bottom of the plot, through which the user can interact with the plot, by dragging the slider along.
Example 2: Build Candlestick Plot without Range Slider
In this second example, we will build the same plot as above; but this time, we will remove the range slider widget. Run the code below to create the visualization.
fig <- df |> plot_ly(x = ~Date, type = "candlestick", open = ~GOOG.Open, close = ~GOOG.Close, high = ~GOOG.High, low = ~GOOG.Low) |> layout(title = "Simple Candlestick Plot", xaxis = list(rangeslider = list(visible = FALSE))) fig
As you would notice, the range slider widget has been removed with the help of the
layout() function, which disables the display of the range slider by parsing the boolean FALSE to the
visible = argument.
Example 3: Build Candlestick Plot with Custom Colors
In this third example, we will build a candlestick plot, using custom colors to highlight the high and low data points.
inc <- list(line = list(color = "#2E86C1")) dec <- list(line = list(color = "#F39C12")) fig <- df |> plot_ly(x = ~Date, type = "candlestick", open = ~GOOG.Open, close = ~GOOG.Close, high = ~GOOG.High, low = ~GOOG.Low, increasing = inc, decreasing = dec) |> layout(title = "Simple Candlestick Plot") fig
First of all, we defined the line colors for the high and low data points, and then parsed those to the arguments
increasing = and
decreasing = in the
Example 4: Build Candlestick Plot with Line Tracing
In this fourth and final example, we will build a candlestick plot with line tracing, which connects all the data points on the graph.
fig <- df |> plot_ly(x = ~Date, type = "candlestick", open = ~GOOG.Open, close = ~GOOG.Close, high = ~GOOG.High, low = ~GOOG.Low) |> add_lines(x = ~Date, y = ~GOOG.Open, line = list(color = "black", width = 1.5), inherit = FALSE) |> layout(title = "Candlestick Plot with Line Tracing", showlegend = FALSE) fig
In the code chunk above, we introduced a new function,
add_lines(), wherein we mapped Google’s opening data as the line over the graph and defined the line’s specifications, in terms of color and width. Then, in the
layout() function, we disabled the legend with
showlegend = FALSE, otherwise the traces (boxplot and line) would be shown by default.
Video, Further Resources & Summary
Do you need more explanations on how to make plotly candlestick plots in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to make plotly candlestick plots in R.
The YouTube video will be added soon.
With that, you can now easily create your own plotly candlestick visualizations in the R programming language. Like said at the start of this tutorial, it is easy and simple to do, just as you have seen. Therefore, I hope you found this tutorial helpful, and I will see you soon in the next one!
- Custom Button in plotly Graph in R (Example)
- Create Dropdown Menu in plotly Graph in R (Example)
- Visualize Data Frame in plotly Graph in R (Example)
- Disable Hover Information in plotly Using R (Example)
- Change Size of plotly Graph in R (Example)
- Learn R Programming
This post has shown how to make plotly candlestick plots in R. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter