0

how can I add names to the the center of my polylines? I added a snippet of the code:

Where do I have to put what code, to get the example at the bottom? Text should be always there, no popup or hide or hover.

With thematic map, it would be just tm_text("county") at addPolylines. Thanks in advance!

leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%  
addMarkers(
    lng= 125.781199, lat= 39.039555,
    label="fun",
    labelOptions = labelOptions(noHide = FALSE, direction = "bottom", offset=c(0,5))) %>%

addPolygons(data=data,
            stroke=FALSE,
            smoothFactor = 0.2,
            fillOpacity = 0.8,
            label = lapply(list, HTML),
            color= ~pal(data$data)) %>%
addPolylines(data = county,
             fillColor = "transparent",
             color = "#000000",
             stroke = TRUE,
             weight = 1.5,
             smoothFactor = 0.5,
             group = "county") %>%
addLegend(position = "topright",
          pal = pal, 
          values = data$data,
          title = "",
          opacity = 1,
          labFormat = labelFormat(suffix = " %",
                                  transform = function(x) 100 * x))

enter image description here

Schillerlocke
  • 305
  • 1
  • 14

1 Answers1

1

I don't think you can do it with addPolygons() directly however you can overlay a text only layer. Check out the answer to How to add labels on top of polygons in leaflet which has a fully example.

The summary is to:

  1. Calculate centroids of polygons in a dataframe and add the label text
  2. After you have added the polygons you can add another addLabelOnlyMarkers() using the centroid dataframe
Will Hore-Lacy
  • 131
  • 1
  • 6