Bubble Plots (ggplot2)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
1 Introduction
Rarely have I seen a three dimension graph including time, value, and volatility.
It is essential on risk management.
Used wisely, ggplot2 is a powerful and beautiful tool.
We can easily make many dimension graphs with ggplot2.
2 S&P500 and VIX (base)
The S&P500(Standard & Poor’s 500 Stock Index) is a capitalization-weighted index based on the common stock prices of 500 American companies.
On the other hand, VIX is a measure of the implied volatility of S&P500 index options.
At first, I make two graphs individually with R base package.
3 Line Graph (ggplot2)
Secondly, I make a graph including time,value, and volatility with R ggplot2 package.
Line colors represent volatility(Risk) level.
Red represent high volatility, blue represent low volatility.
When the S&P500 value is decreasing rapidly, volatility tend high.
This is why VIX is called “investor fear gauge”.
4 Bubble plots (ggplot2)
Thirdly, I make monthly(the beginning of the months) bubble plots.
Bubble size represent value of S&P500.
As same as line graph, Bubble color represent volatility level.
For instance, after the bankruptcy of Lehman Brothers, the bubble of October 2008 represent downturn trend and the most high volatility in the stock market (S&P500=968.75, VIX=59.89).
5 Appendix
Sample code of R is below:
# library library(ggplot2) # Datasets prc <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=^GSPC&d=0&e=1&f=2013&g=m&a=0&b=1&c=1990&ignore=.csv", as.is=T) vix <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=%5EVIX&a=00&b=2&c=1990&d=0&e=1&f=2013&g=m&ignore=.csv", as.is=T) # Data processing prc$Date <- as.Date(prc$Date) prc <- prc[, c(1,7)] colnames(prc)[2] <-c("Value") vix$Date <- as.Date(vix$Date) vix <- vix[, c(1,5)] colnames(vix)[2] <-c("VIX") df <- merge(prc, vix) df$year <- as.integer(substring(df$Date,1,4)) df$month <- as.integer(substring(df$Date,6,7)) # Graphs par(mfrow=c(2,1)) plot(df$Date, df$Value, type="l",main="S&P500", xlab="", ylab="") plot(df$Date, df$VIX, type="l",main="VIX ( VOLATILITY S&P 500) ", xlab="", ylab="") # Erase frame() par(mfrow=c(1,1)) # ggplot2 base layer p <- ggplot(df) # Line graph (p + geom_line(aes(x=Date, y=Value, colour=VIX)) + scale_colour_gradient(low="blue", high="red") ) # Bubble plots (p + geom_point(aes(x = month, y = year, size = Value, colour = VIX),shape=16, alpha=0.80) + scale_colour_gradient(limits = c(10, 60), low="blue", high="red", breaks= seq(10, 60, by = 10)) + scale_x_continuous(breaks = 1:12, labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) + scale_y_continuous(trans = "reverse") ) # fin.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.