Reputation: 11660
I am trying to see if it is at all possible to produce a heat map of stock performance - something similar to the following where the largest value appears in one corner and the smallest in the other.
http://shares.telegraph.co.uk/heatmaps/f_heatmap.php
My data is an xts object that looks like the following:
> 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
I have been reading through the R ggplot2 book but have not figured out how to produce such a map. I've tinkered with various plots but nothing like what I want to achieve. I greatly appreciate the help.
Upvotes: 2
Views: 2050
Reputation: 58825
The data as you gave it is hard to read in. Here is a much easier version:
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")
Given that, this is the best I could come up with. Note that as Alex said, this is not a heatmap. That is because horizontal and vertical positions of the squares are not related to any specific measurements.
First I reshape the data to make it easier to work with; ggplot2 likes data in long rather than wide format.
library("reshape2")
ap <- melt(data=AdjPrices50AvgPercent)
ap <- ap[rev(order(ap$value)),]
ap$variable <- factor(ap$variable, levels=ap$variable)
Then I plot each square in its own facet and put the text in manually. There are a lot of options used to get rid of the coordinate scales (since they have no meaning for you).
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())
This gives:
Upvotes: 6