Skip to content

@timelyportfolio /apple comparison.r
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
apple compared to others
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
Something went wrong with that request. Please try again.