[R-sig-Geo] Adding a text-only label to a leaflet map?

Kevin Zembower kev|n @end|ng |rom zembower@org
Thu Jun 1 21:29:05 CEST 2023


Hello, all,

With Ben and Olivier's help, I was able to plot my neighborhood's census 
blocks and tracts on an OSM base map, with leaflet, without warnings.

My next problem is labeling the polygons I've plotted. Here's an example 
of what I've tried so far:

## Reproducible example
library(tidycensus)
library(tidyverse)
library(sf)
library(tigris)
options(tigris_use_cache = TRUE)
library(leaflet)
library(leafem)

rw_tracts <- tracts(state = "MD",
                     county = "Baltimore city",
                     year = "2020") %>%
     filter(TRACTCE == "271101") %>%
     st_transform('+proj=longlat +datum=WGS84')

lopot = labelOptions(noHide = TRUE,
                      direction = 'top',
                      textOnly = TRUE)

rw_tracts %>%
     leaflet() %>%
     addTiles() %>%
     addPolygons(color = "#00E600") %>%
     ## addAwesomeMarkers(lng = -76.62, lat = 39.352,
     ##                   label = "2711.01",
     ##                   labelOptions(
     ##                       noHide = TRUE,
     ##                       direction = 'top',
     ##                       textOnly = TRUE,
     ##                       opacity = 1,
     ##                       textsize = "60px",
     ##                       style = list(
     ##                           "color" = "#FFFFFF", ## "#00E600",
     ##                           "font-weight" = "bold")
     ##                   ),
     ##                   options = markerOptions(
     ##                       ## interactive = FALSE,
     ##                       ## clickable = FALSE,
     ##                       ## draggable = FALSE,
     ##                       ## keyboard = FALSE,
     ##                   ),
     ## ) %>%
     addStaticLabels(
         lng = -76.63, lat = 39.36,
         label = "2711.01",
         style = list("color" = "#009900",
                      "font-weight" = "bold",
                      "font-size" = "60px")
     ) %>%
     addLabelOnlyMarkers(
         label = "2711.01",
         lng = -76.613, lat = 39.348,
         labelOptions(noHide = TRUE,
                      direction = 'top',
                      textOnly = TRUE,
                      opacity = 1,
                      textsize = "60px",
                      style = list("color" = "#FFFFFF", ## "#00E600",
                                   "font-weight" = "bold")
                      )
     )

So far, the addStaticLabels() example comes closest to what I want. 
However, I need to move the label to a different location, and it 
doesn't seem to respond to the lng and lat options (but doesn't throw an 
error, either). Also, I'm concerned that the leafem package comes with a 
warning that it's based on deprecated packages.

addAwesomeMarkers() put the marker when I wanted it, and my label 
appeared when I clicked or moused over it, but I just want a static label.

addLabelOnlyMarker() seems designed for my use-case, but I can't get 
anything to appear. Not sure what I'm doing wrong there.

Finally, all these methods seem like a lot of work. Am I overlooking 
something simple? I just need static text, that I can control the 
location, size and color of. Is this the way to do it?

Thanks, again, for your advice and guidance.

-Kevin



More information about the R-sig-Geo mailing list