Bubble Plots (ggplot2)

July 8, 2012

(This article was first published on Knowledge Discovery » R, and kindly contributed to R-bloggers)

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

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

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

To leave a comment for the author, please follow the link and comment on their blog: Knowledge Discovery » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)