This gets the S&P 500 time series from Yahoo using the tseries package function get.hist.quote which returns a zoo object. As we’re using ggplot2 we have to convert the zoo series into a data frame.
1 2 3 4 5 6 7 8 9 10 11 |
library(zoo) library(ggplot2) library(tseries) spx <- get.hist.quote(instrument="^gspc", start="2000-01-01", end=Sys.Date(), quote="AdjClose", provider="yahoo", origin="1970-01-01", compression="d", retclass="zoo") spx.df <- data.frame(x=time(spx),y=spx$AdjClose) ggplot(spx.df) + geom_line(aes(x=x,y=y)) + theme_bw() |
Alternatively if you don’t want to use ggplot2 the old-school plot function works just fine, but doesn’t look as good.
1 |
plot(spx) |