I have a data frame with three columns and I’d like to make a image/heatmap of the data.
The three columns are pe, vix, and ret with pe and vix being x and y and ret being z.
There are 220 lines in the data frame so i’d like to bin the data if possible, the ranges are below.
Any suggestions for how to bin the x and y data and also create a matrix for use in an image()?
> range(matr$pe)
[1] 13.32 44.20
> range(matr$vix)
[1] 10.42 59.89
> range(matr$ret)
[1] -0.09274936 0.04693118
> class(matr)
[1] "data.frame"
> head(matr)
pe vix ret
1 20.86 13.16 -0.002931561
2 20.46 12.53 -0.003546889
3 20.52 12.42 0.006339165
4 20.61 13.47 0.009683174
5 20.57 11.26 -0.002666668
6 20.81 11.73 0.002895003
Here’s what I ended up doing. I used the interp() function in the akima package to create the appropriately binned matrix object. It seems to do the work of binning and ‘matricizing’ of the data frame. On a side note, in order to make the heatmap WITH a legend, I ended up using the image.plot() method from the fields package. Here’s the
code:
and resulting product for anyone interested:

Thanks to everyone for their help and suggestions.