Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created January 27, 2014 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/8655338 to your computer and use it in GitHub Desktop.
Save timelyportfolio/8655338 to your computer and use it in GitHub Desktop.
Emerging Currencies with rCharts + nvd3 | dimplejs
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment