如何在 R 中格式化 Heatmap.2 中的数字

How to Format Numbers in Heatmap.2 in R

我从该站点获取此代码来制作相关矩阵热图。如何将热图中的数字格式化为只有 2 位小数?: 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")

您可能希望颜色值使用完整的未四舍五入的数字,但显示四舍五入的数字。

在那种情况下这样做...

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

}

如果您希望颜色与显示的数字完全匹配。保留现有功能并更改输入...

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