StreamGraph

Alexander Ng

11/14/2020

An R Version of the D3.js Streamgraph Example

Leveraging the R package streamgraph which requires htmlwidgets, you can build the same streamgraph in R in about 3 lines of R code.

The package has some limitations, however. For example, no chart title seems to be possible on the interactive version - at least as an explicit parameter.

However, it is less complex to implement than the d3 version because the low level steps are abstracted by the single function call in R.

library(tidyverse)
library(streamgraph)
library(kableExtra)

After reading the raw data file into csv format, pivot it to a required long skinny format. Then because the index needs to recognized as a year value, add 1900 to the index to make it interpretable as a year.

yy = read_csv("ue_industry.csv")

yy %>% pivot_longer(!index, names_to = 'industry', values_to = "count") -> ts2

#
# In order to convert the X-axis to a year parameter 
# as required by the R streamgraph package.  We shift the index by a constant.
# ------------------------------------------------------------------------------
ts2$index = ts2$index + 1900

The resulting streamgraph below replicates the d3 result reasonably well.

pp2 = streamgraph(ts2, key= "industry",
                   value="count",
                  date="index", height="500px", width="800px", order = "inside-out")

pp2

Lastly, representative pivot table data is shown below.

head(ts2, n=20) %>% kable() %>% kable_styling(bootstrap_options = c("hover", "striped"))
index industry count
1900 Agriculture 154
1900 Business services 655
1900 Construction 745
1900 Education and Health 353
1900 Finance 228
1900 Government 430
1900 Information 125
1900 Leisure and hospitality 782
1900 Manufacturing 734
1900 Mining and Extraction 19
1900 Other 274
1900 Self-employed 239
1900 Transportation and Utilities 236
1900 Wholesale and Retail Trade 1000
1901 Agriculture 173
1901 Business services 587
1901 Construction 812
1901 Education and Health 349
1901 Finance 240
1901 Government 409