gpt4 book ai didi

R 传单。将点数据分组到单元格中以汇总许多数据点

转载 作者:行者123 更新时间:2023-12-03 19:09:46 29 4
gpt4 key购买 nike

早上、下午或晚上。
我有以下位置数据(从'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)
使用以下内容:
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()
给出:
enter image description here
厉害,但是
我想在传单中做同样的事情,但似乎无法找到一个简单的解决方案。实际上,我有超过 5,000,000 个数据点。
最好在将鼠标悬停在单元格上或使用传单弹出功能时,将显示单元格的数据点数。

最佳答案

这是我的解决方案..它使用 sf -package,以及 tim 的惊人快速 leafgl -包裹...
样本数据

# Demo data 
set.seed(123)
lat <- runif(1000, 46.5, 48.5)
lon <- runif(1000, 13,16)
pos <- data.frame(lon, lat)
代码
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 )
输出
enter image description here

关于R 传单。将点数据分组到单元格中以汇总许多数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62548277/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com