仅根据邮政编码在 R 中绘制热图
我想在 R 中绘制热图,但我的数据文件是这样的
Lat, Long, Zip, Zvalue
我基本上需要在纬度和经度值之间进行插值,并根据 z值绘制颜色
我如何在 R 中做到这一点
我最终想要得到这样的东西
I want to draw a heatmap in R but my datafile is like this
Lat, Long, Zip, Zvalue
I basically need to interpolate between lat and long values and draw colors based on the zvalue
How can I do that in R
I eventually want to get something like this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
spatstat
包是你的朋友!http://www.spatstat.org/spatstat/
如果您的数据是一组事件(例如对于许多 (x,y)),“犯罪发生在 (x,y)”,那么您可以使用核密度估计来生成热图。您可以在此处查看示例:
https://github.com/drewconway/WikiLeaks_Analysis/ blob/master/密度.r
特别是第 72 行。
如果您的 z 值是真实值(这可能是阅读您的问题的情况),那么您可以使用 spatstat 的
smooth
函数,该函数使用高斯内核来执行插值并返回一组通过插值数据生成的像素。或者,您可以使用
akima
包(按照spatstat
作者的建议)在您指定的位置进行插值。它使用线性或基于样条的插值,看起来非常简单(尽管我没有这方面的经验!)。更一般地说,你想要做的事情通常被称为“克里金法”,所以如果你点击谷歌搜索,你会得到很多结果。例如,请参阅
gstat
和geoR
软件包。最后(和 FTW)您可以使用高斯过程来做同样的事情。这实际上将为您提供给定数据的可能插值的分布。 R 包
kernlab
有一个实现,但我不知道如何使用它。The package
spatstat
is your friend!http://www.spatstat.org/spatstat/
If your data is a set of events (like "a crime occurred at (x,y)" for many (x,y)), then you can use a kernel density estimate to generate your heat map. You can see an example here:
https://github.com/drewconway/WikiLeaks_Analysis/blob/master/density.r
specifically line 72.
If your z values are real values (which is probably the case reading your question) then you can use spatstat's
smooth
function which uses a Gaussian kernel to perform the interpolation and get back a set of pixels which have been generated by interpolating your data.Alternatively, you can use the
akima
package (as recommended by thespatstat
authors) for interpolation at locations you specify. It uses either linear or spline-based interpolation and seems pretty straightforward (though I've no experience with it!).More generally what you're trying to do is often called "Kriging", and so you get lots of results if you hit up google for that. See e.g. the
gstat
andgeoR
packages.Finally (and FTW) you can use Gaussian Processes to do the same sort of thing. This will actually give you a distribution over possible interpolations given your data. The R package
kernlab
has an implementation though I have no idea how to use it.Jeffrey Breen 使用 ggplot2 和 zipcode 包完成了这种类型的绘图: http: //jeffreybreen.wordpress.com/2011/01/05/cran-zipcode/
这应该可以帮助您开始
Jeffrey Breen has done this type of plot using the ggplot2 and the zipcode packages: http://jeffreybreen.wordpress.com/2011/01/05/cran-zipcode/
That should get you started
您可以查看 ggplot 案例研究竞赛获胜者的代码 这里
作者使用geom_tile制作了一个热图,图的标题是:休斯顿市中心暴力犯罪天气图
You can look at the code of the ggplot case studies competition winner here
The author used geom_tile to make a heatmap, the title of the graph is : Violent Crime Weather Map of Downtown Houston