R Dygraphs: How To Visualize Time Series Data In R And R Shiny (2024)

[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (1)

When it comes to finding an R package capable of making interactive visualizations out of the box while also working flawlessly with R Shiny, you don’t have that many options. Sure, there’s Highcarts, but what if you’re looking for something more specialized for time series?

Well, that’s where R Dygraphs chime in! This package serves as an R interface to a popular JavaScript library, and it works like a charm in R scripts and R Shiny. We’ll explore both options today.

After reading, you’ll know how to:

  • Plot R time series objects with R Dygraphs
  • Style R Dygraphs charts, add title and axis labels, show multiple series, and customize aesthetics
  • Tweak the chart interactivity
  • Use R Dygraphs in R Shiny

What are race charts and how do you make one in R? Read our guide on visualizing stock data over time with gganimate.

Table of contents:

R Dygraphs – How To Get Started

The Dygraphs package is available on CRAN, which means you can get it by running your typical `install.packages()` command. Make sure to run this first before proceeding with the article:

install.packages("dygraphs")

And now to start using it, you’ll only need one line of code. Well, after the package import, of course. The following code snippet will create an interactive time series chart of the famous Airline passengers dataset:

library(dygraphs)dygraph(datasets::AirPassengers)

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (2)

Can you believe it only took one line of code? R Dygraphs does so much for you behind the scenes. As it turns out, tweaking the chart also requires much less code than the alternative charting packages. Let’s dive into that next.

Deep Dive Into R Dygraphs – Visualize Stock Data

In this section, you’ll learn the essentials behind R Dygraphs. We’ll cover extensive chart creation and styling, multi-series plots, and interactivity – all on stock data pulled straight from the web.

How to Get Stock Data in R

The first order of business is data gathering. Since we’re working with stock data, the `quantmod` package is your friend (install if necessary). It allows you to pull stock data from Yahoo Finance for a given ticker and time frame.

The returned stock dataframe will contain the time period (daily level) as an index column, and several other attributes showing the trade volume, open, close, and adjusted price for the day. We only care about the volume and the adjusted price:

library(quantmod)get_stock_data <- function(ticker, start_date, end_date) { data <- getSymbols(ticker, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE) # Removes the ticker name from column names colnames(data) <- gsub(paste0(ticker, "\\."), "", colnames(data)) data <- data[, c("Volume", "Adjusted")] return(data)}aapl <- get_stock_data(ticker = "AAPL", start_date = "2023-01-01", end_date = "2024-01-01")tail(aapl, 10)

The above code snippet declares a function for fetching stock data and prints the last 10 values for the AAPL ticker (Apple):

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (3)

Now in R Dygraphs, you only need to call the `dygraph()` function on the column you want to visualize – Adjusted price in our case:

dygraph(aapl$Adjusted)

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (4)

Hovering over the points on the line will show you the corresponding X and Y axis values:

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (5)

is few and far between. It’s great to see Dygraphs doesn’t belong to this group, and does everything with so few lines of code.

Dygraph Customization and Chart Options

Let’s take a look at some basic tweaking options first. The `dygraph()` function can take a couple of optional parameters. For example, `main`, `xlab`, and `ylab` control the labels for the title, X-axis, and Y-axis.

The `width` and `height` parameters will set the chart to have a fixed size, rather than its content resizing with the window:

dygraph( aapl$Adjusted, main = "Apple Stock Price (AAPL)", xlab = "Time period", ylab = "Adjusted price (USD)", width = 1200, height = 800)

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (6)

We’ll keep only `main`, `xlab`, and `ylab` moving forward.

The cool thing about R Dygraphs is that it allows you to use the pipe operator (`%>%`) to chain function calls. For example, you can add an additional `dySeries()` function to control how the value axis looks and behaves.

We’ll change the line color and add square points to each data point:

dygraph(aapl$Adjusted, main = "Apple Stock Price (AAPL)", xlab = "Time period", ylab = "Adjusted price (USD)") %>% dySeries(color = "#0198f9", drawPoints = TRUE, pointSize = 3, pointShape = "square")

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (7)

Now we’re getting somewhere!

Dygraphs also make it possible to plot multiple series on a single chart. To demonstrate, we’ll plot the scaled version of the `Volume` attribute as a filled and stepped area chart. The scaling down process is more or less mandatory since their values are on different orders of magnitude.

While here, let’s also change the labels shown on the series legend:

aapl$VolumeScaled <- aapl[, "Volume"] / 1000000dygraph(aapl[, c("Adjusted", "VolumeScaled")], main = "Apple Stock Price (AAPL) and Trade Volume") %>% dySeries("Adjusted", label = "Adjusted Price (USD)", color = "#0198f9", drawPoints = TRUE, pointSize = 3, pointShape = "square") %>% dySeries("VolumeScaled", label = "Trade Volume (M)", stepPlot = TRUE, fillGraph = TRUE, color = "#FF9900")

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (8)

This kind of chart might work, but scaling up/down one variable to accommodate the other leaves a lot to be desired.

Luckily, R Dygraphs has the option to display multiple axis ticks. All you need to do is to set `axis = “y2”` on the series you’re working on second:

aapl <- get_stock_data(ticker = "AAPL", start_date = "2023-01-01", end_date = "2024-01-01")dygraph(aapl) %>% dySeries("Adjusted", label = "Adjusted Price (USD)", color = "#0198f9", drawPoints = TRUE, pointSize = 3, pointShape = "square") %>% dySeries("Volume", label = "Trade Volume (M)", stepPlot = TRUE, fillGraph = TRUE, color = "#FF9900", axis = "y2")

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (9)

You’ve now successfully plotted two variables with different orders of magnitude!

And finally, let’s take a look into interactivity. We’ll fetch stock price information for a couple of additional tickers – Microsoft and Amazon – and concatenate everything into a single XTS object:

get_stock_data <- function(ticker, start_date, end_date) { data <- getSymbols(ticker, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE) colnames(data) <- gsub(paste0(ticker, "\\."), "", colnames(data)) data <- data[, "Adjusted"] return(data)}start_date <- "2023-01-01"end_date <- "2024-01-01"stock_data <- get_stock_data("AAPL", start_date, end_date)names(stock_data) <- ("AAPL")stock_data$MSFT <- get_stock_data("MSFT", start_date, end_date)stock_data$AMZN <- get_stock_data("AMZN", start_date, end_date)tail(stock_data, 10)

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (10)

You can now use the `dyHighlight()` function to control the highlighting behavior. For example, the below code snippet will increase the width of the highlighted line, add a circle marker, and decrease the opacity of the lines not currently highlighted:

dygraph(stock_data, main = "Stock Price Comparison") %>% dyHighlight( highlightSeriesOpts = list(strokeWidth = 3), highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.5 )

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (11)

You can also see how highlighting a point on one series also highlights the same point in time on others. R Dygraphs does this synchronization automatically – you don’t have to lift a finger!

And now with the basics of time series visualization under your belt, let’s take a look at implementation in R Shiny!

R Dygraphs In R Shiny – Build An App From Scratch

The idea behind the Shiny application you’re about to build is simple:

  • Give the user the option to select a stock ticker from a predefined list
  • Allow the user to change start/end dates for the stock analysis time window
  • Display historic stock prices, trade volume, and daily return percentage

To use Dygraphs in R Shiny, you’ll want to leverage the built-in `dygraphOutput()` function in the UI, and `renderDygraph()` server.

The `get_stock_data()` function will get the stock data for a given ticker and date range for you, and it will also calculate daily returns.

Moving further, the UI code is self-explanatory. It just contains the required components for input and output elements.

The server function is where things get interesting. First, we need to update date inputs on the fly through the `observe()` function, so that the user can’t select an end date that’s lower than the start date (and vice versa). Chart data is stored in `stock_data` reactive value and is referenced in chart code down below:

library(dplyr)library(shiny)library(dygraphs)# Constantsallowed_tickers <- c("AAPL", "AMZN", "GOOGL", "META", "MSFT", "NVDA", "TSLA")allowed_min_date <- as.Date("2010-01-01")allowed_max_date <- Sys.Date() - 1# Custom functions for getting and organizing dataget_stock_data <- function(ticker, start_date, end_date) { data <- getSymbols(ticker, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE) colnames(data) <- gsub(paste0(ticker, "\\."), "", colnames(data)) data <- data[, c("Volume", "Adjusted")] data <- data.frame(Date = index(data), coredata(data)) # In addition to what we had before, also calculate daily returns data <- data %>% arrange(Date) %>% mutate(DailyReturn = (Adjusted / lag(Adjusted) - 1) * 100) %>% na.omit() return(as.xts(data))}ui <- fluidPage( sidebarLayout( sidebarPanel( tags$h3("Stock price analyzer"), tags$hr(), selectInput(inputId = "inTicker", label = "Stock ticker:", choices = allowed_tickers, selected = allowed_tickers[1]), dateInput(inputId = "inStartDate", label = "Start date:", value = as.Date("2023-01-01"), min = allowed_min_date, max = allowed_max_date), dateInput(inputId = "inEndDate", label = "End date:", value = allowed_max_date, min = allowed_min_date, max = allowed_max_date) ), mainPanel( dygraphOutput("graphPrice"), dygraphOutput("graphVolume"), dygraphOutput("graphReturns") ) ))server <- function(input, output, session) { # Make sure the dates don't get messed up observe({ updateDateInput(session, "inStartDate", max = input$inEndDate - 1) }) observe({ updateDateInput(session, "inEndDate", min = input$inStartDate + 1) }) # Update the data as we go stock_data <- reactive({ get_stock_data(ticker = input$inTicker, start_date = input$inStartDate, end_date = input$inEndDate) }) # Display charts output$graphPrice <- renderDygraph({ dygraph(stock_data()$Adjusted, main = paste0(input$inTicker, " Stock Price (USD)")) %>% dySeries("Adjusted", label = "Adjusted price") }) output$graphVolume <- renderDygraph({ dygraph(stock_data()$Volume, main = paste0(input$inTicker, " Trade Volume")) %>% dySeries("Volume", label = "Trade volume", stepPlot = TRUE, fillGraph = TRUE, color = "#FF9900") }) output$graphReturns <- renderDygraph({ dygraph(stock_data()$DailyReturn, main = paste0(input$inTicker, " Daily Returns (%)")) %>% dySeries("DailyReturn", label = "Daily returns", color = "#cc0000") %>% dyLimit(0) })}shinyApp(ui = ui, server = server)

You’ll see the following Shiny application after running the code:

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (12)

Everything works like a charm! R Dygraphs integrates nicely with R Shiny, and the only thing you have to worry about is the data format going into the visualizations. It has to be an XTS object – a plain dataframe won’t suffice.

Summing Up R Dygraphs

If there’s one thing R doesn’t lack, it has to be options for data visualization. Even when R-native ones aren’t enough, there are plenty of alternatives from different programming languages, such as JavaScript.

R Dygraphs is one such option specializing in time series data visualization. It works exceptionally well with XTS objects and sets the record for the least amount of code the developer has to write. All charts are interactive by default, which isn’t always a given with R, and the package has superb R Shiny support. What more do you need?

What are your thoughts on time series visualization with R Dygraphs? Do you use it daily or do you prefer an alternative package? Make sure to let us know in our Slack community.

Is R the right choice for analyzing huge datasets? Here’s how well it does in a 1 billion row challenge.

The post appeared first on appsilon.com/blog/.

Related

To leave a comment for the author, please follow the link and comment on their blog: Tag: r - Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R Dygraphs: How To Visualize Time Series Data In R And R Shiny (2024)

FAQs

R Dygraphs: How To Visualize Time Series Data In R And R Shiny? ›

To use Dygraphs in R Shiny, you'll want to leverage the built-in `dygraphOutput()` function in the UI, and `renderDygraph()` server. The `get_stock_data()` function will get the stock data for a given ticker and date range for you, and it will also calculate daily returns.

How do you analyze time series data in R? ›

Time Series Analysis in R is used to see how an object behaves over some time. In R Programming Language, it can be easily done by the ts() function with some parameters. Time series takes the data vector and each data is connected with a timestamp value as given by the user.

What is dygraph in R? ›

The dygraphs package allows to represent time series: a chart where the X axis represent time, and the Y axis the evolution of one or several variables. dygraphs allows to make interactive charts: you can zoom on a specific time period, hover a data point to have more info, and more.

How to declare time series data in R? ›

Creating a time series

The ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

How to cluster time series data in R? ›

For time series clustering with R, the first step is to work out an appropriate distance/similarity metric, and then, at the second step, use existing clustering techniques, such as k-means, hierarchical clustering, density-based clustering or subspace clustering, to find clustering structures.

Is R good for time series analysis? ›

R has many useful functions and packages for time series analysis. You'll find pointers to them in the task view for Time Series Analysis.

How to plot multiple time series data in R? ›

Method 1: Using Basic R methods

First, we create a data vector that has data for all the time series that have to be drawn. Then we plot the time series using the first dataset and plot() function. Then add other time series using line() function to the existing plot.

How do you plot a monthly time series in R? ›

Declare the data set to be time series. Use tsp and seq to generate the required x-axis labels. Plot the chart but suppress x-axis. Use the axis command to add the custom x-axis labels.

How to detect seasonality in time series data in R? ›

A maximum at a frequency of 1 indicates seasonality and the function returns TRUE. 'acf' computes the auto-correlation function of the de-trended time series using acf . A minimum acf value at a lag of 0.5 indicates seasonality and the function returns TRUE.

What is the best way to cluster time series data? ›

Then, the data is grouped into clusters based on the measured similarity or distance using machine learning methods. Clustering algorithms commonly employed for time-series data include partitioning, hierarchical, model-based, and density-based clustering algorithms7,8.

How to sort time series data in R? ›

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.

How do you read time series data? ›

Steps to Interpreting a Time Series Plot
  1. Step 1: Determine the minimum and maximum of the plot.
  2. Step 2: Determine if the data is consistent over time or changing from time period to time period.
  3. Step 3: Summarize the information, to include minimum, maximum, trends, and outliers.

How to identify trend and seasonality in time series data in R? ›

To estimate the trend component and seasonal component of a seasonal time series that can be described using an additive model, we can use the “decompose()” function in R. This function estimates the trend, seasonal, and irregular components of a time series that can be described using an additive model.

How to check stationarity of time series data in R? ›

How to determine if a time series is stationery in R
  1. Step 1 - Install required library. library(tseries)
  2. Step 2 - Generate random time series data. data <- rnorm(50) data_ts <- ts(data,,frequency=4) ...
  3. Step 3 - Plot the data. plot(data_ts)
  4. Step 4 - Perform the dickey-fuller test. adf.test(data_ts)
Mar 22, 2023

Top Articles
Petro logra su primera gran victoria legislativa de 2024: el Congreso aprueba su reforma pensional
All Archon mods and ability bonuses in Warframe, explained
12 Rue Gotlib 21St Arrondissem*nt
Gilbert Public Schools Infinite Campus
Bon plan – Le smartphone Motorola Edge 50 Fusion "4 étoiles" à 339,99 €
Ofw Pinoy Channel Su
Savage X Fenty Wiki
Promiseb Discontinued
123Movies The Idol
Craigslist Pets Longview Tx
Missed Connections Dayton Ohio
manhattan cars & trucks - by owner - craigslist
Westelm Order
The Nun 2 Showtimes Tinseltown
Hose Woe Crossword Clue
Who Is Denise Richards' Husband? All About Aaron Phypers
Shooters Lube Discount Code
Erika Henriquez-Quallo
Is Robert Manse Leaving Hsn
Wac 182
Fingerfang Rock Conan
JPMorgan and 6 More Companies That Are Hiring in 2024, Defying the Layoffs Trend
352-730-1982
Oppenheimer Showtimes Near Amc Rivertowne 12
Solid Red Light Litter Robot 4
Guide:How to make WvW Legendary Armor
Milf Lingerie Caption
Danielle Moodie-Mills Net Worth
Jvid Rina Sauce
Cronología De Chelsea Contra Fulham
Tqha Yearling Sale 2023 Results
Bolly2Tolly Sale
Craigslist Cars Los Angeles
Kltv Com Big Red Box
Katie Sigmond - Net Worth 2022, Age, Height, Bio, Family, Career
Ralph Macchio Conservative
How To Use Price Chopper Points At Quiktrip
Vernon Autoplex
How To Create A Top Uber Boss Killer In POE 3.25 League?
The Legend of Maula Jatt | Rotten Tomatoes
Below Her Mouth | Rotten Tomatoes
Recharging Iban Staff
Franchisee Training & Support | Papa Johns Pizza Franchise UK
Pressconnects Obituaries Recent
Saw X Showtimes Near Stone Theatres Sun Valley 14 Cinemas
Leuke tips & bezienswaardigheden voor een dagje Wijk bij Duurstede
Plusword 358
Lifetime Benefits Login
Craigslist Pelham Al
Alvin Isd Ixl
The t33n leak 5-17: Understanding the Impact and Implications - Mole Removal Service
Nfl Spotrac Transactions
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6174

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.