R,ggplot2,热图

时间:2011-12-11 05:04:29

标签: r ggplot2 heatmap

我试图看看是否有可能产生股票表现的热图 - 类似于以下内容,其中最大值出现在一个角落而最小值出现在另一个角落。

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 个答案:

答案 0 :(得分: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")

鉴于此,这是我能想到的最好的。请注意,正如亚历克斯所说,这不是热图。这是因为正方形的水平和垂直位置与任何特定测量无关。

首先,我重新整理数据,以便更轻松地使用; 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())

这给出了: