# Creating a heatmap to visualize returns with R-ggplot2

We wished to visualize the monthly returns of the London ICE gasoil futures contract. To do so, we created a heatmap with R, using the ggplot2 library.

The original data were downloaded from the internet. They consisted on daily closing prices of the 1st nearby London ICE gasoil futures contract from April 1981 to October 2013. The monthly returns were computed as $ln(close_{month}-close_{month-1})$.

The R code below is straightforward. It can easily be modified to suit specific needs and serve as a basis for more advanced graphs. We also show the result.

###################################################
# Édouard Tallent @ TaGoMa.Tech, November 2013    #
# QuantCorner @ https://quantcorner.wordpress.com  #
###################################################

# Create a data object from the file object that actually is an object for the file containing the data

# Remove rows containing NAs
row.has.NA <- apply(data, 1, function(x){any(is.na(x))})
# sum(row.has.NA)
data <- data[!row.has.NA,]

# Create a xts object from the data object
data <- as.xts(data[,2], order.by=as.Date(data[,1], format='%d/%m/%Y')) # Create the xts object

# Compute the monthly log return
ret <- monthlyReturn(data, type='log', leading=TRUE)    # Compute the monthly returns

# Create a matrix mat object from ret
ret <- c(NA, NA, NA, ret, NA, NA)                               # The original time series nor starts on a Jan month nor ends on Dec
mat <- matrix(ret*100, ncol=12, byrow=TRUE, dimnames=NULL)      # Create a R matrix
year <- seq(from=1981, to=2013, by=1)                           # Vector containing the years
month <- c('Jan','Feb','Mar', 'Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')    # Vector containing the year months
colnames(mat) <- month  # Give columns names
rownames(mat) <- year   # Give rows names

# Transform the data in mat into a long series using the melt() function
molten <- melt(mat)         # Transform the series into a long series

# Define the color palette to be used in the hetmap
LtoM <-colorRampPalette(c('red', 'yellow' ))            # The spectrum of colors for the lowest returns
Mid <- "snow3"                                          # Snow3 is the color for the (approximatedly) median value
MtoH <-colorRampPalette(c('lightgreen', 'darkgreen'))   # The spectrum of colors for the highest values

# Plot the heatmap
hm <- ggplot(data=molten, aes(x=factor(Var2, levels=month.abb),
y=Var1, fill=value)) + geom_raster()                        # Draw the heatmap using geom_raster()
hm <- hm + labs(fill='Return (%)')                                          # The legend
hm <- hm + geom_text(aes(label=paste(sprintf("%.1f %%", value))), size=4)   # Write the monthly returns in hte heatmap
hm <- hm + scale_y_continuous(breaks=1981:2013)                             # Deal with the y-axis
hm <- hm + xlab(label=NULL) +   ylab(label=NULL)                            # No axis names
hm <- hm + theme_bw()                                                       # No background grey grid
hm <- hm + theme(axis.text.x=element_text(size=10, hjust=0, vjust=0.4,
angle=90))                                                  # Beautify the x-axis
hm <- hm + ggtitle(label='Monthly return - 1st nearby London ICE Gasoil futures contract')  # Add a title

print(hm)                                                                   # Print out the heatmap on the screen

Anuncios

## 4 pensamientos en “Creating a heatmap to visualize returns with R-ggplot2”

1. édouard Autor de la entrada

Hi Sean Patrick. Thanks for stopping at my blog.

Actually, my data are from a a komma-separated file with 2 columns that is one for the dates and another for the gasoil prices.

Tell me a bit more on the problem you are encountering, maybe. I will try to help you.

1. Sean Patrick Fallon (@Seanfallon)

Thx Edouard for the reply. I will try it again. I was downloading the file in OHLC format instead of just date and price. From what I understand, just two columns, first on date and the second one should be close price.

Let me know if I have understood this correctly.

1. édouard Autor de la entrada

Hi Sean Patrick.

I should probablx have been giving more details on the data source. You are right, the source file in my example (it is a flat file .txt) contains two columns, one for dates and the other one for closing prices.

Another thing is that it makes much sense to base the heatmap (in this case) on say monthly averaged returns rather than compute the returns using the first and the last values for each days. Quantmod and other R libraries do that in no time.

Edouard