# Bubble Plots (ggplot2)

July 8, 2012
By

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. “Can’t Complain!”

## 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”. “It’s cool!”

## 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). “Like ? “

## 5 Appendix

Sample code of R is below:

```# library
library(ggplot2)

# Datasets

# Data processing
prc\$Date <- as.Date(prc\$Date)
prc <- prc[, c(1,7)]
colnames(prc) <-c("Value")

vix\$Date <- as.Date(vix\$Date)
vix <- vix[, c(1,5)]
colnames(vix) <-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)) +
)

# 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.