rvariablesleafletmarker

Variable marker size feature in leaflet R


Is there a way to change the marker size in a map based on a certain value?

For example, if I were plotting the population of individual cities and the marker was a circle, the circle would be bigger for the more populated cities.

I was wondering if there was a specific feature for this or if I could add a column to the dataset which has the individual marker sizes for each lat/lon I want to plot.

Thanks!


Solution

  • Let's say you have a field in your spatial points data frame (df) called quantity, and you want to make the radius of the marker the size of the square root of the quantity. Then the command would be:

    leaflet(df) %>% addTiles() %>%
      addCircleMarkers(
        radius = ~ sqrt(quantity),
        stroke = FALSE, fillOpacity = 0.5
     )