Create "bins" for choropleth maps creating using either
ggplot2
or leaflet
. The function can create the bins
automatically or will accept pre-specified breaks.
dep_map_breaks(.data, var, new_var, classes, style, breaks,
sig_digits = 2, return = "col", show_warnings = TRUE)
A data object, either sf, tibble, or data.frame
Variable breaks should be based on, can be quoted or unquoted
Optional name of new variable to store breaks in, can be quoted or unquoted. This is required if you are returning a column, but can be omitted if you are returning breaks instead of a column.
Optional integer scalar; count of the number of classes to create. If you are supplying breaks manually, this can be omitted.
String scalar; one of the classes supported by classInt::classIntervals()
.
"jenks" is the ArcGIS default. "quantile" and "fisher" are better
alternatives. As with classes
, this can be omitted if you are
supplying breaks manually.
Optional numeric vector if you want to pre-specify the cut points for your breaks. Provide the lower and upper bounds of your distribution. Any values supplied in between the bounds will be the upper bound of individual bins.
Integer; how many significant digits should be applied when calculating breaks and constructing labels?
String scalar; one of either "col"
(default) or
"breaks"
. The default behavior adds a factor containing the bins
to the data object to facilitate mapping. Specifying "breaks"
will return the calculated breaks instead, which can be modified and
passed to the breaks
argument later in a script to make the
final map.
Logical scalar; if TRUE
, warnings created by
classInt::classIntervals()
if NA
values are identified while
findings classes.
Either a data object (if return
is "col"
) or a vector
of breaks (if return
is "breaks"
). If a data object is
returned, the new column will be placed directly after the input variable
specified in var
.
# prep data
## load sample data
ndi_m <- dep_sample_data(index = "ndi_m")
## calculate NDI with sample data
ndi_m <- dep_calc_index(ndi_m, geography = "county", index = "ndi_m", year = 2022,
return_percentiles = TRUE)
#> Warning: The proportion of variance explained by PC1 is less than 0.50.
# calculate breaks using a built-in algorithm
dep_map_breaks(ndi_m, var = "NDI_M", new_var = "map_breaks", classes = 5,
style = "fisher")
#> # A tibble: 115 × 5
#> GEOID NAME YEAR NDI_M map_breaks
#> <chr> <chr> <dbl> <dbl> <fct>
#> 1 29001 Adair County, Missouri 2022 63.2 60.10 - 80.26
#> 2 29003 Andrew County, Missouri 2022 6.14 0.00 - 19.74
#> 3 29005 Atchison County, Missouri 2022 24.6 19.75 - 39.91
#> 4 29007 Audrain County, Missouri 2022 58.8 39.92 - 60.09
#> 5 29009 Barry County, Missouri 2022 59.6 39.92 - 60.09
#> 6 29011 Barton County, Missouri 2022 92.1 80.27 - 100.00
#> 7 29013 Bates County, Missouri 2022 83.3 80.27 - 100.00
#> 8 29015 Benton County, Missouri 2022 68.4 60.10 - 80.26
#> 9 29017 Bollinger County, Missouri 2022 78.9 60.10 - 80.26
#> 10 29019 Boone County, Missouri 2022 14.0 0.00 - 19.74
#> # ℹ 105 more rows
# use manually specified breaks
## set breaks
breaks <- c(0, 25, 50, 75, max(ndi_m$NDI_M))
## calculate breaks
dep_map_breaks(ndi_m, var = "NDI_M", new_var = "map_breaks", breaks = breaks)
#> # A tibble: 115 × 5
#> GEOID NAME YEAR NDI_M map_breaks
#> <chr> <chr> <dbl> <dbl> <fct>
#> 1 29001 Adair County, Missouri 2022 63.2 50.01 - 75.00
#> 2 29003 Andrew County, Missouri 2022 6.14 0.00 - 25.00
#> 3 29005 Atchison County, Missouri 2022 24.6 0.00 - 25.00
#> 4 29007 Audrain County, Missouri 2022 58.8 50.01 - 75.00
#> 5 29009 Barry County, Missouri 2022 59.6 50.01 - 75.00
#> 6 29011 Barton County, Missouri 2022 92.1 75.01 - 100.00
#> 7 29013 Bates County, Missouri 2022 83.3 75.01 - 100.00
#> 8 29015 Benton County, Missouri 2022 68.4 50.01 - 75.00
#> 9 29017 Bollinger County, Missouri 2022 78.9 75.01 - 100.00
#> 10 29019 Boone County, Missouri 2022 14.0 0.00 - 25.00
#> # ℹ 105 more rows