R 传单。将点数据分组到单元格中以汇总许多数据点
R Leaflet. Group point data into cells to summarise many data points
上午、下午或晚上。
我有以下位置数据(根据“”调整)
# 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()
给出:
很棒,但是,
我想在传单中做同样的事情,但似乎找不到直接的解决方案。实际上我有超过 5,000,000 个数据点。
最好在 运行 将鼠标悬停在单元格上时,或使用传单弹出功能时,将显示单元格的数据点数。
这是我的解决方案..它使用 sf
-package,以及 tim 惊人的 fast leafgl
-package...
示例数据
# 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 )
输出
上午、下午或晚上。
我有以下位置数据(根据“
# 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()
给出:
很棒,但是,
我想在传单中做同样的事情,但似乎找不到直接的解决方案。实际上我有超过 5,000,000 个数据点。
最好在 运行 将鼠标悬停在单元格上时,或使用传单弹出功能时,将显示单元格的数据点数。
这是我的解决方案..它使用 sf
-package,以及 tim 惊人的 fast leafgl
-package...
示例数据
# 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 )
输出