Skip to content

Commit 9d03bed

Browse files
committed
Add ability to change default map exclude with env var
1 parent f8112ed commit 9d03bed

File tree

16 files changed

+184
-38
lines changed

16 files changed

+184
-38
lines changed

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# [unreleased]
22

3+
### New Features
4+
* Puerto Rico has been added!
5+
* Finally, after years of procrastinating, Puerto Rico has finally been added to the map, see [Issue #48](https://github.com/pdil/usmapdata/issues/48).
6+
* By default, it is not shown. It can be shown by setting `include = "PR"` (or using any combination of `include`/`exclude` parameters that results in `PR` being shown).
7+
* The default can be changed to show `PR` by setting the environment variable `USMAP_EXCLUDE_PR = FALSE`.
8+
* All included map files have been retroactively updated to include Puerto Rico, so any valid value of `data_year` will include Puerto Rico if desired.
9+
* Special thanks [@dcaud](https://github.com/dcaud) who started this work [years ago](https://github.com/pdil/usmap/pull/34).
10+
11+
### Enhancements
12+
* `include` now takes precedence over `exclude` in `us_map()`.
13+
* Any items that are in both the `include` and `exclude` vectors will be _included_.
14+
315
### Removed
416
* The `as_sf` parameter has been completely removed from `us_map()`, `centroid_labels()`, and `fips_data()`.
517
* It was no longer used by `usmap` nor did it have any effect if set.

R/aaa.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#' Internal package environment for usmapdata.
2+
#' @keywords internal
3+
.pkg_env <- new.env(parent = emptyenv())

R/create-us-map.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' \link[usmap]{usmap} package.
66
#'
77
#' `ea_crs()` returns the US National Atlas Equal Area coordinate reference system
8-
#' (CRS) used by this package and `usmap`.
8+
#' (CRS) used by this package and \link[usmap]{usmap}.
99
#'
1010
#' `transform2D()` computes a two dimensional affine transformation matrix
1111
#' for the provided rotation angle and scale factor.

R/fips-data.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#'
33
#' @inheritParams us_map
44
#'
5-
#' @return An data frame of FIPS codes of the desired \code{regions}.
5+
#' @return An data frame of FIPS codes of the desired `regions`.
66
#'
77
#' @examples
88
#' str(fips_data())

R/us-map.R

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
#' Retrieve US map data
22
#'
33
#' @param regions The region breakdown for the map, can be one of
4-
#' (\code{"states"}, \code{"state"}, \code{"counties"}, \code{"county"}).
5-
#' The default is \code{"states"}.
6-
#' @param include The regions to include in the resulting map. If \code{regions} is
7-
#' \code{"states"}/\code{"state"}, the value can be either a state name, abbreviation or FIPS code.
4+
#' (`"states"`, `"state"`, `"counties"`, `"county"`).
5+
#' The default is `"states"`.
6+
#' @param include The regions to include in the resulting map. If `regions` is
7+
#' `"states"`/`"state"`, the value can be either a state name, abbreviation or FIPS code.
88
#' For counties, the FIPS must be provided as there can be multiple counties with the
99
#' same name. If states are provided in the county map, only counties in the included states
1010
#' will be returned.
11-
#' @param exclude The regions to exclude in the resulting map. If \code{regions} is
12-
#' \code{"states"}/\code{"state"}, the value can be either a state name, abbreviation or FIPS code.
11+
#' @param exclude The regions to exclude in the resulting map. If `regions` is
12+
#' `"states"`/`"state"`, the value can be either a state name, abbreviation or FIPS code.
1313
#' For counties, the FIPS must be provided as there can be multiple counties with the
14-
#' same name. The regions listed in the \code{include} parameter are applied first and the
15-
#' \code{exclude} regions are then removed from the resulting map. Any excluded regions
16-
#' not present in the included regions will be ignored.
14+
#' same name. The regions listed in the `include` parameter are applied first and the
15+
#' `exclude` regions are then removed from the resulting map. Any excluded regions
16+
#' not present in the included regions will be ignored. The default value is "PR".
17+
#' The default can be set to be an empty vector by setting the environment variable
18+
#' `USMAP_EXCLUDE_PR = FALSE`.
1719
#' @param data_year The year for which to obtain map data.
18-
#' If the value is \code{NULL}, the most recent year's data is used. If the
20+
#' If the value is `NULL`, the most recent year's data is used. If the
1921
#' provided year is not found from the available map data sets, the next most
2022
#' recent year's data is used. This can be used if an older data set is being
2123
#' plotted on the US map so that the data matches the map more accurately.
2224
#' Therefore, the provided value should match the year of the plotted data set.
23-
#' The default is \code{NULL}, i.e. the most recent available year is used.
25+
#' The default is `NULL`, i.e. the most recent available year is used.
2426
#'
25-
#' @return An `sf` data frame of US map coordinates divided by the desired \code{regions}.
27+
#' @return An `sf` data frame of US map coordinates divided by the desired `regions`.
2628
#'
2729
#' @examples
2830
#' str(us_map())
@@ -38,7 +40,7 @@
3840
us_map <- function(
3941
regions = c("states", "state", "counties", "county"),
4042
include = c(),
41-
exclude = c(),
43+
exclude = .pkg_env$usmap_default_exclude(),
4244
data_year = NULL
4345
) {
4446
regions <- match.arg(regions)
@@ -50,20 +52,25 @@ us_map <- function(
5052
file_path <- system.file("extdata", map_year, file_name, package = "usmapdata")
5153
df <- sf::read_sf(file_path, as_tibble = FALSE)
5254

53-
if (length(include) > 0) {
54-
df <- df[df$full %in% include |
55-
df$abbr %in% include |
56-
df$fips %in% include |
57-
substr(df$fips, 1, 2) %in% include, ]
58-
}
55+
# remove excluded items that are in `include`
56+
exclude <- setdiff(exclude, include)
5957

58+
# remove excludes
6059
if (length(exclude) > 0) {
6160
df <- df[!(df$full %in% exclude |
6261
df$abbr %in% exclude |
6362
df$fips %in% exclude |
6463
substr(df$fips, 1, 2) %in% exclude), ]
6564
}
6665

66+
# remove non-includes
67+
if (length(include) > 0) {
68+
df <- df[df$full %in% include |
69+
df$abbr %in% include |
70+
df$fips %in% include |
71+
substr(df$fips, 1, 2) %in% include, ]
72+
}
73+
6774
df[order(df$abbr), ]
6875
}
6976

@@ -72,7 +79,7 @@ us_map <- function(
7279
#' @inheritParams us_map
7380
#'
7481
#' @return An `sf` data frame of state or county centroid labels and positions
75-
#' relative to the coordinates returned by the \code{us_map} function.
82+
#' relative to the coordinates returned by the \link{us_map} function.
7683
#'
7784
#' @export
7885
centroid_labels <- function(
@@ -105,8 +112,8 @@ available_map_years <- function() {
105112

106113
#' Select appropriate map data year from available years
107114
#'
108-
#' @param data_year The year for which to obtain \code{usmap} data.
109-
#' If the value is \code{NULL}, the most recent year is returned. If the
115+
#' @param data_year The year for which to obtain \link{us_map} data.
116+
#' If the value is `NULL`, the most recent year is returned. If the
110117
#' provided year is not found from the available map data sets, the next most
111118
#' recent available year is returned.
112119
#'
@@ -140,3 +147,11 @@ select_map_year <- function(data_year) {
140147
data_year
141148
}
142149
}
150+
151+
#' @keywords internal
152+
.pkg_env$usmap_default_exclude <- function() {
153+
if (Sys.getenv("USMAP_EXCLUDE_PR", unset = TRUE))
154+
c("PR")
155+
else
156+
c()
157+
}

R/usmapdata-package.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#' @section Map data frames:
1010
#' Alaska and Hawaii have been manually moved to a new location so that
1111
#' their new coordinates place them to the bottom-left corner of
12-
#' the map. These maps can be accessed by using the \code{\link{us_map}} function.
12+
#' the map. These maps can be accessed by using the \link{us_map} function.
1313
#'
1414
#' The function provides the ability to retrieve maps with either
15-
#' state borders or county borders using the \code{regions} parameter
15+
#' state borders or county borders using the `regions` parameter
1616
#' for convenience.
1717
#'
1818
#' States (or counties) can be included such that all other states (or counties)
19-
#' are excluded using the \code{include} parameter.
19+
#' are excluded using the `include` parameter.
2020
#'
2121
#' @author Paolo Di Lorenzo \cr
2222
#' \itemize{

data-raw/test-harness/test-harness.R

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,76 @@
44
devtools::load_all(getwd()) # load local developer build of usmapdata
55

66
usmapdata:::create_us_map(
7-
type = "states",
8-
input_file = "data-raw/shapefiles/2024/cb_2024_us_state_20m.shp",
7+
type = "counties",
8+
input_file = "data-raw/shapefiles/2024/cb_2024_us_county_20m.shp",
99
output_dir = "data-raw/test-harness",
10-
output_file = "us_states.gpkg"
10+
output_file = "us_counties.gpkg"
1111
)
1212

13-
file_path <- system.file("data-raw/test-harness/us_states.gpkg", package = "usmapdata")
13+
file_path <- system.file("data-raw/test-harness/us_counties.gpkg", package = "usmapdata")
1414
df <- sf::read_sf(file_path, as_tibble = FALSE)
1515

16-
ggplot2::ggplot(data = df) +
16+
ggplot2::ggplot(data = df[df$abbr == "PR", ]) +
1717
ggplot2::geom_sf(color = "black", fill = "white")
18+
19+
perform_transform <- function(data, ...) {
20+
data <- sf::st_as_sf(as.data.frame(data), coords = c("lon", "lat"))
21+
data_sf <- sf::st_as_sf(data, ...)
22+
23+
if (is.na(sf::st_crs(data_sf))) {
24+
crs <- list(...)[["crs"]]
25+
if (is.null(crs)) crs <- sf::st_crs(4326)
26+
sf::st_crs(data_sf) <- crs
27+
}
28+
29+
# Transform to canonical projection
30+
transformed <- sf::st_transform(data_sf, usmap::usmap_crs())
31+
sf::st_agr(transformed) <- "constant"
32+
33+
# Transform Alaska points
34+
ak_bbox <- usmapdata:::alaska_bbox()
35+
alaska <- sf::st_intersection(transformed, ak_bbox)
36+
alaska <- usmapdata:::transform_alaska(alaska)
37+
38+
# Transform Hawaii points
39+
hi_bbox <- usmapdata:::hawaii_bbox()
40+
hawaii <- sf::st_intersection(transformed, hi_bbox)
41+
hawaii <- usmapdata:::transform_hawaii(hawaii)
42+
43+
# Transform Hawaii points
44+
pr_bbox <- usmapdata:::puerto_rico_bbox()
45+
puerto_rico <- sf::st_intersection(transformed, pr_bbox)
46+
puerto_rico <- usmapdata:::transform_puerto_rico(puerto_rico)
47+
48+
# Re-combine all points
49+
transformed_excl_ak <- sf::st_difference(transformed, ak_bbox)
50+
sf::st_agr(transformed_excl_ak) <- "constant"
51+
52+
transformed_excl_ak_hi <- sf::st_difference(transformed_excl_ak, hi_bbox)
53+
sf::st_agr(transformed_excl_ak_hi) <- "constant"
54+
55+
transformed_excl_ak_hi_pr <- sf::st_difference(transformed_excl_ak_hi, pr_bbox)
56+
sf::st_agr(transformed_excl_ak_hi_pr) <- "constant"
57+
58+
rbind(transformed_excl_ak_hi_pr, alaska, hawaii, puerto_rico)
59+
}
60+
61+
data <- data.frame(
62+
lon = c(-74.01, -95.36, -118.24, -87.65, -134.42, -157.86, -66.104),
63+
lat = c(40.71, 29.76, 34.05, 41.85, 58.30, 21.31, 18.466),
64+
pop = c(8398748, 2325502, 3990456, 2705994, 32113, 347397, 347052)
65+
)
66+
67+
transformed_data <- perform_transform(data)
68+
69+
library(ggplot2)
70+
ggplot(data = df) +
71+
geom_sf(color = "black", fill = "white") +
72+
geom_sf(
73+
data = transformed_data,
74+
aes(size = pop),
75+
color = "red", alpha = 0.5
76+
) + usmap:::theme_map()
77+
78+
# delete map files
79+
unlink(Sys.glob("data-raw/test-harness/*.gpkg"))
-352 KB
Binary file not shown.
-96 KB
Binary file not shown.

man/centroid_labels.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)