public
Last active

apple compared to others

  • Download Gist
apple comparison.r
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 38 39 40 41 42 43
require(ggplot2)
 
#to get ggthemes from jrnold github if you have not installed
#require(devtools)
#install_github("ggthemes","jrnold")
require(ggthemes)
 
require(reshape2)
require(directlabels)
require(quantmod)
require(PerformanceAnalytics)
 
tckrs <- c("CSCO","MSFT","AAPL","^GSPC")
 
getSymbols(tckrs,from="1990-01-01")
 
prices <- na.omit(merge(CSCO[,6],MSFT[,6],AAPL[,6],GSPC[,6]))
colnames(prices) <- c("Cisco","Microsoft","Apple","SP500")
returns <- prices/lag(prices) - 1
returns[1,] <- 0
cumul <- cumprod(returns+1)
cumul.df <- as.data.frame(cbind(index(cumul),coredata(cumul)))
cumul.melt <- melt(cumul.df,id.vars=1)
colnames(cumul.melt) <- c("Date","Stock","Cumul")
cumul.melt[,"Date"] <- as.Date(cumul.melt[,"Date"])
 
direct.label(
ggplot(cumul.melt, aes(x=Date,y=log(Cumul),colour=Stock)) +
geom_line() +
theme_economist() + #if you want to play try theme_wsj() or theme_few()
scale_colour_economist() +
ggtitle("Apple Compared to Others Since 1990")
, list(last.bumpup,hjust=0.45,cex=0.65))
 
ggsave(plot=last_plot(),file="apple.png",width=6,height=4)
 
#for reference I will use my old favorite theEconomist from latticeExtra
require(latticeExtra)
direct.label(
asTheEconomist(xyplot(log(Cumul)~Date,data=cumul.melt,groups=Stock,
main="Apple Compared to Microsoft and Cisco Since 1990")
)
, list(last.bumpup,hjust=0.25,cex=1))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.