Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet labels overlap fix - leaflet::addMarkers

Tags:

r

maps

leaflet

I am looking for a fix for overlapping labels when using R function leaflet::addMarkers.

long <- c(147.768, 147.768, 147.768,147.768, 147.768, 147.768)
lat <- c(-36.852, -36.852, -36.852,-36.852, -36.852, -36.852)
label <- c('long label1', 'long label2', 'long label3','long label4', 'long label5', 'long label6')

markers <- data.frame(lat,long,label)


leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup="The birthplace of R",
             label = markers$label,
             labelOptions = labelOptions(noHide = T, direction = 'auto'),
            clusterOptions = markerClusterOptions()
             )
like image 232
Erin Avatar asked Sep 25 '17 15:09

Erin


1 Answers

You can set the noHide = F instead of noHide = T in labelOptions

And you can try adding options = markerOptions(riseOnHover = TRUE) to get the labels on top of the marker.

Final code would be :

leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup="The birthplace of R",
             label = markers$label,
             labelOptions = labelOptions(noHide = F, direction = 'auto'),
             options = markerOptions(riseOnHover = TRUE),
             clusterOptions = markerClusterOptions()
             )
like image 189
ZedzDeD Avatar answered Oct 16 '22 10:10

ZedzDeD