Search code examples
rformattingheatmapquandl

How to Format Numbers in Heatmap.2 in R


I've taken this code from this site to make a correlation matrix heatmap. How do I format the numbers in the heatmap to have only 2 decimal places worth?: http://blog.revolutionanalytics.com/2014/08/quantitative-finance-applications-in-r-8.html

    library(xts)
    library(Quandl)

    my_start_date <- "1998-01-05"
    SP500.Q <- Quandl("YAHOO/INDEX_GSPC", start_date = my_start_date, type = "xts")
    RUSS2000.Q <- Quandl("YAHOO/INDEX_RUT", start_date = my_start_date, type = "xts")   
    NIKKEI.Q <- Quandl("NIKKEI/INDEX", start_date = my_start_date, type = "xts") 
    HANG_SENG.Q <- Quandl("YAHOO/INDEX_HSI", start_date = my_start_date, type = "xts") 
    DAX.Q <- Quandl("YAHOO/INDEX_GDAXI", start_date = my_start_date, type = "xts") 
    CAC.Q <- Quandl("YAHOO/INDEX_FCHI", start_date = my_start_date, type = "xts") 
    KOSPI.Q <- Quandl("YAHOO/INDEX_KS11", start_date = my_start_date, type = "xts")   

    # Depending on the index, the final price for each day is either 
    # "Adjusted Close" or "Close Price".  Extract this single column for each:
    SP500 <- SP500.Q[,"Adjusted Close"]
    RUSS2000 <- RUSS2000.Q[,"Adjusted Close"]
    DAX <- DAX.Q[,"Adjusted Close"]
    CAC <- CAC.Q[,"Adjusted Close"]
    KOSPI <- KOSPI.Q[,"Adjusted Close"]
    NIKKEI <- NIKKEI.Q[,"Close Price"]
    HANG_SENG <- HANG_SENG.Q[,"Adjusted Close"]

    # The xts merge(.) function will only accept two series at a time.
    # We can, however, merge multiple columns by downcasting to *zoo* objects.
    # Remark:  "all = FALSE" uses an inner join to merge the data.
    z <- merge(as.zoo(SP500), as.zoo(RUSS2000), as.zoo(DAX), as.zoo(CAC), 
               as.zoo(KOSPI), as.zoo(NIKKEI), as.zoo(HANG_SENG), all = FALSE)   

    # Set the column names; these will be used in the heat maps:
    myColnames <- c("SP500","RUSS2000","DAX","CAC","KOSPI","NIKKEI","HANG_SENG")
    colnames(z) <- myColnames

    # Cast back to an xts object:
    mktPrices <- as.xts(z)

    # Next, calculate log returns:
    mktRtns <- diff(log(mktPrices), lag = 1)
    head(mktRtns)
    mktRtns <- mktRtns[-1, ]  # Remove resulting NA in the 1st row

        require(gplots)

    generate_heat_map <- function(correlationMatrix, title)
    {

      heatmap.2(x = correlationMatrix,      # the correlation matrix input
                cellnote = correlationMatrix,   # places correlation value in each cell
                main = title,           # heat map title
                symm = TRUE,            # configure diagram as standard correlation matrix
                dendrogram="none",      # do not draw a row dendrogram
                Rowv = FALSE,           # keep ordering consistent
                trace="none",           # turns off trace lines inside the heat map
                density.info="none",        # turns off density plot inside color legend
                notecol="black")        # set font color of cell labels to black

    }



corr1 <- cor(mktRtns) * 100
generate_heat_map(corr1, "Correlations of World Market Returns, Jan 1998 - Present")

Solution

  • You might want the color values to use the full unrounded number, but show a rounded number.

    In that case do this...

    generate_heat_map <- function(correlationMatrix, title)
    {
    
      heatmap.2(x = correlationMatrix,      # the correlation matrix input
                cellnote = round(correlationMatrix, 2),   # places correlation value in each cell
                main = title,           # heat map title
                symm = TRUE,            # configure diagram as standard correlation matrix
                dendrogram="none",      # do not draw a row dendrogram
                Rowv = FALSE,           # keep ordering consistent
                trace="none",           # turns off trace lines inside the heat map
                density.info="none",        # turns off density plot inside color legend
                notecol="black")        # set font color of cell labels to black
    
    }
    

    If you want the colors to match the numbers shown exactly. Leave the existing function alone and change the input...

    corr1 <- round(cor(mktRtns) * 100, 2)
    generate_heat_map(corr1, "Correlations of World Market Returns, Jan 1998 - Present")