Drawing a heat map in R based on zipcodes only

I want to draw a heatmap in R, but my data file looks like this

Lat, Long, Zip, Zvalue 

I basically need to interpolate between lat and long values โ€‹โ€‹and paint colors based on zvalue

How can I do this in R

In the end I want to get something like this

enter image description here

+8
r
source share
3 answers

The spatstat package is your friend!

http://www.spatstat.org/spatstat/

If your data is a set of events (for example, โ€œcrime that took place (x, y)โ€ for many (x, y)), you can use the kernel density estimate to create your heat map. Here you can see an example:

https://github.com/drewconway/WikiLeaks_Analysis/blob/master/density.r

in particular line 72.

If your z values โ€‹โ€‹are real values โ€‹โ€‹(this probably applies to reading your question), you can use the spartstat smooth function, which uses the Gaussian kernel to perform interpolation and returns the set of pixels that were generated by interpolating your data.

Alternatively, you can use the akima package (as recommended by spatstat ) to interpolate at the locations you specify. It uses linear or spline interpolation and seems pretty simple (although I have no experience!).

More generally, what you are trying to do is often called โ€œKriging,โ€ and so you get a lot of results if you click google. See gstat and geoR .

Finally (and FTW) you can use Gaussian processes to do the same. This will give you a distribution of the possible interpolations based on your data. The kernlab package implements an implementation, although I have no idea how to use it.

+3
source share

Jeffrey Breen made this type of plot using ggplot2 and zipcode packages: http://jeffreybreen.wordpress.com/2011/01/05/cran-zipcode/

That should make you start.

+3
source share

You can see the ggplot practical research contest winner code here

The author used geom_tile to create a heat map, the name of the graph: Weather in Houston Weather in Houston

+2
source share

All Articles