How to use interactive time series graph using dygraphs in R
Last Updated :
27 Jun, 2022
Dygraphs refer to as Dynamic graphics which leads to an easy way to create interaction between user and graph. The dygraphs are mainly used for time-series analysis. The dygraphs package is an R interface to the dygraphs JavaScript charting library in R Programming Language.
Creating simple dygraphs
Let's take stock data for a better understanding of time series.
Download (Tesla) Stock data from the quantmod library by using getSymbols(). We will plot 5 different dynamic plots.
- Series colour
- Vertical and Horizontal Shading
- Candlestick
- Upper/lower bar
- Range Selector
First of all, install dygraphs and quantmod package in rstudio.
install.packages("dygraphs")
install.packages("quantmod")
You can download data directly from this link: TSLA Stock Data
Here, OHLC represents the open, high, low, and closing price of each period in stock data. So, we are fetching this parameter and storing it in the price variable.
R
# import library
library(dygraphs)
library(quantmod)
# download data
getSymbols("TSLA")
# view top 5 row data
head(TSLA,n=5)
# Get OHLC data
price<-OHLC(TSLA)
head(price, n=5)
Output:
coloursSeries color graph
Dygraph can assign different color palettes to each visualization line. For example, our four-parameter in TSLA data (open, high, low, close) can be represented as different colors. Dygraph provides dyOptions() in which colors parameter can be useful to identify each stock line smoothly.
R
# RColorBrewer used to create nice color palettes
# display.brewer.pal(n, name)
dygraph(price, main = "TSLA Stock price analysis") %>%
dyOptions(colors = RColorBrewer::brewer.pal(4, "Dark2"))
Output:
Visualising 4 parameter of stock priceVertical and Horizontal shading
Vertical shading:
Dygraph can assign shading features on the timeline portion to analyze different time intervals. By using dyshading, we can assign time-interval corresponding to color for better analysis of the stock portion.
R
dygraph(price, main = "TSLA Stock price analysis") %>%
dySeries(label = "Temp (F)",
color = "black") %>%
dyShading(from = "2018-1-1",
to = "2019-12-1",
color = "#FFE6E6") %>%
dyShading(from = "2020-1-1",
to = "2021-1-1",
color = "#CCEBD6")
Output:
Horizontal shading
Dygraphs provide horizontal shading by use of mean and standard deviations. We can assign intervals in dyshading and assign axis parameters to y. Here, ROC function is used to create ROC Curve.
R
ret = ROC(TSLA[, 4])
mn = mean(ret, na.rm = TRUE) #mean
std = sd(ret, na.rm = TRUE) #standard deviation
dygraph(ret, main = "TSLA Share Price") %>%
dySeries("TSLA.Close", label = "TSLA") %>%
dyShading(from = mn - std, to = mn + std, axis = "y")
Output:
Horizontal shade in grey colourCandlestick graph
Candlestick graphs are most commonly used by professional traders in the stock market to predict price movement based on past data. dygraph can easily plot this graph just in one line of command. Here, we are storing the last 30 days stock data to create a candlestick graph. Red bar shows down in stock and the green bar shows up in stock.
R
TSLA <- tail(TSLA, n=30)
graph <- dygraph(OHLC(TSLA))
dyCandlestick(graph)
Output:
Candlestick GraphUpper/lower bar
We can create multiple series in which each series has an upper/lower bar in terms of shade just like the error bar. In the Below graph, the amazon and tesla series example is shown.
R
# import tesla and amazon stock price
getSymbols("AMZN")
stocks <- cbind(AMZN[,2:4], TSLA[,2:4])
# plot graph
dygraph(stocks, main = "Amazon and Tesla Share Prices") %>%
dySeries(c("AMZN.Low", "AMZN.Close",
"AMZN.High"), label = "AMZN") %>%
dySeries(c("TSLA.Low", "TSLA.Close",
"TSLA.High"), label = "TSLA")
Output:
Upper/Lower Bar GraphRange Selector
We can add a range selector at the bottom of the dygraph chart for an easy interface of panning and zooming by dyRangeSelector() function.
R
dygraph(TSLA,
main = "Tesla Stock Price Analysis")
%>% dyRangeSelector()
Output:
Select range from below
Similar Reads
How to Change Line Properties in ggplot2 Halfway in a Time Series Using R
In time series visualizations, it can be useful to distinguish between different segments of data by changing line properties such as color, size, or type. For example, you may want to change the line's appearance at a specific time point to highlight a change in the data. In this article, we'll dem
3 min read
Interactive Data Visualizations in R Using ggiraph
Interactive data visualizations can significantly enhance the ability to explore and understand complex datasets. In R, the ggiraph package allows you to create interactive versions of ggplot2 visualizations. This article will provide an overview of ggiraph, its key features, and step-by-step exampl
5 min read
What is Arithmetic Line-Graph or Time-Series Graph?
A time series is an arrangement in which the values of variables are recorded in relation to the time of occurrence. In the case of a long series of data, time series helps identify the trend, periodicity, etc. The time period can be defined as a year, quarter, month, week, days, hours, and so on. A
6 min read
How to resize a graph in ggplot2 in R?
In this article, we are going to see how to resize the graph in ggplot2 in the R programming language. To resize the graph we like to use option() methods in R. option() method: It returns the height and weight of the specific graph Syntax: option(weight, height) Resizing a graph in ggplot2 Here we
2 min read
How to Plot Multiple Series/Lines in a Time Series Using Plotly in R?
Plotly is a powerful and flexible graphing library that enables the creation of interactive plots in R. It is especially useful for visualizing time series data with multiple lines or series. In this article, we will cover how to plot multiple time series in a single plot using Plotly in R.Multiple
5 min read
How to Plot Timeseries using highcharter library in R
Time series data is a sequence of data points that are measured at regular intervals over time. Plotting time series data is a useful way to visualize trends and patterns over time. Highcharter is an R package that provides an interface to the Highcharts JavaScript charting library. Highcharts is a
9 min read
How to Draw Rainfall Runoff Graph in R Using ggplot?
Rainfall-runoff analysis is an essential concept in hydrology, often used to study the relationship between rainfall and the resulting runoff from a watershed or catchment area. This helps hydrologists and environmental scientists understand water flow and predict flooding events. In this article, w
5 min read
How to plot a graph in R using CSV file ?
To plot a graph in R using a CSV file, we need a CSV file with two-column, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be looking at the way to plot a graph
2 min read
How to Use JupyterLab Inline Interactive Plots
This article shows how to create inline interactive plots in JupyterLab with Python-3 programming language. It assumes basic familiarity with JupyterLab/Jupyter Notebooks and Python-3. By the end of the article, the reader will be able to understand and create inline interactive plots with Matplotli
5 min read
Interactive Graphs in Jupyter Notebook
When working in a Jupyter Notebook environment, you can produce interactive Matplotlib plots that allow you to explore data and interact with the charts dynamically. In this article, we'll explore how to create such interactive plots using Matplotlib within Jupyter. Before we proceed with the steps,
3 min read