A personal blog about my fumblings with statistics, finance and anything R


Built using jekyll and hyde

RCharts

I just got introduced to the wonderful rCharts package and I had to write a about it. I’ll be using the NVD3 library for all the graphs. Thanks to Ramnath Vaidyanathan for this awesome package. Charting is so much more fun.

Data and Libraries

Let’s play with some stock prices.

#install.packages('devtools')
#devtools::install_github('rcharts','ramnathv')
library(rCharts)
library(PerformanceAnalytics)
library(quantmod)
library(ggplot2)
library(knitr)

getSymbols(Symbols = "AAPL")

[1] “AAPL”

Base Graphics

plot(AAPL)

center

ggplot2

df = data.frame(y = as.numeric(AAPL[,4]),
                x = index(AAPL))
ggplot(df, aes(x,y)) + geom_line() + xlab("") + ylab("AAPL") + ggtitle("APPLE")

center

NVD3

####Line Chart

mat = data.frame(date = index(AAPL), AAPL = as.numeric(AAPL[,4]))
p = nPlot(AAPL ~ date, data = mat, type = "lineChart")

# Title
p$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
p$set(title = "AAPL Closing Price")

#X Axis Properties
p$xAxis(axisLabel = 'Time',showMaxMin = 'false',
        tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#",
        width = 50)

#Y Axis Properties
p$yAxis(axisLabel = 'Prices',
        showMaxMin = 'false',
        tickFormat = "#! function(d) {return d3.format(',.1%')(d)} !#",
        width = 70)

#Size
p$set(width = 1024)

#Margins
p$chart(margin = list(left = 150,
                      right = 100))
# p$show('iframesrc', standalone = TRUE) #Not run
p$save('LineChart.html', standalone = TRUE) 

####Line Chart with Focus

mat = data.frame(date = index(AAPL), AAPL = as.numeric(AAPL[,4]))
p = nPlot(AAPL ~ date, data = mat, type = "lineWithFocusChart")

# Title
p$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
p$set(title = "AAPL Closing Price")

#X Axis Properties
p$xAxis(axisLabel = 'Time',showMaxMin = 'false',
        tickFormat="#!function(d) {return d3.time.format('%b %Y')(new Date( d * 86400000 ));}!#",
        width = 50)

#Y Axis Properties
p$yAxis(axisLabel = 'Prices',
        showMaxMin = 'false',
        tickFormat = "#! function(d) {return d3.format(',.1%')(d)} !#",
        width = 70)

#Size
p$set(width = 1024)

#Margins
p$chart(margin = list(left = 150,
                      right = 100))
# p$show('iframesrc', standalone = TRUE) #Not run
p$save('LineChartFocus.html', standalone = TRUE) 

####Barchart

getSymbols(Symbols = c("AAPL","AMZN", "MSFT", "IBM"))

[1] “AAPL” “AMZN” “MSFT” “IBM”

mat = na.omit(merge(AAPL[,4], AMZN[,4], MSFT[,4], IBM[,4]))
mat = CalculateReturns(prices = mat)
df = table.DrawdownsRatio(R = mat)
df = data.frame(Categories = rownames(df),
                Apple = df[,1],
                Amazon = df[,2],
                MSFT = df[,3],
                IBM = df[,4])

df = reshape2::melt(df, id = "Categories")

#Plot
p = nPlot(value ~ variable, group = 'Categories', data = df, type = "multiBarChart")

# Title
p$templates$script <- "http://timelyportfolio.github.io/rCharts_nvd3_templates/chartWithTitle.html"
p$set(title = "Drawdown Ratios")

#X Axis Properties
p$xAxis(axisLabel = 'Stocks',showMaxMin = 'false',width = 50)

#Y Axis Properties
p$yAxis(axisLabel = 'Drawdown Ratios',
        showMaxMin = 'false',
        tickFormat = "#! function(d) {return d3.format(',.0')(d)} !#",
        width = 70)

#Size
p$set(width = 1000)

#Margins
p$chart(margin = list(right = 100,left = 100),
        groupSpacing = 0.1,
        reduceXTicks = FALSE,
        rotateLabels = 0,
        transitionDuration = 350)

#Save
# p$show('iframesrc', standalone = TRUE) #Not run
p$save('BarChart.html',standalone = "TRUE")