2011-12-11 143 views
2

我想知道是否完全有可能生成股票表現的熱圖 - 類似於以下內容,其中最大值出現在一個角落,最小值出現在另一個角落。R,ggplot2,熱圖

http://shares.telegraph.co.uk/heatmaps/f_heatmap.php

我的數據是XTS對象,看起來像以下:

> AdjPrices50AvgPercent 
      SPY.Adjusted IWM.Adjusted DIA.Adjusted XLI.Adjusted XLB.Adjusted 
2011-12-09   3.12   4.61   4.39   4.49   2.32 
      XLF.Adjusted XLE.Adjusted XOP.Adjusted OIH.Adjusted XLY.Adjusted 
2011-12-09   2.84   3.8   5.45   0.45   3.1 
      XLP.Adjusted XLV.Adjusted XLU.Adjusted SMH.Adjusted QQQ.Adjusted 
2011-12-09   3.41   2.63   1.86   1.99   1.2 
      XHB.Adjusted PPH.Adjusted XME.Adjusted GDX.Adjusted GLD.Adjusted 
2011-12-09   9.46   4.41   3.73  -0.02   0.15 
      SLV.Adjusted USO.Adjusted MOO.Adjusted KRE.Adjusted KBE.Adjusted 
2011-12-09  -1.24   7.46   0.11   5.78   2.84 
      XRT.Adjusted VNQ.Adjusted JNK.Adjusted HYG.Adjusted LQD.Adjusted 
2011-12-09   4.32   3.12   2.08   2.35  -0.35 
      TLT.Adjusted TIP.Adjusted IEF.Adjusted VXX.Adjusted 
2011-12-09  -0.27   0.25   0.45  -9.27 

我一直在讀通過R GGPLOT2書,但還沒有想出如何產生這樣的地圖。我對各種情節進行了修飾,但沒有像我想要的那樣。我非常感謝幫助。

+1

您的例子沒有顯示任何熱圖。不過你可以從這裏開始:http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/ –

回答

6

,你給了它是很難在讀這是一個容易得多的版本中的數據:

AdjPrices50AvgPercent <- 
structure(list(SPY.Adjusted = 3.12, IWM.Adjusted = 4.61, DIA.Adjusted = 4.39, 
    XLI.Adjusted = 4.49, XLB.Adjusted = 2.32, XLF.Adjusted = 2.84, 
    XLE.Adjusted = 3.8, XOP.Adjusted = 5.45, OIH.Adjusted = 0.45, 
    XLY.Adjusted = 3.1, XLP.Adjusted = 3.41, XLV.Adjusted = 2.63, 
    XLU.Adjusted = 1.86, SMH.Adjusted = 1.99, QQQ.Adjusted = 1.2, 
    XHB.Adjusted = 9.46, PPH.Adjusted = 4.41, XME.Adjusted = 3.73, 
    GDX.Adjusted = -0.02, GLD.Adjusted = 0.15, SLV.Adjusted = -1.24, 
    USO.Adjusted = 7.46, MOO.Adjusted = 0.11, KRE.Adjusted = 5.78, 
    KBE.Adjusted = 2.84, XRT.Adjusted = 4.32, VNQ.Adjusted = 3.12, 
    JNK.Adjusted = 2.08, HYG.Adjusted = 2.35, LQD.Adjusted = -0.35, 
    TLT.Adjusted = -0.27, TIP.Adjusted = 0.25, IEF.Adjusted = 0.45, 
    VXX.Adjusted = -9.27), .Names = c("SPY.Adjusted", "IWM.Adjusted", 
"DIA.Adjusted", "XLI.Adjusted", "XLB.Adjusted", "XLF.Adjusted", 
"XLE.Adjusted", "XOP.Adjusted", "OIH.Adjusted", "XLY.Adjusted", 
"XLP.Adjusted", "XLV.Adjusted", "XLU.Adjusted", "SMH.Adjusted", 
"QQQ.Adjusted", "XHB.Adjusted", "PPH.Adjusted", "XME.Adjusted", 
"GDX.Adjusted", "GLD.Adjusted", "SLV.Adjusted", "USO.Adjusted", 
"MOO.Adjusted", "KRE.Adjusted", "KBE.Adjusted", "XRT.Adjusted", 
"VNQ.Adjusted", "JNK.Adjusted", "HYG.Adjusted", "LQD.Adjusted", 
"TLT.Adjusted", "TIP.Adjusted", "IEF.Adjusted", "VXX.Adjusted" 
), class = "data.frame", row.names = "2011-12-09") 

鑑於這種情況,這是我能拿出最好的。請注意,正如Alex所說,這不是熱圖。這是因爲正方形的水平和垂直位置與任何特定測量都沒有關係。

首先我重塑數據以使其更易於使用; ggplot2喜歡長數據而不是寬數據格式。

library("reshape2") 
ap <- melt(data=AdjPrices50AvgPercent) 
ap <- ap[rev(order(ap$value)),] 
ap$variable <- factor(ap$variable, levels=ap$variable) 

然後我在它自己的方面繪製每個方塊並手動放置文本。有很多選項用於擺脫座標尺度(因爲它們對你沒有意義)。

ggplot(ap) + 
    geom_rect(aes(xmin=0, xmax=1, ymin=0, ymax=1, fill=value)) + 
    geom_text(aes(label=variable), x=0.5, y=0.6, size=3) + 
    geom_text(aes(label=paste(value,"%",sep="")), x=0.5, y=0.4, size=3) + 
    scale_x_continuous(expand=c(0,0)) + 
    scale_y_continuous(expand=c(0,0)) + 
    scale_fill_gradient2(low="blue", mid="green", high="red", 
         limits=c(-1,1)*max(abs(ap$value)), breaks=(-9):9) + 
    coord_equal() + 
    facet_wrap(~variable) + 
    opts(axis.text.x = theme_blank(), 
     axis.text.y = theme_blank(), 
     axis.title.x = theme_blank(), 
     axis.title.y = theme_blank(), 
     axis.ticks = theme_blank(), 
     axis.ticks.margin = unit(0, "mm"), 
     strip.background = theme_blank(), 
     strip.text.x = theme_blank(), 
     panel.margin = unit(0, "mm"), 
     panel.background = theme_blank()) 

這給:

+0

非常感謝。這正是我需要的。我會發布一些後續細節,介紹爲了更好地適合我的目的所做的一些修改。謝謝。 – codingknob