R、 ggplot2,热图

R、 ggplot2,热图,r,ggplot2,heatmap,R,Ggplot2,Heatmap,我试图看看是否有可能生成股票表现的热图——类似于下面的情况,最大值出现在一个角落,最小值出现在另一个角落 我的数据是一个xts对象,如下所示: > AdjPrices50AvgPercent SPY.Adjusted IWM.Adjusted DIA.Adjusted XLI.Adjusted XLB.Adjusted 2011-12-09 3.12 4.61 4.39 4.49 2.3

我试图看看是否有可能生成股票表现的热图——类似于下面的情况,最大值出现在一个角落,最小值出现在另一个角落

我的数据是一个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

我一直在阅读ggplot2这本书,但还没有弄明白如何制作这样的地图。我修改了各种情节,但没有达到我想要的效果。非常感谢您的帮助。

您提供的数据很难读入。以下是一个简单得多的版本:

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")
这使得:


您的示例没有显示任何热图。不过,你可以从这里开始:非常感谢。这正是我需要的。我将发布一篇后续文章,详细介绍我为更好地适应我的目的所做的一些修改。非常感谢。
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())