Variable marker size feature in leaflet R
Asked Answered
F

2

11

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!

Frants answered 19/10, 2016 at 16:44 Comment(2)
In the help for addCircleMarkers for argument radius it says: a numeric vector of radii for the circles; it can also be a one-sided formula, in which case the radius values are derived from the data (units in meters for circles, and pixels for circle markers). So, something like addCircleMarkers(..., radius = ~ data$variable) should produce what you want.Melvin
Thanks! I'm new to R and didn't realize how helpful the help within R isFrants
W
14

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
 )
Wellchosen answered 28/12, 2017 at 19:56 Comment(0)
S
2

You can also use the rescale function if dividing the values does not work well:

leaflet(df) %>% addTiles() %>%
  addCircleMarkers(
    radius = ~ rescale(quantity, c(1,10)),
    stroke = FALSE, fillOpacity = 0.5
 )
Schellens answered 22/11, 2022 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.