fmt <- function(v) formatC(v, format = "d", big.mark = ",")
# classes (manual breaks)
brks <- c(0, 100, 250, 1000, 5000, Inf)
rad <- c(4, 7, 11, 16, 23) # px, one per class
labs <- c("1–100", "101–250", "251–1,000", "1,001–5,000", "> 5,000")
pl$radius <- rad[cut(pl$n_reviews, brks, labels = FALSE, include.lowest = TRUE)]
pl <- pl[order(-pl$n_reviews), ] # large first, small on top
# fill by type
cols <- c(Park = "#2E7D32", Playground = "#EF8A17")
# popup and hover label
pl$reg <- ifelse(pl$region == "inside_brussels", "Brussels-Capital Region", "Outside Brussels-Capital Region")
pl$popup <- sprintf(
"<b>%s</b><br>%s<br>%s<br>%s<br>Reviews: <b>%s</b> (%s with text)<br>%s–%s<br><a href='https://www.google.com/maps/place/?q=place_id:%s' target='_blank'>Open in Google Maps</a>",
htmlEscape(pl$ParkName), ifelse(is.na(pl$muni) | pl$muni == "", "—", htmlEscape(pl$muni)), pl$reg, pl$type,
fmt(pl$n_reviews), fmt(pl$n_with_text), pl$first, pl$last, pl$GoogleID
)
pl$label <- paste0(pl$ParkName, " (", fmt(pl$n_reviews), ")")
# size legend
size_legend <- function(r, labs, title) {
d <- 2 * max(r) + 2
rows <- paste0(
"<div style='display:flex;align-items:center;margin:3px 0'>",
"<span style='width:", d, "px;display:flex;justify-content:center'>",
"<span style='width:", 2 * r, "px;height:", 2 * r,
"px;border-radius:50%;border:1px solid #000;background:rgba(128,128,128,.35)'></span></span>",
"<span style='margin-left:6px'>", labs, "</span></div>",
collapse = ""
)
HTML(paste0("<b>", title, "</b>", rows))
}
leaflet(pl, width = "100%", height = "85vh") |>
addTiles("https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}", # Google roadmap
attribution = "Map data © Google", group = "Google roadmap") |>
addTiles("https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}", # Google hybrid
attribution = "Imagery © Google", group = "Google hybrid") |>
setView(lng = 4.37, lat = 50.84, zoom = 12) |>
addCircleMarkers(
lng = ~lon, lat = ~lat, radius = ~radius, group = ~type,
color = "#000", weight = 1, opacity = 0.9,
fillColor = ~unname(cols[type]), fillOpacity = 0.55,
popup = ~popup, label = ~label,
labelOptions = labelOptions(direction = "top", textsize = "12px")
) |>
addControl(size_legend(rad, labs, "Google reviews"), position = "bottomright") |>
addLegend("bottomright", colors = unname(cols), labels = names(cols), opacity = 0.7, title = "Type") |>
addLayersControl(baseGroups = c("Google roadmap", "Google hybrid"), overlayGroups = names(cols),
options = layersControlOptions(collapsed = FALSE)) |>
addScaleBar("bottomleft", options = scaleBarOptions(imperial = FALSE))