Created
December 5, 2012 16:44
-
-
Save timelyportfolio/4217278 to your computer and use it in GitHub Desktop.
apple compared to others
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment