使用经/纬坐标创建六角形单元格 [英] create hexagonal cells grid using lat/lon coordinates

查看:99
本文介绍了使用经/纬坐标创建六角形单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用WGS84坐标(即由2个坐标X = Latitude和Y = Longitude定义的像元)创建具有六边形像元的空间网格

I would like to create a spatial grid with hexagonal cells using WGS84 coordinates (ie cells defined by 2 coordinates X=Latitude and Y=Longitude)

所以,这就是我的想法:

So, this is what I was thinkin about :

library(ggplot2);library(hexbin)
X<-seq(-10,20,by=0.1) # create coordinates vectors X and Y
Y<-seq(35,65,by=0.1)
z<-rnorm(301,0.5,1)
df<-as.data.frame(cbind(X,Y,z)) # create data frame with a z value for each cells (X,Y)
pl<-ggplot2(data=mat,aes(x=X,y=Y,z=z))+stat_summury_hex(fun=function(x) sum(x))
plot(pl)

但是这样做并不能满足我的需求.

But doing this does not provide what I wanted.

所以,我的问题是:如何使用经/纬坐标来绘制带有六角形单元的空间网格?

So, my question is : how to do a spatial grid with hexagonal cells using lat/lon coordinates ?

第二个问题:如何创建一个以一个点为中心的网格(它将代表质心,而不是通常的左下角?)

And second question : how to create a grid centered from one point (that would represent the centroid, and not the left bottom corner as usual?)

推荐答案

如果我理解正确,那么您正在寻找expand.grid():

If I understand properly, you're looking for expand.grid():

xy <- expand.grid(X=X,Y=Y)
z<-rnorm(nrow(xy),0.5,1)
df<-as.data.frame(cbind(xy,z)) # create data frame with a z value for each cells (X,Y)
head(df)
pl<-ggplot(data=df,aes(x=X,y=Y,z=z))+stat_summary_hex(fun=function(x) sum(x))
plot(pl)

对于第二个问题,我不确定,但是由于所有六边形的大小均相同,并且需要相同的操作来定心,因此可以通过适当地更改XY来均匀地移动它们.不确定,也许也可以通过参数来完成.

As for the second question, I'm not sure, but since all hexagons are the same size and will require the same operation to center, you can shift them uniformly by changing X and Y appropriately. Perhaps this can also be done via arguments also, not sure.

[] 第二个问题是如何获取十六进制坐标的data.frame.进行了一些挖掘,但这是一个示例:

[] second question was how to get a data.frame of hex coordinates. Took some digging, but here's an example:

library(hexbin)
coords <- hcell2xy( hexbin(x=X,y=Y))
head(coords)
      x        y
1 -10.0 35.00000
2  -9.5 35.86603
3  -8.5 35.86603
4  -9.0 36.73205
5  -8.0 36.73205
6  -7.5 37.59808

hcell2xy()ggplot2调用的关键函数,您可能需要明确指定参数xbins,该参数在ggplot2内部自动确定,但在两种情况下默认都设置为30.

hcell2xy() is the key function called by ggplot2, and you may need to be explicit about specifying the argument xbins, which is determined automatically inside ggplot2, but appears to default to 30 in both cases.

[] 这也是要求z级别的评论的答案.从ggplot2:::hexBin

[] This is an answer to the comment asking for z levels as well. Ripped from ggplot2:::hexBin

hb <- hexbin(x=X,y=Y)
# Convert to data frame
data.frame(
        hcell2xy(hb), 
        count = hb@count, 
        density = hb@count / sum(hb@count, na.rm=TRUE)
)

您可以稍后选择是使用count还是density进行颜色设置,但要警告:这些颜色与输入给ggplot2z变量不同.如果您想基于其他一些统计信息进行总结,那么我建议您也研究一下这些函数的内在特性,以了解事物如何传递.那就是我一直在做的事.

You can choose whether to use count or density for colors later, but warning: those are different from your z variable fed to ggplot2. If you'd like to summarize based on some other statistic, then I suggest you also look into the guts of those functions to see how things are passed around. That's what I've been doing.

这篇关于使用经/纬坐标创建六角形单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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