我正在制作一张 R 传单 map ,这张 map 有人口普查区,每个区都有一个相关联的数字。是否可以在 map 上显示标签,无论是滚动时还是使用图层控件单选按钮。

标签已经在形状文件中:

map1<-leaflet()%>%
  addTiles()%>%

addPolygons(data = plotMerge,
          fillColor = ~pal(plotMerge$incomePerCapita),
          color = "#000000", #this is an outline color hoping to
          fillOpacity = 0.8, # add the labels inside this outline
          weight = 0.2,
          popup=popup)%>%
addLegend(pal = pal,
            values  = plotMerge$incomePerCapita,
            position = "bottomright",
            title = "State-wide Income Percentiles",
            labFormat = labelFormat(digits=1))

saveas(map1, "map1.html")
map1

最佳答案

一种方法是在 highlight 中使用 addPolygons 选项,如下所示:

leaflet(df) %>% addTiles() %>%
addPolygons(data=df,weight=.85,
    highlight = highlightOptions(
    weight = 5, color= "#666",
    dashArray = "", fillOpacity = 0.7,
    bringToFront = TRUE),
   label=~df$labelyouwant)

当您将鼠标悬停在多边形上时,标签将出现。

10-08 16:40