在R中创建连续的热图 [英] Creating a continuous heat map in R

查看:1394
本文介绍了在R中创建连续的热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列x和y坐标,每个坐标都有一个距离。我想创建一个热图,显示x和y范围内每个点的平均距离作为热图。由于点不是以格子状形式彼此均匀地分开,所以该方法将需要某种平滑函数,其聚集数据并计算每个点附近的平均值,然后用颜色表示该平均值。



到目前为止,使用 ggplot2 ,我只能找到方法,如 stat_density2d geom_tile ,这只能用于显示点密度和表示均匀间隔的点(据我所知)。



理想情况下,它将遵循与此图像相同的原则:



根据附近的给定点分配颜色,即使点的密度和位置不均匀。



我不想像这样的图像以矩阵形式创建热图:



其中表格为彩色编码。相反,我想使用非均匀分布的x和y坐标创建一个连续的热图,实际上,它显示了将数据分解成无穷多个矩形的极限。这可能不是函数使用的实际方法,但它提供了我正在寻找的一般概念。



以下是一些示例数据: p>

  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))

如何我可以制作一个距离的热图,作为涵盖整个数字范围的颜色比例,如第一个链接中的情节?



任何帮助都非常感谢!

解决方案

为了生成具有不规则间隔坐标的连续地图,您需要先插入一个规则网格(这里使用函数 interp akima ):

 code> 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,1 04,29,70,19,60,50,46,36))
分辨率< - 0.1#您可以通过减少此数字来提高分辨率(警告:结果数据帧大小非常快速地增加)$ b $ (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)#你当然可以修改颜色调色板和颜色类别。查看?图像以获得更多的解释



或者您可以使用,对于绘图本身,函数 filled.contour

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

>


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:

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:

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!

解决方案

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

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

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

这篇关于在R中创建连续的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆