R传单.将点数据分组为单元格以汇总许多数据点 [英] R Leaflet. Group point data into cells to summarise many data points

查看:48
本文介绍了R传单.将点数据分组为单元格以汇总许多数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早晨,下午或晚上.

我有以下位置数据(从'

太棒了,但是

我想在传单中做同样的事情,但是似乎找不到简单的解决方案.实际上,我有超过5,000,000个数据点.

在将鼠标移到单元格上或使用传单弹出功能时,最好显示该单元格的数据点数.

解决方案

这是我的解决方案.它使用 sf -package和tim的 fast leafgl -package ...

样本数据

 #演示数据set.seed(123)lat<-runif(1000,46.5,48.5)lon<-runif(1000,13,16)pos<-data.frame(lon,lat) 

代码

 库(sf)库(colourvalues)#use leafgl用于快速渲染大量多边形.#devtools :: install_github("r-spatial/leafgl")图书馆(leafgl)图书馆(传单)#创建具有所有点的空间对象pos.sf<-st_as_sf(pos,coords = c("lon","lat"),crs = 4326)#根据pos.sf中点的边界框创建多边形(25x25)的网格pos.grid<-st_make_grid(st_as_sfc(st_bbox(pos.sf)),n = 25)%>%st_cast("POLYGON")%&%;%st_as_sf()#根据#点与网格中多边形的交点pos.grid $ count<-lengths(st_intersects(pos.grid,pos.sf))#根据数量将颜色添加到多边形cols = colour_values_rgb(pos.grid $ count,include_alpha = FALSE)/255#绘制传单传单()%>%addTiles()%&%;%leafgl :: addGlPolygons(data = pos.grid,重量= 1fillColor = cols,fillOpacity = 0.8,弹出=〜计数) 

输出

Morning, afternoon or evening.

I have the following positional data (adjusted from 'Count of sampling points within a grid cell')

# Demo data 
set.seed(123)
#
lat <- runif(1000, 46.5, 48.5)
lon <- runif(1000, 13,16)
#
pos <- data.frame(lon, lat)

Using the following:

ggplot(pos, aes(x = lon, y=lat)) + 
  geom_bin2d(bins = 25) +
  stat_bin_2d(aes(label=stat(count)), bins = 25, position="identity") +
  scale_fill_gradient(low = "white", high = "red")+
  theme_void()

gives:

Awesome, but,

I want to do the exact same in leaflet but cannot seem to find a straightforward solution. In reality I have over 5,000,000 data points.

Preferable when running the mouse over the cell, or using leaflets popup functionality, the number of data points for the cell will be shown.

解决方案

Here is my solution.. it uses the sf-package, as well as tim's amazingly fast leafgl-package...

sample data

# Demo data 
set.seed(123)
lat <- runif(1000, 46.5, 48.5)
lon <- runif(1000, 13,16)
pos <- data.frame(lon, lat)

code

library( sf )
library( colourvalues )
#use leafgl for FAST rendering of large sets of polygons..
#devtools::install_github("r-spatial/leafgl")
library( leafgl )
library( leaflet )

#create a spatial object with all points
pos.sf <- st_as_sf( pos, coords = c("lon","lat"), crs = 4326)
#create e grid of polygons (25x25) based on the boundary-box of the points in pos.sf
pos.grid <- st_make_grid( st_as_sfc( st_bbox( pos.sf ) ), n = 25 ) %>% 
  st_cast( "POLYGON" ) %>% st_as_sf()
#add count of points in each grid-polygon, based on an 
# intersection of points with polygons from the grid
pos.grid$count <- lengths( st_intersects( pos.grid, pos.sf ) )
#add color to polygons based on count
cols = colour_values_rgb(pos.grid$count, include_alpha = FALSE) / 255
#draw leaflet
leaflet() %>% 
  addTiles() %>% 
  leafgl::addGlPolygons( data = pos.grid,
                         weight = 1,
                         fillColor = cols,
                         fillOpacity = 0.8,
                         popup = ~count )

output

这篇关于R传单.将点数据分组为单元格以汇总许多数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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