Skip to content

Commit

Permalink
Update documentation and theme based on recent ggplot2 updates (#90)
Browse files Browse the repository at this point in the history
* `size` was erroneously left in the documentation after the `ggplot2
3.4.0` update.
  * It has been replaced with `linewidth`.
* Fix legend in theme after the `ggplot2 v3.5.0`
* Update tests to use 3rd edition of `testthat` 

fixes #89
  • Loading branch information
pdil authored Mar 7, 2024
2 parents 51211ba + ab20398 commit f5c81cc
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 75 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ Imports:
usmapdata (>= 0.2.0)
Suggests:
covr,
ggplot2,
ggplot2 (>= 3.5.0),
ggrepel,
knitr,
proto,
rmarkdown,
scales,
sf,
testthat,
testthat (>= 3.0.0),
vdiffr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Config/testthat/edition: 3
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# [unreleased]

### Improvements
* Add citation information to README, see [Issue #86](https://github.com/pdil/usmap/issues/86).
* Update map theme to work with new legend behavior in [ggplot2 v3.5.0](https://www.tidyverse.org/blog/2024/02/ggplot2-3-5-0-legends/#placement).

### Bug Fixes
* Replace `size` with `linewidth` in `plot_usmap()` documentation, see [Issue #89](https://github.com/pdil/usmap/issues/89).

# usmap 0.7.0
Released Saturday, January 20, 2024.
Expand Down
10 changes: 5 additions & 5 deletions R/plot-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#' for more color options.
#' @param ... Other arguments to pass to [ggplot2::aes()]. These are
#' often aesthetics, used to set an aesthetic to a fixed value, like \code{color = "red"}
#' or \code{size = 3}. They affect the appearance of the polygons used to render
#' or \code{linewidth = 3}. They affect the appearance of the polygons used to render
#' the map (for example fill color, line color, line thickness, etc.). If any of
#' \code{color}/\code{colour}, \code{fill}, or \code{size} are not specified they
#' \code{color}/\code{colour}, \code{fill}, or \code{linewidth} are not specified they
#' are set to their default values of \code{color="black"}, \code{fill="white"},
#' and \code{size=0.4}.
#' and \code{linewidth=0.4}.
#'
#' @return A [ggplot2::ggplot] object that contains a basic
#' US map with the described parameters. Since the result is a \code{ggplot}
Expand Down Expand Up @@ -174,6 +174,6 @@ theme_map <- function(base_size = 9, base_family = "") {
panel.grid = element_blank,
panel.spacing = unit(0, "lines"),
plot.background = element_blank,
legend.justification = c(0, 0),
legend.position = c(0, 0))
legend.position = "inside",
legend.justification.inside = c(0, 0))
}
6 changes: 3 additions & 3 deletions man/plot_usmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/usmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(usmap)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 16 additions & 13 deletions tests/testthat/test-fips-info.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Retrieving FIPS code info")

test_that("all states returned when no FIPS specified", {
expect_equal(fips_info()$fips, fips())
Expand All @@ -12,8 +11,8 @@ test_that("returns correct state FIPS information", {
stringsAsFactors = FALSE
)

expect_equivalent(fips_info(c(2, 34, 2)), ak_result)
expect_equivalent(fips_info(c("02", "34", "02")), ak_result)
expect_equal(fips_info(c(2, 34, 2)), ak_result, ignore_attr = TRUE)
expect_equal(fips_info(c("02", "34", "02")), ak_result, ignore_attr = TRUE)

ak_result_sorted <- data.frame(
abbr = c("AK", "NJ"),
Expand All @@ -22,10 +21,12 @@ test_that("returns correct state FIPS information", {
stringsAsFactors = FALSE
)

expect_equivalent(fips_info(c(2, 34, 2), sortAndRemoveDuplicates = TRUE),
ak_result_sorted)
expect_equivalent(fips_info(c("02", "34", "02"), sortAndRemoveDuplicates = TRUE),
ak_result_sorted)
expect_equal(fips_info(c(2, 34, 2), sortAndRemoveDuplicates = TRUE),
ak_result_sorted,
ignore_attr = TRUE)
expect_equal(fips_info(c("02", "34", "02"), sortAndRemoveDuplicates = TRUE),
ak_result_sorted,
ignore_attr = TRUE)
})

test_that("returns correct county FIPS information", {
Expand All @@ -38,8 +39,8 @@ test_that("returns correct county FIPS information", {
stringsAsFactors = FALSE
)

expect_equivalent(fips_info(c(2020, 2016, 2020)), ak_result)
expect_equivalent(fips_info(c("02020", "02016", "02020")), ak_result)
expect_equal(fips_info(c(2020, 2016, 2020)), ak_result, ignore_attr = TRUE)
expect_equal(fips_info(c("02020", "02016", "02020")), ak_result, ignore_attr = TRUE)

ak_result_sorted <- data.frame(
full = rep("Alaska", 2),
Expand All @@ -49,10 +50,12 @@ test_that("returns correct county FIPS information", {
stringsAsFactors = FALSE
)

expect_equivalent(fips_info(c(2020, 2016, 2020), sortAndRemoveDuplicates = TRUE),
ak_result_sorted)
expect_equivalent(fips_info(c("02020", "02016", "02020"), sortAndRemoveDuplicates = TRUE),
ak_result_sorted)
expect_equal(fips_info(c(2020, 2016, 2020), sortAndRemoveDuplicates = TRUE),
ak_result_sorted,
ignore_attr = TRUE)
expect_equal(fips_info(c("02020", "02016", "02020"), sortAndRemoveDuplicates = TRUE),
ak_result_sorted,
ignore_attr = TRUE)
})

# non-existent yet valid means a FIPS such as "03", which falls
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-fips.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Retrieving FIPS codes")

test_that("all FIPS codes returned when no state specified", {
expect_equal(fips(),
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-join-data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Joining state and county data to map")

state_data <- data.frame(fips = c("01", "02", "04"), values = c(3, 5, 10))
county_data <- data.frame(fips = c("01001", "01003", "01005"), values = c(3, 5, 10))
Expand Down
59 changes: 29 additions & 30 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Plotting US maps")

test_that("dependencies are verified", {
expect_package_error("ggplot2", plot_usmap())
Expand All @@ -20,13 +19,13 @@ u <- plot_usmap(include = .new_england, exclude = "ME", labels = TRUE)
v <- plot_usmap("state", labels = TRUE)

test_that("ggplot object is returned", {
expect_is(p, "ggplot")
expect_is(q, "ggplot")
expect_is(r, "ggplot")
expect_is(s, "ggplot")
expect_is(t, "ggplot")
expect_is(u, "ggplot")
expect_is(v, "ggplot")
expect_s3_class(p, "ggplot")
expect_s3_class(q, "ggplot")
expect_s3_class(r, "ggplot")
expect_s3_class(s, "ggplot")
expect_s3_class(t, "ggplot")
expect_s3_class(u, "ggplot")
expect_s3_class(v, "ggplot")
})

test_that("no warnings are produced", {
Expand Down Expand Up @@ -80,60 +79,60 @@ test_that("plots are stable", {
})

test_that("layer parameters are correct", {
expect_is(p$layers[[1]], "ggproto")
expect_is(p$layers[[1]]$geom, "GeomSf")
expect_s3_class(p$layers[[1]], "ggproto")
expect_s3_class(p$layers[[1]]$geom, "GeomSf")
expect_equal(as.character(p$layers[[1]]$aes_params$colour), "black")
expect_equal(as.character(p$layers[[1]]$aes_params$fill), "red")
expect_equal(p$layers[[1]]$aes_params$linewidth, 0.4)

expect_is(q$layers[[1]], "ggproto")
expect_is(q$layers[[1]]$geom, "GeomSf")
expect_s3_class(q$layers[[1]], "ggproto")
expect_s3_class(q$layers[[1]]$geom, "GeomSf")
expect_equal(deparse(q$layers[[1]]$mapping$fill), "~.data[[\"pop_2015\"]]")
expect_equal(as.character(q$layers[[1]]$aes_params$colour), "blue")
expect_equal(q$layers[[1]]$aes_params$linewidth, 0.4)

expect_is(r$layers[[1]], "ggproto")
expect_is(r$layers[[1]]$geom, "GeomSf")
expect_s3_class(r$layers[[1]], "ggproto")
expect_s3_class(r$layers[[1]]$geom, "GeomSf")
expect_equal(deparse(r$layers[[1]]$mapping$fill), "~.data[[\"values\"]]")
expect_equal(as.character(r$layers[[1]]$aes_params$colour), "black")
expect_equal(r$layers[[1]]$aes_params$linewidth, 0.8)

expect_is(s$layers[[1]], "ggproto")
expect_is(s$layers[[1]]$geom, "GeomSf")
expect_s3_class(s$layers[[1]], "ggproto")
expect_s3_class(s$layers[[1]]$geom, "GeomSf")
expect_equal(as.character(s$layers[[1]]$aes_params$fill), "white")
expect_equal(as.character(s$layers[[1]]$aes_params$colour), "black")
expect_equal(s$layers[[1]]$aes_params$linewidth, 0.4)
expect_is(s$layers[[2]], "ggproto")
expect_is(s$layers[[2]]$geom, "GeomText")
expect_s3_class(s$layers[[2]], "ggproto")
expect_s3_class(s$layers[[2]]$geom, "GeomText")
expect_equal(deparse(s$layers[[2]]$mapping$label), "~.data$abbr")
expect_equal(as.character(s$layers[[2]]$aes_params$colour), "blue")

expect_is(t$layers[[1]], "ggproto")
expect_is(s$layers[[1]]$geom, "GeomSf")
expect_s3_class(t$layers[[1]], "ggproto")
expect_s3_class(s$layers[[1]]$geom, "GeomSf")
expect_equal(as.character(t$layers[[1]]$aes_params$fill), "yellow")
expect_equal(as.character(t$layers[[1]]$aes_params$colour), "black")
expect_equal(t$layers[[1]]$aes_params$linewidth, 0.6)
expect_is(t$layers[[2]], "ggproto")
expect_is(t$layers[[2]]$geom, "GeomText")
expect_s3_class(t$layers[[2]], "ggproto")
expect_s3_class(t$layers[[2]]$geom, "GeomText")
expect_equal(deparse(t$layers[[2]]$mapping$label),
"~sub(\" County\", \"\", .data$county)")

expect_is(u$layers[[1]], "ggproto")
expect_is(u$layers[[1]]$geom, "GeomSf")
expect_s3_class(u$layers[[1]], "ggproto")
expect_s3_class(u$layers[[1]]$geom, "GeomSf")
expect_equal(as.character(u$layers[[1]]$aes_params$fill), "white")
expect_equal(as.character(u$layers[[1]]$aes_params$colour), "black")
expect_equal(u$layers[[1]]$aes_params$linewidth, 0.4)
expect_is(u$layers[[2]], "ggproto")
expect_is(u$layers[[2]]$geom, "GeomText")
expect_s3_class(u$layers[[2]], "ggproto")
expect_s3_class(u$layers[[2]]$geom, "GeomText")
expect_equal(deparse(u$layers[[2]]$mapping$label), "~.data$abbr")

expect_is(v$layers[[1]], "ggproto")
expect_is(v$layers[[1]]$geom, "GeomSf")
expect_s3_class(v$layers[[1]], "ggproto")
expect_s3_class(v$layers[[1]]$geom, "GeomSf")
expect_equal(as.character(v$layers[[1]]$aes_params$fill), "white")
expect_equal(as.character(v$layers[[1]]$aes_params$colour), "black")
expect_equal(v$layers[[1]]$aes_params$linewidth, 0.4)
expect_is(v$layers[[2]], "ggproto")
expect_is(v$layers[[2]]$geom, "GeomText")
expect_s3_class(v$layers[[2]], "ggproto")
expect_s3_class(v$layers[[2]]$geom, "GeomText")
expect_equal(deparse(v$layers[[2]]$mapping$label), "~.data$abbr")
})

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-transform.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Transforming coordinate data frames")

test_that("data frame with AK and HI points is transformed", {
data <- data.frame(
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-usmap.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Retrieving US map data")

test_that("state data frame is returned", {
data <- us_map()
Expand Down

0 comments on commit f5c81cc

Please sign in to comment.