A Look At A Few Financial Stocks Using R

Hi there. This page is about some of my findings about a few financial stocks when I was playing with R and its Quandl, ggplot2, and plotly packages. I look at closing stock market prices for Apple, Blackberry and Google.


Setting Up The Quandl Package In R

A Look At Apple Stock Prices

Blackberry Closing Stock Prices

Google Closing Stock Prices


Setting up Quandl In R

I have made a page sometime back in 2016 on setting up the Quandl package in R. Having the Quandl package set up allows the user to access Quandl’s financial database with  your own API key. (You need a login to get an API key.)

Here is the R code for setting up access to Quandl. (I load ggplot2 and plotly packages as well.)


A Look At Apple Stock Prices

To load financial stock, you need Quandl codes. Since I am using a free login account, I have access to free databases on Quandl. To load Apple stock data from the NASDAQ, I use the ticker “XNAS/AAPL”. I also preview the data using the head(), tail() and str() functions.

Using the head() and tail() functions does not really help here. It is better to see how the stock performs over time by plotting it.


A ggplot2 Plot Of Apple Stock Prices

In the code below, I plot the closing stock prices for Apple from 2010 to 2012. The x-axis would contain dates in the Month, Year format as indicated from the scale_x_date() part. The y-axis indicates the closing stock price. The labs() functions enables labels for axes and for the title. The theme() function allows for customization of fonts in terms of size, colours and positioning.

Stock prices in general have up movement and down movements. In the probability and statistics sense, stocks can be perceived as random. (Randomness can be measured by measures such as variance/standard deviation.)

For the Apple stock, it is easy to see that the general trend from Jan. 2010 to Jan. 2012 is upwards. Noticeable large up movements include February 2010 to Mid-April 2010, September 2010 to Mid-October 2010 and Mid-June 2011 to August 2011. These large up movements need further investigation as it could be caused by economic forces, new product announcements, hype, good news, etc.


A plotly Plot Of Apple Stock Prices

An alternative to using ggplot2 is plotly. Here is what I have come up with.

Blackberry Closing Stock Prices

The second stock I chose to look at was Blackberry.

Here is the ggplot2 plot for Blackberry.

Here is the plotly plot in R.

From January 2010 to January 2012, Blackberry has been a downward trend in its closing stock prices. (Apple was had its closing stock prices increasing in this same time period.) I think it had to do with Apple providing competition in the phone market, tablet market. Also, if I recall correctly I think in 2011 or 2012 Blackberry did not do really with its Playbook tablet. This tablet failure impacted its sales and stock prices fell as a result.

Today in 2017, Blackberry is still around (in the Waterloo, Ontario HQ) but it is not the big brand it once was.


Google Closing Stock Prices

This third example is on closing stock prices on Google from Yahoo.

Here is the ggplot2 plot of Google closing stock prices.

Here is the plotly plot for Google closing stock prices.

The overall trend for Google is upwards except for that big stock price drop at around January 2014. One could take a closer look at it.

You can add optional arguments in the Quandl function such as the start date and the end date. In this case, I have a start date of June 1, 2013 and an end date of June 1, 2014.

After some research (Google search), it turns out that the big drop in the stock price between March to April 2014 was from a stock split.



R Graphics Cookbook by Winston Chang (2012)



Leave a Reply