Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating stock charts


Given R's powerful analysis and graphical capabilities, it is no surprise that R is very popular in the world of finance. In this recipe, we will learn how to plot data from the stock market using some special libraries.

Getting ready

We need the tseries and quantmod packages to run the following recipes. Let's install and load these two packages:

install.packages("quantmod")
install.packages("tseries")
library(quantmod)
library(tseries)

How to do it...

Let's first see an example using the tseries library function, get.hist.quotes(). We will compare the stock prices of three technology companies:

aapl<-get.hist.quote(instrument = "aapl", quote = c("Cl", "Vol"))

goog <- get.hist.quote(instrument = "goog", quote = c("Cl", "Vol"))

msft <- get.hist.quote(instrument = "msft", quote = c("Cl", "Vol"))

plot(msft$Close,main = "Stock Price Comparison",
ylim=c(0,800)    ,col="red"  ,type="l"  ,lwd=0.5,
pch=19  ,cex=0.6  ,xlab="Date" ,ylab="Stock Price (USD)")

lines(goog...