Dynamic Timeseries Plotting

We will directly download precious metal price data to R with quantmod package and create a dynamic plot with dygraphs.

  • Objectives: basic use of quantmod and dygraphs
  • Requirements: R Basics

Data Download and Understanding

We begin by loading required packages: quantmod and dygraphs. You can install quantmod with basic install.packages() from CRAN. As of date of writing you need to install dygraphs directly from github. For this you can use the function call below. After installing it once, you can load it.

suppressPackageStartupMessages(library(quantmod))
#devtools::install_github(c("ramnathv/htmlwidgets", "rstudio/dygraphs"))  # run once for install
suppressPackageStartupMessages(library(dygraphs))

Now we download data. This is done with getMetals() from quantmod package. Metals are passed as a vector with ticker names for metals. “From” is supposed to be the start of the period, but only the last 5 years are provided as we will see. So this parameter is partially ignored, but if you don’t set it a much shorter period is provided.

For each of these metals an xts-object is stored. These objects are combined with cbind to one single xts-object.

quantmod::getMetals(Metals = c('XAU', 'XAG', 'PALL', 'PL'),
      from="2000-01-01",
      to = Sys.Date())
## [1] "XAUUSD" "XAGUSD" "XPDUSD" "XPTUSD"
metals <- cbind(XAGUSD, XAUUSD, XPDUSD, XPTUSD)

Creating the Plot

Let’s skip the first line. We will come to that later.

Plot is created with dygraph() function. Similar to ggplot() you start with basic information on dataframe, and then put more aesthetics on top. Most importantly “data” is passed. It has to be a timeseries xts-object. Some general information title, x- and y-label can be passed as well. More definitions on how the plot should look and feel can be added with piping “%>%”.

With dyRebase() all prices are referenced to 100 at beginning of period.

dyRangeSelector() defines highlighted area. Plot can be extended to other periods with slider at the bottom of the page.

dy_window <- c("2018-08-25", "2019-02-20")

dygraph(data = metals, 
    xlab = "Time", 
    ylab = "Price [-]",
    main = "Gold, Silver, Platin and Palladium Performance") %>%
    dyRebase(value = 100) %>% 
    dyRangeSelector(dateWindow = dy_window)

plot of chunk dygraph

Click on the graph to get a full interactive version.

This plot is very dynamic. You can modify time range by changing start or end, or moving with a fixed period. Specific areas can be zoomed in by clicking on the graph. If you want to unzoom just double-click in the graph. Prices are always shown for current position at the top of the plot.

All metals increased prices since September 2018, Palladium by more than 60 %. But this picture changes when period is extended to the last 5 years.

More Information

There is more to discover. Take a look at these websites to learn more about financial modeling with quantmod or dynamic timeseries plotting with dygraphs.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close