Emerging Currencies with rCharts + FRED
[This article was first published on Timely Portfolio, 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.
I liked this chart a lot.
#Dailychart: Argentina’s plunging peso is not alone: http://t.co/CWnaNLcm4T pic.twitter.com/4Nr6EgRGx9
— The Economist (@ECONdailycharts) January 27, 2014
I thought I would show how we can semi-replicate it in R with rCharts. Here it is with the currencies that are on FRED with dimplejs.
… and here with nvd3.
Code to replicate from Github Gist:
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
require(quantmod) | |
require(xtsExtra) | |
require(reshape2) | |
library(rCharts) | |
#turn off rstudio viewer which is default now | |
options(viewer=NULL) | |
currencies <- c( | |
"DEXBZUS", #Brazil | |
"DEXINUS", #India | |
"DEXSFUS", #South Africa | |
"DEXMXUS" #Mexico | |
) | |
getSymbols(currencies, src="FRED") | |
currencies.xts <- na.omit(do.call(merge,lapply(currencies,get))) | |
#get inverse for emerging currency/US$ | |
currencies.xts <- 1/currencies.xts | |
#do check for reasonableness | |
xtsExtra::plot.xts(currencies.xts["2007::",]) | |
#get pct change from 2013 | |
currencies.pct <- | |
currencies.xts["2013::",]/ | |
matrix( | |
rep( | |
as.vector(head(currencies.xts["2013",],1)), #first day of the year | |
NROW(currencies.xts["2013::",]) #repeat for each row | |
), | |
ncol=length(currencies), | |
byrow=T | |
) - 1 | |
#another reasonableness check | |
xtsExtra::plot.xts(currencies.pct) | |
xtsExtra::plot.xts(currencies.pct,screens=1) | |
currencies.df <- data.frame( | |
format(index(currencies.pct),"%Y-%m-%d"), | |
currencies.pct, | |
stringsAsFactors = FALSE | |
) | |
colnames(currencies.df) <- c("Date","Brazil","India","South Africa","Mexico") | |
currencies.df <- melt(currencies.df, id.vars=1) | |
colnames(currencies.df) <- c("Date","Country","ExchangeRate") | |
#dimple plot | |
d1 <- dPlot( | |
ExchangeRate ~ Date, | |
groups = "Country", | |
data = currencies.df, | |
type = "line" | |
) | |
d1$xAxis( | |
type = "addTimeAxis", | |
inputFormat = "%Y-%m-%d", | |
outputFormat = "%b %Y" | |
) | |
d1$yAxis( | |
outputFormat = "0.2%" | |
) | |
d1 | |
#nvd3 plot | |
#convert date | |
currencies.df$Date = as.Date(currencies.df$Date) | |
n1 <- nPlot( | |
ExchangeRate ~ Date, | |
group = "Country", | |
data = currencies.df, | |
type = "lineWithFocusChart" | |
) | |
n1$xAxis( | |
tickFormat = | |
"#! function(d) { | |
return d3.time.format('%b %Y')(new Date(d * 86400000)); | |
} !#" | |
) | |
n1$yAxis(tickFormat = "#!d3.format('0.2%')!#") | |
n1 |
To leave a comment for the author, please follow the link and comment on their blog: Timely Portfolio.
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.