-
Notifications
You must be signed in to change notification settings - Fork 3
/
ex_plot-stock-by-quarter.R
37 lines (30 loc) · 1.23 KB
/
ex_plot-stock-by-quarter.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## Get stock quote from quantmod, and plot quarterly opening values using ggplot
library(quantmod)
library(ggplot2)
library(dplyr)
splitindex <- function(x){
#split index of CRI.quarterly (format: YYYY Q#)
x.indexsplit <- strsplit(as.character(index(x)), split = " Q")
quarter <- unlist(lapply(x.indexsplit, "[[", 2))
year <- unlist(lapply(x.indexsplit, "[[", 1))
data.frame(quarter = as.numeric(quarter), year = as.numeric(year), stringsAsFactors=FALSE)
}
factor2character <- function(df){
if(!is.data.frame(df)) stop("df must be of class data.frame")
for(i in which(sapply(df, class) == "factor")) df[[i]] = as.character(df[[i]])
return(df)
}
# get stock quote and extract quarterly data
# reference on subsetting quantmod object: http://www.quantmod.com/examples/data/#subset
symbol <- "CRI"
# get stock quote if don't have it already
if(!(symbol %in% ls())){
getSymbols(symbol)
}
# extract quarterly data
CRI.quarterly <- to.quarterly(CRI)
df.quarters <- splitindex(CRI.quarterly)
df <- cbind(df.quarters, open = as.numeric(CRI.quarterly[,1]))
# line ggplot (x axis: quarter, y axis: open, faceted by year)
ggplot(df, aes(quarter, open)) + geom_line() + facet_grid(.~year) +
ggtitle(paste(symbol,"Opening Price (by Quarter)"))