简体   繁体   中英

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

You can drag the zip into this site and use the i button to see it's correctly labeled

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

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.

Change the order of polygons in hr_shape_gabs : polygon in position 3 should be the smaller one.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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