2019年9月3日火曜日

ggplot() geom_bar() その4  SPX weekly ROI for the past 69 years.



SPX weekly ROI for the past 69 years. "Sell in May, come on back on St. Leger's Day." seems to be highly reasonable.

## codes begin

len <- length(index(to.weekly(SP5)))
w <-  to.weekly(SP5)[,4][2:len]/as.vector(to.weekly(SP5)[,4][1:(len-1)])
w <- w-1
weekloc <- floor(length(seq(as.Date(paste(year(Sys.Date()),"-01-01",sep="")),Sys.Date(),by="days"))/7)
weekorder <- c(last(seq(1,52,1),weekloc),head(seq(1,52,1),52-weekloc))
mtrx <- matrix(last(w,floor(length(w)/52)*52),nrow=52)
df <- data.frame(data=apply(mtrx,1,mean)[weekorder],mon=factor(seq(1,52,1)))
p <- ggplot(df, aes(x=mon,y=data,fill=mon)) 
p <- p + scale_x_discrete(label=as.character(seq(1,52,1)))
p <- p + geom_bar(stat = "identity") # need identity to draw value itself. 
p <- p + theme(legend.position = 'none')  # erase legend
plot(p)

## codes end


0 件のコメント: