Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
calc and draw aapl_moving ave
data <- read.csv('stock_px.csv')
#setup libraries
options(CRAN="http://cran.ism.ac.jp/")
options(repos="http://cran.ism.ac.jp/")
install.packages('ggplot2')
install.package('reshape2')
library(ggplot2)
library(reshape2)
colnames(data)[1] <- "dtm"
data.bind <- cbind(1:nrow(data), data)
d30 <- rep(1,30)/30; d90 <- rep(1,90)/90; d180 <- rep(1,180)/180
AAPL <- data.bind$AAPL
AAPLd30 <- filter(data.bind$AAPL, filter=d30, sides=2)
AAPLd90 <- filter(data.bind$AAPL, filter=d90, sides=2)
AAPLd180 <- filter(data.bind$AAPL, filter=d180, sides=2)
AAPL.bind <- data.frame(data.bind$dtm, AAPL, AAPLd30, AAPLd90, AAPLd180)
# head(AAPL.bind)
colnames(AAPL.bind)[1] <- "dtm"
AAPL.melted <- melt(AAPL.bind, id.var=c("dtm"))
#head(AAPL.melted)
apl <- ggplot(AAPL.bind, aes(x=as.Date(AAPL.melted$dtm), y=AAPL.melted$value, colour=AAPL.melted$variable))
apl <- apl + geom_line(size=1)
apl <- apl + xlab("Date") + ylab("stock") + ggtitle('AAPL stock moving ave') + labs(colour="apple-ave-range")
apl.msft <- data.bind[c(2,3,4)]
apl.msft.melted <- melt(apl.msft, id.var=c("dtm"))
apl.msft <- ggplot(MSFT, aes(x=as.Date(apl.msft.melted$dtm), y=apl.msft.melted$value, colour=apl.msft.melted$variable))
apl.msft <- apl.msft + geom_line(size=1)
apl.msft <- apl.msft + xlab('Date') + ylab('stock') + ggtitle('MSFT stock price') + labs(colour="apple-msft")
library(grid)
#call grid.newpage is here
grid.newpage()
pushViewport(viewport(layout=grid.layout(2, 1) ))
print(apl, vp=viewport(layout.pos.row=1, layout.pos.col=1))
print(apl.msft, vp=viewport(layout.pos.row=2, layout.pos.col=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.