Rick Arko Rick Arko - 2 months ago 44
R Question

How to specify radius units in addCircleMarkers() when using Leaflet in R

I'm trying to visualize a shipment from origin to destination using a leaflet map created with R within a shiny application.

I want to add a circle marker of a radius corresponding to a the odist and ddist variables which come form a reactive dataframe called main()

below is a relevant snapshot with accompanying code:


output$leaflet1 <- renderLeaflet({
leaflet() %>%
addCircleMarkers(data = main(), lng = main()$Olong, lat = main()$Olat, color = 'black', fillColor = coyGreen,
radius = main()$odist, opacity = .5) %>%
addCircleMarkers(data = main(), lng = main()$Dlong, lat = main()$Dlat, color = 'black', fillColor = coyGreen,
radius = main()$ddist, opacity = .3)

For the above example the argument
radius = main()$odist
is equivalent to
radius = 50
. However, the 50 units seem to be arbitrary (the cirlce is smaller than the larger one with
radius = main()$ddist = 125
however both circles enlarge and shrink as I zoom in and out). I would like to be able to set the radius of my circle marker to be a fixed radius in miles, however I haven't been able to figure out how to do so. Any help is greatly appreciated!


If you use addCircles instead of addCircleMarkers your circles will have constant radius (in meters). Here's a reproducible example using mapview which uses addCircleMarkers. On top we plot the same locations using addCircles


m <- mapview(breweries91) # uses addCirclemarkers so circle radius changes with zoom

m@map %>% 
  addCircles(data = breweries91, color = "red", radius = 100) # circle radius constant

If you zoom in you will see that the initially smaller red circles will become bigger in relation to the standard blue circlemarkers used in mapview