Skip to content

Commit 144f053

Browse files
authored
Add Puerto Rico support (#118)
* Add support for Puerto Rico based on `usmapdata 1.0.0` closes #20
2 parents 5d167c6 + b163a07 commit 144f053

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1955
-1725
lines changed

.github/workflows/check.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: check
32

43
on:

.github/workflows/lint.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: lint
32

43
on:

.github/workflows/release.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: release
32
run-name: Release ${{ github.event.repository.name }} ${{ inputs.version }}
43

.github/workflows/test-coverage.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
name: test-coverage
32

43
on:

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ URL: https://usmap.dev
1616
BugReports: https://github.com/pdil/usmap/issues
1717
Imports:
1818
rlang,
19-
usmapdata (>= 0.6.0)
19+
usmapdata (>= 1.0.0)
2020
Suggests:
2121
covr,
2222
ggplot2 (>= 3.5.0),
@@ -28,7 +28,8 @@ Suggests:
2828
sf,
2929
spelling,
3030
testthat (>= 3.0.0),
31-
vdiffr
31+
vdiffr,
32+
withr
3233
RoxygenNote: 7.3.2
3334
Roxygen: list(markdown = TRUE)
3435
VignetteBuilder: knitr

NEWS.md

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

3+
Introducing `usmap 1.0.0`! This week we celebrated 10 years since the [first commit](https://github.com/pdil/usmap/commit/1d6a950d54c8195507406d7042093041381b605c) to this project. It has been quite a ride and `usmap` has grown greatly over the years. To everyone who has supported the project in any way, either by reporting issues, contributing code, or just using the package in your work, thank you so much.
4+
5+
I'm happy to announce that with this version we have finally added Puerto Rico! This addition has been [years](https://github.com/pdil/usmap/issues/20) in the making and its now available throughout `usmap`.
6+
7+
### New Features
8+
* Add Puerto Rico support to all `usmap` functions (where relevant), see [Issue #20](https://github.com/pdil/usmap/issues/20).
9+
* All included map years have been retroactively updated to include Puerto Rico, so any valid value of `data_year` will include Puerto Rico if desired.
10+
* Functions now return Puerto Rico in their data sets and it can be included or excluded just like any state (using FIPS, full name, abbreviation, etc.).
11+
* Puerto Rican municipalities will be shown when using `"county"` map variants.
12+
* By default, Puerto Rico is not shown when using `plot_usmap()` unless it is specifically included with the `include` parameter. This default behavior can be changed by setting the environment variable `USMAP_DEFAULT_EXCLUDE_PR = FALSE`. Setting this parameter will include Puerto Rico by default when calling `plot_usmap()` unless specifically excluded with the `exclude` parameter.
13+
* Special thanks [@dcaud](https://github.com/dcaud) who started this work [years ago](https://github.com/pdil/usmap/pull/34).
14+
15+
### Improvements
16+
* Improve documentation and vignettes.
17+
318
# usmap 0.8.0
419
Released Wednesday, May 28, 2025.
520

@@ -19,6 +34,11 @@ Released Wednesday, May 28, 2025.
1934
### Improvements
2035
* Update package author email.
2136

37+
### Removed
38+
* The `as_sf` parameter has been completely removed from `usmapdata`.
39+
* Its usage was already removed from `usmap` but its removal is noted here as a final notice.
40+
* Any existing code that sets it can safely delete it from `usmapdata` function calls.
41+
2242
# usmap 0.7.1
2343
Released Thursday, March 21, 2024.
2444

R/fips.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#' the available map data sets, the next most recent year's data is used.
2020
#' The default is NULL, i.e. the most recent available year is used.
2121
#'
22-
#' @note A \code{state} must be included when searching for \code{county},
22+
#' @note A `state` must be included when searching for `county`,
2323
#' otherwise multiple results may be returned for duplicate county names.
2424
#'
2525
#' @details State and county FIPS (Federal Information Processing Standards) are
@@ -30,7 +30,7 @@
3030
#' information for the 50 US states (and their associated counties and
3131
#' census designated areas).
3232
#'
33-
#' @return The FIPS code(s) of given \code{state} or \code{county}.
33+
#' @return The FIPS code(s) of given `state` or `county`.
3434
#'
3535
#' If only states are entered, a vector of length equal to the number of states
3636
#' is returned. If any states are not found or are invalid, `NA` is returned in their place.

R/join-data.R

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
#' parameter should be a data frame consisting of two columns,
66
#' a fips code (2 characters for state, 5 characters for county)
77
#' and the value that should be associated with that region. The
8-
#' columns of \code{data} \emph{must} be \code{fips} or \code{state} and
9-
#' the value of the `values` parameter. If both \code{fips} and \code{state}
10-
#' are provided, this function uses the \code{fips}.
8+
#' columns of `data` _must_ be `fips` or `state` and
9+
#' the value of the `values` parameter. If both `fips` and `state`
10+
#' are provided, this function uses the `fips`.
1111
#' @param values The name of the column that contains the values to be associated
12-
#' with a given region. The default is \code{"values"}.
12+
#' with a given region. The default is `"values"`.
1313
#' @param na The value to be inserted for states or counties that don't have
14-
#' a value in \code{data}. This value must be of the same type as the \code{value}
15-
#' column of \code{data}.
14+
#' a value in `data`. This value must be of the same type as the `value`
15+
#' column of `data`.
1616
#'
1717
#' @return A data frame composed of the map data frame (from [us_map()]) except
18-
#' an extra column containing the values in \code{data} is included.
18+
#' an extra column containing the values in `data` is included.
1919
#'
2020
#' The result can be plotted using [ggplot2::ggplot()] or [plot_usmap()].
2121
#'
@@ -69,7 +69,12 @@ map_with_data <- function(
6969
data$fips <- as.character(data$fips)
7070

7171
region_type <- ifelse(nchar(data$fips[1]) <= 2, "state", "county")
72-
map_df <- usmap::us_map(regions = region_type, include = include, exclude = exclude, data_year = data_year)
72+
map_df <- usmap::us_map(
73+
regions = region_type,
74+
include = include,
75+
exclude = exclude,
76+
data_year = data_year
77+
)
7378

7479
# Remove columns in data that are already in map_df
7580
data$abbr <- NULL

R/plot-map.R

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
#' parameter should be a data frame consisting of two columns,
66
#' a FIPS code (2 characters for state, 5 characters for county)
77
#' and the value that should be associated with that region. The
8-
#' columns of \code{data} \emph{must} be \code{fips} or \code{state} and
8+
#' columns of `data` _must_ be `fips` or `state` and
99
#' the value of the `values` parameter.
1010
#' @param values The name of the column that contains the values to be associated
11-
#' with a given region. The default is \code{"value"}.
11+
#' with a given region. The default is `"value"`.
1212
#' @param theme The theme that should be used for plotting the map. The default
13-
#' is \code{theme_map} from \href{https://github.com/jrnold/ggthemes}{ggthemes}.
13+
#' is `theme_map` from [ggthemes](https://github.com/jrnold/ggthemes).
1414
#' @param labels Whether or not to display labels on the map. Labels are not displayed
1515
#' by default.
16-
#' @param label_color The color of the labels to display. Corresponds to the \code{color}
17-
#' option in the [ggplot2::aes()] mapping. The default is \code{"black"}.
18-
#' \href{https://usmap.dev/docs/Rcolor.pdf}{Click here}
16+
#' @param label_color The color of the labels to display. Corresponds to the `color`
17+
#' option in the [ggplot2::aes()] mapping. The default is `"black"`.
18+
#' [Click here](https://usmap.dev/docs/Rcolor.pdf)
1919
#' for more color options.
2020
#' @param ... Other arguments to pass to [ggplot2::aes()]. These are
21-
#' often aesthetics, used to set an aesthetic to a fixed value, like \code{color = "red"}
22-
#' or \code{linewidth = 3}. They affect the appearance of the polygons used to render
21+
#' often aesthetics, used to set an aesthetic to a fixed value, like `color = "red"`
22+
#' or `linewidth = 3`. They affect the appearance of the polygons used to render
2323
#' the map (for example fill color, line color, line thickness, etc.). If any of
24-
#' \code{color}/\code{colour}, \code{fill}, or \code{linewidth} are not specified they
25-
#' are set to their default values of \code{color="black"}, \code{fill="white"},
26-
#' and \code{linewidth=0.4}.
24+
#' `color`/`colour`, `fill`, or `linewidth` are not specified they
25+
#' are set to their default values of `color="black"`, `fill="white"`,
26+
#' and `linewidth=0.4`.
2727
#'
2828
#' @return A [ggplot2::ggplot] object that contains a basic
29-
#' US map with the described parameters. Since the result is a \code{ggplot}
29+
#' US map with the described parameters. Since the result is a `ggplot`
3030
#' object, it can be extended with more [ggplot2::Geom] layers, scales, labels,
3131
#' themes, etc.
3232
#'
33+
#' @details
34+
#' By default, Puerto Rico is not plotted unless it is specifically included
35+
#' via `include = c("PR")` etc. The default behavior can be changed by setting
36+
#' the environment variable `USMAP_DEFAULT_EXCLUDE_PR = FALSE`.
37+
#'
3338
#' @seealso [usmap], [ggplot2::theme()]
3439
#'
3540
#' @examples
@@ -75,6 +80,12 @@ plot_usmap <- function(
7580

7681
.data <- ggplot2::.data
7782

83+
# exclude PR by default if env variable is not `FALSE`
84+
if (Sys.getenv("USMAP_DEFAULT_EXCLUDE_PR", unset = TRUE) == TRUE &&
85+
!("PR" %in% include || "72" %in% include)) {
86+
exclude <- unique(c(exclude, "PR", "72"))
87+
}
88+
7889
# parse parameters
7990
regions <- match.arg(regions)
8091
geom_args <- list(...)
@@ -113,14 +124,10 @@ plot_usmap <- function(
113124

114125
centroid_labels <- usmapdata::centroid_labels(regions, data_year = data_year)
115126

116-
if (length(include) > 0) {
117-
centroid_labels <- centroid_labels[
118-
centroid_labels$full %in% include |
119-
centroid_labels$abbr %in% include |
120-
centroid_labels$fips %in% include,
121-
]
122-
}
127+
# remove excluded items that are in `include`
128+
exclude <- setdiff(exclude, include)
123129

130+
# remove excludes
124131
if (length(exclude) > 0) {
125132
centroid_labels <- centroid_labels[!(
126133
centroid_labels$full %in% exclude |
@@ -130,6 +137,15 @@ plot_usmap <- function(
130137
), ]
131138
}
132139

140+
# remove non-includes
141+
if (length(include) > 0) {
142+
centroid_labels <- centroid_labels[
143+
centroid_labels$full %in% include |
144+
centroid_labels$abbr %in% include |
145+
centroid_labels$fips %in% include,
146+
]
147+
}
148+
133149
if (regions == "county" || regions == "counties") {
134150
label_layer <- ggplot2::geom_sf_text(
135151
data = centroid_labels,

R/transform.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,22 @@ perform_transform <- function(data, ...) {
129129
hawaii <- sf::st_intersection(transformed, hi_bbox)
130130
hawaii <- usmapdata:::transform_hawaii(hawaii)
131131

132+
# Transform Hawaii points
133+
pr_bbox <- usmapdata:::puerto_rico_bbox()
134+
puerto_rico <- sf::st_intersection(transformed, pr_bbox)
135+
puerto_rico <- usmapdata:::transform_puerto_rico(puerto_rico)
136+
132137
# Re-combine all points
133138
transformed_excl_ak <- sf::st_difference(transformed, ak_bbox)
134139
sf::st_agr(transformed_excl_ak) <- "constant"
135140

136141
transformed_excl_ak_hi <- sf::st_difference(transformed_excl_ak, hi_bbox)
137142
sf::st_agr(transformed_excl_ak_hi) <- "constant"
138143

139-
rbind(transformed_excl_ak_hi, alaska, hawaii)
144+
transformed_excl_ak_hi_pr <- sf::st_difference(transformed_excl_ak_hi, pr_bbox)
145+
sf::st_agr(transformed_excl_ak_hi_pr) <- "constant"
146+
147+
rbind(transformed_excl_ak_hi_pr, alaska, hawaii, puerto_rico)
140148
}
141149

142150
#' usmap coordinate reference system

0 commit comments

Comments
 (0)