Plot Stock Prices with R

Connie Z
Analytics Vidhya
Published in
3 min readJan 25, 2021

--

http://www.financialtipsor.com/

Github: https://github.com/qyaan/stock-price-2020-big-tech

The article aims to plot the stock price movements of the three major technology companies (Apple, Google, Microsoft) and S&P500 in 2020 with R.

And there are three parts of analysis.

  • Prepare the envionment
  • Get the data
  • Plot
  1. Prepare the envionment

Package “quantmod” is frequently used for financial analysis, and package “ggplot2” for plotting.

install.packages("quantmod")
install.packages("broom"))
library(quantmod)
library(ggplot2)
library(magrittr)
library(broom)

2. Get the data

a. Time range

Before loading the data from yahoo finance, time range needed to be defined first.

start = as.Date("2020-01-01") 
end = as.Date("2021-01-22")

b. Get the stock prices

By using function “getSymbols”, needed stock price can be retrived. And there are three arguments need to be defined: tickers, sources, and time range.

In this case, the tickers needed are “AAPL”(Apple), “GOOGL”(Google), “MSFT”(Microsoft), “^GSPC”(S&P500). Source is yahoo finance.

getSymbols(c("AAPL", "GOOGL", "MSFT","^GSPC"), src = "yahoo", from = start, to = end)

Organize retrived stock prices as a data frame.

stocks = as.xts(data.frame(A = AAPL[, "AAPL.Adjusted"], 
B = GOOGL[, "GOOGL.Adjusted"], C = MSFT[, "MSFT.Adjusted"],
E = GSPC[,"GSPC.Adjusted"]))
names(stocks) = c("Apple", "Google", "Microsoft","S&P 500")
index(stocks) = as.Date(index(stocks))

3. Plot

a. First plot

The first plot is a chart without layer. And the idea is to get the general idea of how the movement of three big tech influence the movement of S&P 500.

But the problem of the first chart is the movement of Microsoft and Apple cannot be seen clearly, because their stock price is way lower than S&P 500 and Google. Therefore the second chart is created below 3b) to see the movement separately.

stocks_series = tidy(stocks) %>% 

ggplot(aes(x=index,y=value, color=series)) +
labs(title = "Top Three US Tech Comany and S&P 500: Daily Stock Prices January 2020 - January 2021 (1)",

subtitle = "End of Day Adjusted Prices",
caption = " Source: Yahoo Finance") +

xlab("Date") + ylab("Price") +
scale_color_manual(values = c("Red", "Black", "DarkBlue","Orange"))+
geom_line()
stocks_series

b. Second plot

The second plot is a chart with facet. And this time, the movement of individual stock price is much more clear.

stocks_series2 = tidy(stocks) %>% 

ggplot(aes(x=index,y=value, color=series)) +
geom_line() +
facet_grid(series~.,scales = "free") +
labs(title = "Top Three US Tech Comany and S&P 500: Daily Stock Prices January 2020 - January 2021 (2)",

subtitle = "End of Day Adjusted Prices",
caption = " Source: Yahoo Finance") +

xlab("Date") + ylab("Price") +
scale_color_manual(values = c("Red", "Black", "DarkBlue","Orange"))
stocks_series2

--

--

Connie Z
Analytics Vidhya
0 Followers

A curious&inspirational recorder.