How to create drill-down graphs with highcharter in R

Drill-down visualizations can be a superior way to current a good deal of information in a digestible format. In this instance, we’ll make a graph of median home values by U.S. point out working with R and the highcharter offer.

Median home values by state Sharon Machlis, IDG

Preliminary graph of median home values by point out (optimum and least expensive 10 states). Info from Zillow.

Just about every state’s bar will be clickable — the drilldown — to see information by county.

Graph of median home values in Massachusetts counties Sharon Machlis, IDG

Immediately after clicking the bar for Massachusetts, a person sees median home values by Massachusetts county. Info from Zillow.

There are a few primary methods to generating a drill-down graph with highcharter:

  1. Wrangle your information into the necessary format 
  2. Make a simple prime-level graph and 
  3. Incorporate the drill-down.

If you want to comply with alongside, download point out- and county-level information sets for the Zillow House Price Index from Zillow at https://www.zillow.com/exploration/information/. I’m working with the ZHVI Solitary-Family Residences series.

To start with, load the packages we’ll be working with:

library(rio)
library(dplyr)
library(purrr)
library(highcharter)
library(scales)
library(stringr)

All can be set up from CRAN with put in.packages() if you don’t currently have them on your procedure.

Take note that highcharter is an R wrapper for the Highcharts JavaScript library — and that library is only no cost for particular, non-business use (including tests it locally), or use by non-income, universities, or community faculties. For nearly anything else, including governing administration use, you need to buy a license. 

Following, I import the point out and county CSV files into R with the pursuing code. (My CSV files are in a information subfolder of my project listing.)

states <- import("data/State_zhvi.csv")
counties <- import("data/County_zhvi.csv")

These files have hundreds of columns, one particular for each thirty day period setting up in 1996. I want to graph the most the latest information, so I glimpse for the identify of the last column with

names(states)[ncol(states)]

At the time I wrote this, that returned 2020-06-thirty, which I’ll use as my MedianValue column. I’d like to examine that price to the start out of the century, so I’ll also incorporate 2020-01-31 as a PriceIn2000 column.

Info wrangling

Here’s my code for generating a most recent_states information body, which I’ll use as a base for the graph:

Copyright © 2020 IDG Communications, Inc.