2016-05-20 8 views
0

このサイトからこのコードを取得して、相関行列ヒートマップを作成しました。どのように私は価値があるだけで小数点以下2桁を持つようにヒートマップ内の数値をフォーマットするか?: http://blog.revolutionanalytics.com/2014/08/quantitative-finance-applications-in-r-8.htmlヒートマップ2で数値をフォーマットする方法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

'round()'を見ましたか? – cory

+0

コード内の様々な場所に置いた試行錯誤がまだありません – Rhodo

+0

最後の2行目はどうですか?これは、ヒートマップへの入力として相関行列を生成するラインです。 – cory

答えて

0

あなたは色の値がフル丸められていない番号を使用したいのですが、丸みを帯びた番号が表示される場合があります。その場合

はこれを行う...

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

Ugh- How Embarasking!(Popeye)ありがとう。それは早かったし、脳の鼓腸があった – Rhodo

関連する問題