if (FALSE) {
library(tigris)
options(tigris_use_cache = TRUE)
rds <- roads("TX", "Tarrant")
tr <- tracts("TX", "Tarrant", cb = TRUE)
cty <- counties("TX", cb = TRUE)
maplibre() |>
fit_bounds(rds) |>
add_fill_layer(
id = "Census tracts",
source = tr,
fill_color = "purple",
fill_opacity = 0.6
) |>
add_line_layer(
"Local roads",
source = rds,
line_color = "pink"
) |>
add_layers_control(
position = "top-left",
background_color = "#ffffff",
active_color = "#4a90e2"
)
# With custom labels
maplibre() |>
add_fill_layer(id = "tract-fill", source = tr) |>
add_line_layer(id = "tract-line", source = tr) |>
add_layers_control(
layers = list(
"Census Tracts" = "tract-fill",
"Tract Borders" = "tract-line"
)
)
# Group multiple layers together
maplibre(bounds = cty) |>
add_fill_layer(id = "county-fill", source = cty, fill_opacity = 0.3) |>
add_line_layer(
id = "county-outline",
source = cty,
line_color = "yellow",
line_width = 3
) |>
add_line_layer(
id = "roads-layer",
source = rds,
line_color = "blue"
) |>
add_layers_control(
layers = list(
"Counties" = c("county-fill", "county-outline"),
"Roads" = "roads-layer"
)
)
}
Run the code above in your browser using DataLab