R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

You need to load the following libraries.

options("getSymbols.warning4.0"=FALSE)
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(magrittr))
suppressPackageStartupMessages(library(dplyr))

Data analysis

data(diamonds, package = "ggplot2")
plot(color ~ price, diamonds,  las = 1, 
        ylab = "The proposed color of the diamonds", 
        xlab = "The price of the diamonds",
        main="Total medals awarded 
     by ordinal position of Olympics with
     predicted three parameter power function fit displayed.",
        las = 1,
        bty="l")

library(xtable)
## 
## Attaching package: 'xtable'
## The following object is masked from 'package:timeSeries':
## 
##     align
## The following object is masked from 'package:timeDate':
## 
##     align
prices <- aggregate(cbind(x,y,z)~cut+color, data=diamonds, sum, na.rm=TRUE)
print(xtable(prices, 
             caption='List of Australian Medals',
             digits=0),
      type='html', 
      caption.placement='top', 
      include.rownames=FALSE,
      html.table.attributes='align="center"')

This is another plot

library(ggplot2)
ggplot(mtcars, aes(y = mpg, x = wt, size = hp)) + geom_point() + stat_smooth(method = "lm", se = FALSE)

This is how to display tables

library(knitr)
kable(head(mtcars), digits = 2, align = c(rep("l", 4), rep("c", 4), rep("r", 4)))

If we use xtable then this is how our plot will look like.

The fancy plot - plot.default (As Old as Time Itself)

# base plot of time series prior to xts
# get the data in data.frame format rather than xts
sp500.df <- data.frame(
  index(sp500.monthly),
  coredata(sp500.monthly),
  stringsAsFactors=FALSE
)
# name columns
colnames( sp500.df ) <- c( "date", "sp500" )
# go back in time to plot.default from the graphics library
graphics::plot.default(
  x = sp500.df$date,
  y = sp500.df$sp500,
  type = "l",
  xlab = "Date",
  ylab = "Closing Value",
  main = "S&P 500 (graphics::plot.default)"
)

stats::plot.ts(
  ts(sp500.monthly,
     start = c(
       as.numeric(format(index(sp500.monthly)[1],"%Y")),
       as.numeric(format(index(sp500.monthly)[1],"%m"))
     ),
     frequency = 12
  ), # some backwards conversion to ts from xts
  xlab = "Date",
  ylab = "Closing Value",
  main = "S&P 500 (stats::plot.ts)"
)

xyplot(
  sp500 ~ date,
  data = sp500.df,
  type = "l",
  main = "S&P 500 (lattice::xyplot)"
)

zoo::plot.zoo(
  sp500.monthly,
  main = "S&P 500 (zoo::plot.zoo)"
)

# although slightly out of chronology
# I'll also use theEconomist from latticeExtra
asTheEconomist(
  xyplot(
    sp500.monthly,
    scales = list( y = list( rot = 0 ) ),
    main = "S&P 500 (lattice::xyplot.xts)"  
  )
)

# 2007-02-02 chart.TimeSeries in PerformanceAnalytics
charts.PerformanceSummary(
  ROC(sp500.monthly, n = 1, type = "discrete"),
  main = "S&P 500 (PerformanceAnalytice::charts.PerformanceSummary)"
)

#ggplot2 requires conversion of xts to data.frame
#we will use the data.frame from the plot.default example
ggplot( sp500.df, aes(date) ) + 
  geom_line( aes( y = sp500 ) ) +
  labs( title = "S&P 500 (ggplot2::ggplot)")

chartSeries(
  sp500.monthly,
  theme = chartTheme("white"),
  TA = c(addBBands(),addTA(RSI(sp500.monthly)))
)

# also easy zooming
zoomChart("1990::")

# 2008-02-17 xts improved zoo and other time series libraries
# http://cran.r-project.org/web/packages/xts/vignettes/xts.pdf
# plot.zoo got ported to plot.xts and little graphing improvement
xts::plot.xts(
  sp500.monthly,
  ylab = "Closing Value",
  main = "S&P 500 (xts::plot.xts)"  
)

timeSeries::plot(
  timeSeries(sp500.monthly),
  main = "S&P 500 (timeseries::plot)"
)

g1 <- gvisLineChart(
  data = sp500.df,
  xvar = "date",
  yvar = "sp500",
  options = list(
    title = "S&P 500 (googleVis::gvisLineChart)",
    height = 400,
    width = 600
  )
)
print(g1,"chart")
autoplot.zoo(sp500.monthly) + 
  ggtitle("S&P 500 (zoo::autoplot.zoo)")

Introduction to R Charts

Working with rCharts in R

This plot is based on MorrisJS java library The data was obtatined from a package in R ggplot. The chart has to have a width and a height

Another package to consider is d3js library, NVD3, which produces amazing interactive visualizations with little customization.

Subsetting the data

Creating Bubble Plots in R: This is an example of a high chart