NZ Real GDP htmlwidget
[This article was first published on some real numbers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Thought I would try my hand at generating an interactive JavaScript line graph using R. Thankfully the dygraphs package makes this very easy!
The code below generates an interactive plot of New Zealand’s real GDP through time. I have added some annotations displaying some of the major financial crises. It is as if the economy fell of a cliff during the GFC!


I would highly recommend you give this package a look. The static images really don’t do it justice.
All data was sourced from the reserve bank website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# required packages | |
require(dplyr) | |
require(dygraphs) | |
require(readxl) | |
require(stringr) | |
require(lubridate) | |
require(xts) | |
# reading in the data | |
df.gdp = read_excel(path = 'data/graphdata.xlsx' | |
, sheet = 2 | |
, na = '' | |
, col_names = TRUE | |
, skip = 2) | |
# standard names | |
colnames(df.gdp) = make.names(colnames(df.gdp)) | |
# . removing the first row of rubbish | |
# . renaming the date column | |
# . removing missing values | |
# . removing the last row due to missing data | |
# . re-modelling the date to match the New Zealand FY | |
# . building the new date | |
# . removing un-needed columns | |
df.gpdClean = df.gdp %>% slice(-1) %>% | |
rename(Q = X) %>% | |
filter(!is.na(Q)) %>% | |
slice(-nrow(.)) %>% | |
mutate(mth = rep(c('06','03','09','12'),26) | |
, yr = substr(Q, 1, 4)) %>% | |
mutate(date = lubridate::ymd(paste0(yr,'-',mth,'-','01'))) %>% | |
select(-mth | |
, -yr | |
, -Q) | |
# creating a list of time series objects from columns in the data frame | |
ts.gpd = lapply(df.gpdClean[,-6], function(x) xts(x, order.by = df.gpdClean$date, frequency = 4)) | |
# fancy graph... | |
dygraph(ts.gpd[[1]], main = 'Real GDP (Annual Average % Change)', group = "nations") %>% | |
dyAxis("y", label = '%', valueRange = c(-5, 10)) %>% | |
dySeries("V1", label = "New Zealand (%)") %>% | |
dyOptions(drawPoints = TRUE, pointSize = 4, strokeWidth = 2, drawGrid = FALSE, colorSaturation = 0.2, colors = c('blue')) %>% | |
dyEvent("1997-7-01", "Asian crisis", labelLoc = "bottom") %>% | |
dyEvent("2000-03-10", "Dotcom Crash", labelLoc = "bottom") %>% | |
dyEvent("2008-9-01", "GFC", labelLoc = "bottom") |

To leave a comment for the author, please follow the link and comment on their blog: some real numbers.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.