Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a continuous heat map in R

I have a series of x and y coordinates that each have a distance attached to them. I would like to create a heat map that displays the average distance for every point within the x and y ranges as a heat map. Since the points are not spaced evenly from each other in a lattice-like shape, the method would require some kind of smoothing function that clusters data and calculates the average for each point the vicinity and then representing that average with a color.

So far, using ggplot2, I can only find methods like stat_density2d and geom_tile, which only work for displaying point density and representing evenly spaced points (as far as I can tell).

Ideally it would follow the same principle as this image: enter image description here

in which colors were assigned based on the given points in the vicinity even though the density and placement of the points was not uniform.

I do not want to create a heat map in matrix form like this image: enter image description here

in which a table is color-coded. Instead, I would like to create a continuous heat map using non-uniformly distributed x and y coordinates that, in effect, displays the limit in which the data is broken into infinitely many rectangles. This may not be the actual method used by the function, but it provides a general idea as to what I'm looking for.

Here is some sample data:

data=data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
  y=c(2,4,5,1,3,8,4,8,1,1,6,9),
  distance=c(66,84,93,76,104,29,70,19,60,50,46,36))

How can I make a heat map with distance as the color scale that covers the entire range of numbers, like the plot in the first link provided?

Any help is greatly appreciated!

like image 967
user1532996 Avatar asked Jul 17 '12 21:07

user1532996


People also ask

How to create a heatmap in R?

The most common function for creating heatmaps in R is the heatmap () function, which is already provided by the base installation of R. The heatmap function is applied as shown below:

How do you make a heat map in Excel?

The heatmap function. The heatmap function has the form of heatmap (x, scale, na.rm, col, labRow, labCol, main) and it produces a heat map of the data. x is the numeric matrix containing the values being used in creating the heat map. col is the color palette to be used by the heat map.

How to create the heatmap in ggplot2?

We can use the following code to create the heatmap in ggplot2: library (ggplot2) ggplot (melt_mtcars, aes (variable, car)) + geom_tile (aes (fill = value), colour = "white") + scale_fill_gradient (low = "white", high = "red")

What is an example of a heat map?

A good example of this would be a heat map of the wifi in your home. Such a heat map would show you where the best places for your devices are. The heatmap function. The heatmap function has the form of heatmap (x, scale, na.rm, col, labRow, labCol, main) and it produces a heat map of the data.


2 Answers

In order to generate a continuous map with irregularly-spaced coordinates you need first to intrapolate a regular grid (here using function interp of package akima):

require(akima)
data <- data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
                   y=c(2,4,5,1,3,8,4,8,1,1,6,9),
                   distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
resolution <- 0.1 # you can increase the resolution by decreasing this number (warning: the resulting dataframe size increase very quickly)
a <- interp(x=data$x, y=data$y, z=data$distance, 
            xo=seq(min(data$x),max(data$x),by=resolution), 
            yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")
image(a) #you can of course modify the color palette and the color categories. See ?image for more explanation

enter image description here

Or you can use, for the plotting itself, function filled.contour:

filled.contour(a, color.palette=heat.colors)

enter image description here

like image 150
plannapus Avatar answered Sep 28 '22 04:09

plannapus


There is a user-written function here that produces heatmaps using ggplot2:

http://www.r-bloggers.com/ggheat-a-ggplot2-style-heatmap-function/

And their example image:

example

If what you want is a topo map as in your example, there are plenty of tools for that (just search under "topo map".

And finally, there's the isarithmic map, which just goes to show that you need to make clear exactly what you want done if you want some smoothing incorporated: http://dsparks.wordpress.com/2011/10/24/isarithmic-maps-of-public-opinion-data/

like image 24
Ari B. Friedman Avatar answered Sep 28 '22 04:09

Ari B. Friedman