Visualization with R: Time plots #rstats

[This article was first published on Blog of Statistical Estimation, 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.



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



[code]# 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)[/code]

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.

[code]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")[/code]

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

[code]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[/code]

Step 4: Draw the time plot!


[code]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”)[/code]



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.
[code]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”))[/code]



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 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.

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)