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

How do you visualize time series data in R? ›

The R programming language provides a strong of tools in the ggplot2 package to visualize data. We can use the geom_line() function to visualize the time-series data using a line plot. Parameter: dataframe: determines the dataframe variable for plotting chart.

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

A line graph is the simplest way to represent time series data. It helps the viewer get a quick sense of how something has changed over time.

What is dygraph in R? ›

dygraphs for R

Automatically plots xts time series objects (or any object convertible to xts). Highly configurable axis and series display (including optional second Y-axis). Rich interactive features including zoom/pan and series/point highlighting. Display upper/lower bars (e.g. prediction intervals) around series.

How to access time series data in R? ›

The most useful way to view raw time series data in R is to use the print() command, which displays the Start , End , and Frequency of your data along with the observations. Another useful command for viewing time series data in R is the length() function, which tells you the total number of observations in your data.

How to make a time series graph on 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 plot a time series using ggplot? ›

To create a time series plot in ggplot2 , you'll use the ggplot() function to specify the data and aesthetics mappings, followed by one or more geom_*() functions to add layers to the plot. Here's a basic syntax outline: R.

Which is a great visualization option for time series charts? ›

Tableau is a popular data visualization tool that supports time series data visualization through its line charts, scatter plots, and Gantt charts. It also offers a variety of other visualization options, including heatmaps, treemaps, and network diagrams.

Which chart is best for time series analysis? ›

Bar Charts

Bar charts represent data as horizontal or vertical bars. The length of each bar is proportional to the value of the variable at that point in time. A bar chart is the right choice for you when you wish to look at how the variable moved over time or when you wish to compare variables versus each other.

Which type of graph is best used to describe time series? ›

Line graph (or chart): the most commonly used graph type for representing time series data.

What is Geom_density () in R? ›

geom_density.Rd. Computes and draws kernel density estimate, which is a smoothed version of the histogram. This is a useful alternative to the histogram for continuous data that comes from an underlying smooth distribution.

How to see variable data in R? ›

To see all the variable names, use names() , and to see all the variable types, use spec() and str() . The spec() function shows you the variable specifications. The str() function gives the same information, plus a listing of the first few values for each variable.

How to display variable type in R? ›

How to find datatype of a variable in R?
  1. Step 1 -Create input vectors. x <- "Hello World" y <- 3 z <- 53L.
  2. Step 3 - Use typeof() function. print(paste("type of x is : ",typeof(x))) "Output is" : character.
  3. Step 4 - Use mode() function. print(paste("mode of x is : ",mode(x))) "Output is" : character. What Users are saying..
Aug 12, 2022

How do you display time series data? ›

There are several ways to display time series data, such as line charts and bar charts. Line charts are the most common type of time series chart and are used to show the trend over time. Bar charts are used to show comparisons between different time periods.

How to get time series data? ›

The time series method of forecasting involves analyzing historical data points collected over time to identify patterns and trends. By applying statistical techniques and models, such as ARIMA, Exponential Smoothing, or Seasonal Decomposition, it predicts future values based on these identified patterns.

How to check for seasonality in time series in R? ›

Check a time series for seasonality
  1. 'pgram' computes a periodogram using fast fourier transformation ( spec. pgram ) and checks at which frequency the periodogram has a maximum. ...
  2. 'acf' computes the auto-correlation function of the de-trended time series using acf . ...
  3. 'lm' fits two linear models to the time series.

How do you visualize timeline data? ›

Gantt charts and Sankey diagrams give aggregated, and often static, views of events and connections over time. Specialist visualizations, like Sankey diagrams and Gantt charts, are more successful at conveying relationships, sequences and dependencies over time in data.

How do you Visualise data in R? ›

When creating a visualization with ggplot, we first use the function ggplot and define the data that the visualization will use, then, we define the aesthetics which define the layout, i.e. the x- and y-axes. In a next step, we add the geom-layer which defines the type of visualization that we want to display.

How do you classify time series data in R? ›

The way for time series classification with R is to extract and build features from time series data first, and then apply existing classification techniques, such as SVM, k-NN, neural networks, regression and decision trees, to the feature set.

Top Articles
What Is Comenity Bank, and Are Its Credit Cards Right for You? - NerdWallet
Comenity Bank Credit Cards | Bankrate
Parke County Chatter
Elleypoint
Www.fresno.courts.ca.gov
Odawa Hypixel
Windcrest Little League Baseball
South Park Season 26 Kisscartoon
Rainbird Wiring Diagram
No Hard Feelings Showtimes Near Metropolitan Fiesta 5 Theatre
Athletic Squad With Poles Crossword
CHESAPEAKE WV :: Topix, Craigslist Replacement
T&G Pallet Liquidation
Crime Scene Photos West Memphis Three
Tribune Seymour
Locate Td Bank Near Me
Voyeuragency
Indiana Wesleyan Transcripts
Dover Nh Power Outage
Hdmovie2 Sbs
Georgia Cash 3 Midday-Lottery Results & Winning Numbers
Rimworld Prison Break
Www.patientnotebook/Atic
Rust Belt Revival Auctions
What Are The Symptoms Of A Bad Solenoid Pack E4od?
Tripcheck Oregon Map
Myra's Floral Princeton Wv
What Is Xfinity and How Is It Different from Comcast?
The Legacy 3: The Tree of Might – Walkthrough
Senior Houses For Sale Near Me
Help with your flower delivery - Don's Florist & Gift Inc.
Cvb Location Code Lookup
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
8 Ball Pool Unblocked Cool Math Games
Cheetah Pitbull For Sale
Cygenoth
Anhedönia Last Name Origin
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
Shipping Container Storage Containers 40'HCs - general for sale - by dealer - craigslist
Jamesbonchai
Sechrest Davis Funeral Home High Point Nc
Bmp 202 Blue Round Pill
Windy Bee Favor
Bama Rush Is Back! Here Are the 15 Most Outrageous Sorority Houses on the Row
10 Bedroom Airbnb Kissimmee Fl
Goosetown Communications Guilford Ct
Skyward Login Wylie Isd
Hampton Inn Corbin Ky Bed Bugs
March 2023 Wincalendar
Vcuapi
Chitterlings (Chitlins)
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5557

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.