These functions create step expressions using different classification methods, similar to choropleth mapping in GIS software. They automatically calculate break points and generate appropriate step expressions for styling map layers.
Usage
step_equal_interval(
column,
data_values,
n = 5,
colors = NULL,
na_color = "grey"
)
step_quantile(column, data_values, n = 5, colors = NULL, na_color = "grey")
step_jenks(column, data_values, n = 5, colors = NULL, na_color = "grey")
Arguments
- column
The name of the column to use for the step expression.
- data_values
A numeric vector of the actual data values used to calculate breaks.
- n
The number of classes/intervals to create. Defaults to 5.
- colors
A vector of colors to use. If NULL, uses viridisLite::viridis(n).
- na_color
The color to use for missing values. Defaults to "grey".
Details
- step_equal_interval()
Creates equal interval breaks by dividing the data range into equal parts
- step_quantile()
Creates quantile breaks ensuring approximately equal numbers of observations in each class
- step_jenks()
Creates Jenks natural breaks using Fisher-Jenks optimization to minimize within-class variance
See also
interpolate_palette()
for continuous color scales
Examples
if (FALSE) { # \dontrun{
# Texas county income data
library(tidycensus)
tx <- get_acs(geography = "county", variables = "B19013_001",
state = "TX", geometry = TRUE)
# Equal interval classification
eq_class <- step_equal_interval("estimate", tx$estimate, n = 5)
# Quantile classification
qt_class <- step_quantile("estimate", tx$estimate, n = 5)
# Jenks natural breaks
jk_class <- step_jenks("estimate", tx$estimate, n = 5)
# Use in a map with formatted legend
maplibre() |>
add_fill_layer(source = tx, fill_color = eq_class$expression) |>
add_legend(
legend_title = "Median Income",
values = get_legend_labels(eq_class, format = "currency"),
colors = get_legend_colors(eq_class),
type = "categorical"
)
# Compare different methods
print(eq_class, format = "currency")
print(qt_class, format = "compact", prefix = "$")
} # }