简体   繁体   English

R 传单中重叠多边形的标签

[英]label for overlapping polygons in R leaflet

I need to label several overlapping polygons, but only the label of the biggest one is shown.我需要标记几个重叠的多边形,但只显示最大的一个的标签。 However when I tested with some simulated data the labels were shown correctly.但是,当我用一些模拟数据进行测试时,标签显示正确。 I compared the data in two cases carefully but cannot find the difference caused the problem.我仔细比较了两种情况下的数据,但找不到导致问题的差异。

Here is a minimal example of simulated overlapping polygons:这是模拟重叠多边形的最小示例:

library(leaflet)
library(sp)

poly_a <- data.frame(lng = c(0, 0.5, 2, 3),
                     lat = c(0, 4, 4, 0))
poly_b <- data.frame(lng = c(1, 1.5, 1.8),
                     lat = c(2, 3, 2))
pgons = list(
  Polygons(list(Polygon(poly_a)), ID="1"),
  Polygons(list(Polygon(poly_b)), ID="2")
)
poly_dat <- data.frame(name = as.factor(c("a", "b")))
rownames(poly_dat) <- c("1", "2")

spgons = SpatialPolygons(pgons)
spgonsdf = SpatialPolygonsDataFrame(spgons, poly_dat, TRUE)

leaflet() %>% addPolygons(data = spgonsdf, label = ~name
                          #           ,
                          #           highlightOptions = highlightOptions(
                          # color = "red", weight = 2,bringToFront = TRUE)
)

It's working properly:它工作正常:

在此处输入图片说明

在此处输入图片说明

However it didn't work with my data.但是它不适用于我的数据。

https://github.com/rstudio/leaflet/files/1430888/Gabs.zip https://github.com/rstudio/leaflet/files/1430888/Gabs.zip

You can drag the zip into this site and use the i button to see it's correctly labeled您可以将 zip 拖入此站点并使用 i 按钮查看它是否正确标记

在此处输入图片说明 在此处输入图片说明

library(rgdal)

# download Gabs.zip and extract files to Gabs folder
hr_shape_gabs <- readOGR(dsn = 'Gabs', layer = 'Gabs - OU anisotropic')
hr_shape_gabs_pro <- spTransform(hr_shape_gabs, 
                                 CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet(hr_shape_gabs_pro) %>%
  addTiles() %>% 
  addPolygons(weight = 1, label = ~name)

Only the biggest polygon label is shown:仅显示最大的多边形标签:

在此处输入图片说明

在此处输入图片说明

The data in both case are SpatialPolygonsDataFrame, the data slot have proper polygon names.两种情况下的数据都是 SpatialPolygonsDataFrame,数据槽具有正确的多边形名称。

Change the order of polygons in hr_shape_gabs : polygon in position 3 should be the smaller one.更改hr_shape_gabs中多边形的hr_shape_gabs :位置 3 中的多边形应该是较小的。

library(leaflet)
library(sp)
library(rgdal)
hr_shape_gabs <- readOGR(dsn = 'Gabs - OU anisotropic.shp', 
                         layer = 'Gabs - OU anisotropic')

# Change the position of the smaller and wider polygons
# Position 1 = wider polygon, position 3 = smaller polygon
pol3 <- hr_shape_gabs@polygons[[3]]
hr_shape_gabs@polygons[[3]] <- hr_shape_gabs@polygons[[1]]
hr_shape_gabs@polygons[[1]] <- pol3
hr_shape_gabs$name <- rev(hr_shape_gabs$name)

hr_shape_gabs_pro <- spTransform(hr_shape_gabs, 
                                 CRS("+proj=longlat +datum=WGS84 +no_defs"))
leaflet() %>%
  addTiles() %>% 
  addPolygons(data= hr_shape_gabs_pro, weight = 1, label = ~name)

在此处输入图片说明

Here's a scalable solution in sf for many layers, based on this answer .这是基于此答案的sf针对许多层的可扩展解决方案。

The idea is to order the polygons by decreasing size, such that the smallest polygons plot last .这个想法是通过减小大小对多边形进行排序,以便最小的多边形最后绘制

library(sf)
library(dplyr)

# calculate area of spatial polygons sf object
poly_df$area <- st_area(poly_df)
poly_df <- arrange(poly_df, -area)

# view with labels in leaflet to see that small polygons plot on top
leaflet(poly_df) %>% addTiles() %>% addPolygons(label = ~id)

Apologies for the lack of reproducibility.对缺乏可重复性表示歉意。 This is more of a concept answer.这更像是一个概念答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM