Charting with tidyquant

Matt Dancho

2017-01-21

Charting financial data using ggplot2

Overview

The tidyquant package has added charting tools to assist its users in developing quick visualizations without data manipulation and formatting by integrating with ggplot2 using the grammar of graphics format and workflow. There are currently three primary geometry (geom) categories and one coordinate manipulation (coord) category within tidyquant:

Prerequisites

Load the tidyquant package to get started.

# Loads tidyquant, tidyverse, lubridate, xts, quantmod, TTR 
library(tidyquant)  

The following stock data will be used for the examples. Use tq_get to get the stock prices.

AAPL <- tq_get("AAPL", get = "stock.prices", from = "2015-09-01", to = "2017-01-01")
AMZN <- tq_get("AMZN", get = "stock.prices", from = "2000-01-01", to = "2017-01-01")
FANG <- c("FB", "AMZN", "NFLX", "GOOG") %>%
    tq_get(get = "stock.prices", from = "2015-09-01", to = "2017-01-01") 

The end date parameter will be used when setting date limits throughout the examples.

end <- as_date("2017-01-01")

Chart Types

Financial charts provide visual cues to open, high, low, and close prices. The following chart geoms are available:

Line Chart

Before we visualize bar charts and candlestick charts using the tidyquant geoms, let’s visualize stock prices with a simple line chart to get a sense of the “grammar of graphics” workflow. This is done using the geom_line from the ggplot2 package. The workflow begins with the stock data, and uses the pipe operator (%>%) to send to the ggplot() function.

The primary features controlling the chart are the aesthetic arguments: these are used to add data to the chart by way of the aes() function. When added inside the ggplot() function, the aesthetic arguments are available to all underlying layers. Alternatively, the aesthetic arguments can be applied to each geom individually, but typically this is minimized in practice because it duplicates code. We set aesthetic arguments, x = date and y = close, to chart the closing price versus date. The geom_line() function steals the aesthetic arguments from the ggplot() function and produces a line on the chart. Labels are added separately using the labs() function. Thus, the chart is built from the ground up by starting with data and progressively adding geoms, labels, coordinates / scales and other attributes to create a the final chart. This is enables maximum flexibility wherein the analyst can create very complex charts using the “grammar of graphics”.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_line() +
    labs(title = "AAPL Line Chart", y = "Closing Price", x = "")

Bar Chart

Visualizing the bar chart is as simple as replacing geom_line with geom_barchart in the ggplot workflow. Because the bar chart uses open, high, low, and close prices in the visualization, we need to specify these as part of the aesthetic arguments, aes(). We can do so internal to the geom or in the ggplot() function.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Bar Chart", y = "Closing Price", x = "")

We zoom into specific sections using coord_x_date, which has xlim and ylim arguments specified as c(start, end) to focus on a specific region of the chart. For xlim, we’ll use lubridate to convert a character date to date class, and then subtract six weeks using the weeks() function. For ylim we zoom in on prices in the range from 100 to 120.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Bar Chart", 
         subtitle = "Zoomed in using coord_x_date",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end),
                 ylim = c(100, 120))

The colors can be modified using color_up and color_down arguments, and parameters such as size can be used to control the appearance.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_barchart(aes(open = open, high = high, low = low, close = close),
                     color_up = "darkgreen", color_down = "darkred", size = 1) +
    labs(title = "AAPL Bar Chart", 
         subtitle = "Zoomed in, Experimenting with Formatting",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end),
                 ylim = c(100, 120))

Candlestick Chart

Creating a candlestick chart is very similar to the process with the bar chart. Using geom_candlestick, we can insert into the ggplot workflow.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Candlestick Chart", y = "Closing Price", x = "")

We zoom into specific sections using coord_x_date.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "AAPL Candlestick Chart", 
         subtitle = "Zoomed in using coord_x_date",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end),
                 ylim = c(100, 120))

The colors can be modified using color_up and color_down, which control the line color, and fill_up and fill_down, which control the rectangle fills.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close),
                        color_up = "darkgreen", color_down = "darkred", 
                        fill_up  = "darkgreen", fill_down  = "darkred") +
    labs(title = "AAPL Candlestick Chart", 
         subtitle = "Zoomed in, Experimenting with Formatting",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end),
                 ylim = c(100, 120))

Combining Chart Geoms

Because the geoms are applied as layers, we can combine multiple chart types. The order of the geoms matters: the further down geoms will be overlayed on the preceding geoms. We’ll build up the plot starting with the candlestick, adding a line, and then adding a points.

AAPL %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
    geom_line(color = "pink", size = 1.5) +
    geom_point(color = "purple", size = 3) +
    labs(title = "AAPL Candlestick Chart", 
         subtitle = "Combining Chart Geoms",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end),
                 ylim = c(100, 120))

Charting Multiple Securities

We can use facet_wrap to visualize multiple stocks at the same time. By adding a group aesthetic in the main ggplot() function and combining with a facet_wrap() function at the end of the ggplot workflow, all four “FANG” stocks can be viewed simultaneously.

FANG %>%
    ggplot(aes(x = date, y = close, group = symbol.x)) +
    geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
    labs(title = "FANG Candlestick Chart", 
         subtitle = "Experimenting with Mulitple Stocks",
         y = "Closing Price", x = "") + 
    coord_x_date(xlim = c(end - weeks(6), end)) +
    facet_wrap(~ symbol.x, ncol = 2, scale = "free_y")

ggplot2 Functionality

Base ggplot2 has a ton of functionality that can be useful for analyzing financial data. We’ll go through some brief examples using Amazon (AMZN).

Example 1: Log Scale with scale_y_log10

ggplot2 has the scale_y_log10() function to scale the y-axis on a logarithmic scale. This is extremely helpful as it tends to expose linear trends that can be analyzed.

Continuous Scale:

AMZN %>%
    ggplot(aes(x = date, y = adjusted)) +
    geom_line() + 
    scale_y_continuous() +
    labs(title = "AMZN Line Chart", 
         subtitle = "Continuous Scale", 
         y = "Closing Price", x = "")

Log Scale:

AMZN %>%
    ggplot(aes(x = date, y = adjusted)) +
    geom_line() + 
    scale_y_log10() +
    labs(title = "AMZN Line Chart", 
         subtitle = "Log Scale", 
         y = "Closing Price", x = "")

Example 2: Regression trendlines with geom_smooth

We can apply a trendline quickly adding the geom_smooth() function to our workflow. The function has several prediction methods including linear ("lm") and loess ("loess") to name a few.

Linear:

AMZN %>%
    ggplot(aes(x = date, y = adjusted)) +
    geom_line() + 
    scale_y_log10() +
    geom_smooth(method = "lm") +
    labs(title = "AMZN Line Chart", 
         subtitle = "Log Scale, Applying Linear Trendline", 
         y = "Adjusted Closing Price", x = "")

Loess:

AMZN %>%
    ggplot(aes(x = date, y = adjusted)) +
    geom_line() + 
    scale_y_log10() +
    geom_smooth(method = "loess") +
    labs(title = "AMZN Line Chart", 
         subtitle = "Log Scale, Applying Loess Trendline", 
         y = "Adjusted Closing Price", x = "")

Example 3: Charting volume with geom_bar

We can use the geom_bar() function to chart daily volume. The default stat = ..density.. so we need to change this to "identity", which uses the actual values instead of the transformed (summarized) density values.

AMZN %>%
    ggplot(aes(x = date, y = volume)) +
    geom_bar(stat = "identity") + 
    geom_smooth(method = "loess", se = FALSE) +
    labs(title = "AMZN Bar Chart", 
         subtitle = "Charting Daily Volume", 
         y = "Volume", x = "")  

And, we can zoom in on a specific region. We use scale_X_date to remove the out-of-bounds data, which allows the fill aesthetic to be controlled by volume.

AMZN %>%
    ggplot(aes(x = date, y = volume, fill = volume)) +
    geom_bar(stat = "identity") + 
    geom_smooth(method = "loess", se = FALSE) +
    labs(title = "AMZN Bar Chart", 
         subtitle = "Charting Daily Volume, Zooming In", 
         y = "Volume", x = "") + 
    scale_x_date(limits = c(end - weeks(24), end)) +
    scale_fill_gradient(low = "red", high = "darkblue") +
    theme(legend.position = "none")

Recap

The tidyquant package has added charting tools to assist the users in developing quick visualizations without data manipulation and formatting. Several ggplot2 geoms and coords are available to assist in the rapid visualization process.