Visualization with R: Time plots #rstats

September 23, 2016
By

(This article was first published on Blog of Statistical Estimation, and kindly contributed to R-bloggers)

R is a great tool to visualize your data: it is free to use and has lots packages to make beautiful plots. In this post, we gonna teach you how to make time plots to visualize stock returns with data from Yahoo finance. For those not familiar with how to automatically download data from Yahoo Finance with R, we suggest that you take a look at our recent presentation.  Or just follow the steps bellow and copy the code and paste it to R console (RStudio is recommended).

Step 1: Setting the working environment

# Setting the working environment -----------------------------------------
## Install packages for this tutorial
install.packages("quantmod",dependencies = T)
install.packages("PerformanceAnalytics",dependencies = T)
##Make sure R gives English outputs
Sys.setlocale(category = "LC_ALL", locale = "english")
##increase the threshold so R will not convert the ouput into scientific notation
options(scipen = 100)

Step 2: Loading Data from Yahoo Finance

We gonna extract US stocks prices (SP500) and Australian Stocks prices (ASX200) from Yahoo Finance with quanmod package. Notice that we further convert the daily prices into monthly with the command to.period.

library(quantmod)
SP500 <- getSymbolsundefined"^GSPC",from = "2000-01-01",to = "2016-08-31", auto.assign = FALSE) #Retrieve data for SP500 from yahoo finance
ASX200 <- getSymbolsundefined"^AXJO",from = "2000-01-01",to = "2016-08-31", auto.assign = FALSE)
M.SP500 <- to.periodundefinedSP500,period = "months")
M.ASX200 <- to.periodundefinedASX200,period = "months")

Step 3: Converting prices into returns

Here, we use adjusted closing prices which include the adjustments for divident payments and stock splits to calculate total returns or holding period returns. If you want to calculate price return or capital gains, you use closing prices

price.M.SP500 <- M.SP500[,6]
Return.M.SP500 <- diff(log(price.M.SP500))[-1] #-1 is used to delete the NA value
price.M.ASX200 <- M.ASX200[,6]
Return.M.ASX200 <- diff(log(price.M.ASX200))[-1] #-1 is used to delete the NA value

Step 4: Draw the time plot!

library(PerformanceAnalytics)
chart.TimeSeries(cbind(Return.M.SP500,Return.M.ASX200),legend.loc="bottomleft",date.format ="%b-%Y",las = 2, ylab = "Returns",main = "Time Series Plot")

A little more

We can add grey shaded areas to the plot to mark the important period like the period of global financial crisis(set from 2007-01-31 to 2008-12-31 in this tutorial). This is done by using the arguments, period.areas and period.color.

chart.TimeSeries(cbind(Return.M.SP500,Return.M.ASX200),legend.loc="bottomleft",date.format ="%b-%Y",las = 2, ylab = "Returns",main = "Time Series Plot",period.areas = c("2007-01-31::2008-12-31"),period.color = c("gray"))

To leave a comment for the author, please follow the link and comment on their blog: Blog of Statistical Estimation.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)