1. Trelliscope in the tidyverse
In the previous chapter, we learned how to create interactive faceted displays with TrelliscopeJS by building on top of ggplot2. TrelliscopeJS provides another intuitive interface for creating displays that allows you to use many other plotting packages and provides a lot of flexibility in the specification of cognostics.
2. Stock market data
We will use a stock market dataset to illustrate the new principles in this chapter.
A dataset called stocks contains historical price data for the year 2016 for the 500 most-traded stocks on the NASDAQ exchange. This daily data contains the typical variables you will find in stock market data, including the open, close, high, and low prices of the day as well as the volume of shares traded.
3. Visualizing stock data
Suppose we wish to visualize the progression of each stock's price over the course of the year. This is a great candidate for faceting, and we could use ggplot2 to create a Trelliscope display of this.
However, suppose instead of using ggplot2, we want to create a nice interactive "candlestick" plot for each stock using a built-in function in the plotly package.
A candlestick chart shows the stock's open and close price in the body of the candlestick and the low and high as the upper and lower "wicks". If the close price is lower than the open price, the body is colored red, otherwise green. With plotly, we can interactively zoom and pan the chart and we can get additional information on hover.
Now suppose we want to use Trelliscope to make candlestick plots for every stock.
4. Tidyverse: nested data frames
To study different subsets of data individually, a common approach in the Tidyverse is to group the data by the variable or variables that define the subsets and nest the data for the other variables into nested data frames, resulting in a data frame with one row per subset.
For the stock data, we can study the data for each individual stock by grouping by the stock symbol and nesting the other variables. This results in a data frame with one row per stock with columns "symbol" and "data". The "data" column contains a data frame of all the data for the corresponding stock symbol. This is a convenient data structure for many tasks, and will form the basis for Tidyverse trelliscope plotting.
Here, the nest() function from the tidyr package takes the grouped data frame as input, notices the specified grouping variable, and performs the appropriate nesting.
5. Tidyverse: computing on nested data frames
To study individual stocks, we need to compute on the data for each stock. To compute on nested data frames, we can use a class of functions in the tidyverse purrr package, the map family. These apply a user-specified function to each element of the input and return a vector of the same length, with the output type determined by what is specified as the asterisk.
Here, we create a new variable last_close using map_dbl(), which returns a numeric vector.
6. Trelliscope in the tidyverse: plot columns
There are many map functions that create new variables of different types. One special variable type is a "plot column", which can be created using a function, map_plot(), provided with TrelliscopeJS.
Here, we apply our candlestick function to the stock data to obtain a new plot column panel that contains the candlestick plot for each symbol. Just as we have a data frame for each stock, we now have a plot for each stock.
7. Creating the display
We can turn a data frame containing a plot column into a trelliscope display by simply passing the data frame to a function, trelliscope(). Here, we create the plot, give it a name, and specify the initial layout.
8. Let's practice!
Now let's try some examples.