if (FALSE) {
# Basic categorical legend
add_legend(map, "Population",
values = c("Low", "Medium", "High"),
colors = c("blue", "yellow", "red"),
type = "categorical")
# Continuous legend with custom styling
add_legend(map, "Income",
values = c(0, 50000, 100000),
colors = c("blue", "yellow", "red"),
type = "continuous",
style = list(
background_color = "white",
background_opacity = 0.9,
border_width = 2,
border_color = "navy",
text_color = "darkblue",
font_family = "Times New Roman",
title_font_weight = "bold"
))
# Legend with custom styling using a list
add_legend(map, "Temperature",
values = c(0, 50, 100),
colors = c("blue", "yellow", "red"),
type = "continuous",
style = list(
background_color = "#f0f0f0",
title_size = 16,
text_size = 12,
shadow = TRUE,
shadow_color = "rgba(0,0,0,0.1)",
shadow_size = 8
))
# Dark legend with white element borders
add_legend(map, "Elevation",
values = c(0, 1000, 2000, 3000),
colors = c("#2c7bb6", "#abd9e9", "#fdae61", "#d7191c"),
type = "continuous",
style = list(
background_color = "#2c3e50",
text_color = "white",
title_color = "white",
element_border_color = "white",
element_border_width = 1
))
# Categorical legend with circular patches
add_categorical_legend(
map = map,
legend_title = "Population",
values = c("Low", "Medium", "High"),
colors = c("#FED976", "#FEB24C", "#FD8D3C"),
patch_shape = "circle",
sizes = c(10, 15, 20),
style = list(
background_opacity = 0.95,
border_width = 1,
border_color = "gray",
title_color = "navy",
element_border_color = "black",
element_border_width = 1
)
)
# Legend with line patches for line layers
add_categorical_legend(
map = map,
legend_title = "Road Type",
values = c("Highway", "Primary", "Secondary"),
colors = c("#000000", "#333333", "#666666"),
patch_shape = "line",
sizes = c(5, 3, 1) # Line thickness in pixels
)
# Legend with hexagon patches (e.g., for H3 data)
add_categorical_legend(
map = map,
legend_title = "H3 Hexagon Categories",
values = c("Urban", "Suburban", "Rural"),
colors = c("#8B0000", "#FF6347", "#90EE90"),
patch_shape = "hexagon",
sizes = 25
)
# Custom SVG shapes - star
add_categorical_legend(
map = map,
legend_title = "Ratings",
values = c("5 Star", "4 Star", "3 Star"),
colors = c("#FFD700", "#FFA500", "#FF6347"),
patch_shape = paste0('')
)
# Using sf objects directly as patch shapes
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
county_shape <- nc[1, ] # Get first county
add_categorical_legend(
map = map,
legend_title = "County Types",
values = c("Rural", "Urban", "Suburban"),
colors = c("#228B22", "#8B0000", "#FFD700"),
patch_shape = county_shape # sf object automatically converted to SVG
)
# For advanced users needing custom conversion options
custom_svg <- mapgl:::.sf_to_svg(county_shape, simplify = TRUE,
tolerance = 0.001, fit_viewbox = TRUE)
add_categorical_legend(
map = map,
legend_title = "Custom Converted Shape",
values = c("Type A"),
colors = c("#4169E1"),
patch_shape = custom_svg
)
}
Run the code above in your browser using DataLab