2016-05-20 5 views
0

Ich habe diesen Code von dieser Website genommen, um eine Korrelationsmatrix Heatmap zu erstellen. Wie formatiere ich die Zahlen in der Heatmap im Wert von nur 2 Dezimalstellen haben ?: http://blog.revolutionanalytics.com/2014/08/quantitative-finance-applications-in-r-8.htmlSo formatieren Sie Zahlen in Heatmap.2 in R

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") 
+0

Haben Sie 'round()' noch angeschaut? – cory

+0

Ich habe aber den Versuch und Irrtum, es an verschiedenen Stellen in den Code hat noch nicht funktioniert – Rhodo

+0

Wie wäre es mit der vorletzten Zeile? Das ist die Linie, die eine Korrelationsmatrix als Eingabe für die Heatmap generiert ... – cory

Antwort

0

Sie können die Farbwerte die volle unrounded Nummer, verwenden möchten, aber eine abgerundete Zahl zeigen.

In diesem Fall tun dies ...

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 

} 

Wenn Sie die Farben wollen genau angezeigt, die Zahlen entsprechen. Belassen Sie die vorhandene Funktion und ändern Sie den Eingang ...

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

Ugh- How Embarasking! (Popeye) Danke. Es war früh und ich hatte Blähungen im Gehirn – Rhodo

Verwandte Themen