diff --git a/.Rbuildignore b/.Rbuildignore index 95cc736..2356ccc 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -5,6 +5,7 @@ ^README\.Rmd$ ^\.github$ ^docs$ +^gallery$ ^pkgdown$ ^_pkgdown\.yml$ ^cran-comments\.md$ diff --git a/DESCRIPTION b/DESCRIPTION index fc5411d..4b1382b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggfields Title: Add Vector Field Layers to Ggplots -Version: 0.0.6.0003 +Version: 0.0.7 Authors@R: c(person("Pepijn", "de Vries", role = c("aut", "cre"), email = "pepijn.devries@outlook.com", comment = c(ORCID = "0000-0002-7961-6646"))) diff --git a/NEWS.md b/NEWS.md index f7e07d1..e05d0f7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,10 @@ -ggfields v0.0.6.0003 +ggfields v0.0.7 ------------- * Fixed test * Added tests * Added test coverage workflow and codecov badge + * Fixes for compatibility with latest `ggplot2` release ggfields v0.0.6 ------------- diff --git a/R/angle_correction.r b/R/angle_correction.r index e9a5843..802e2a1 100644 --- a/R/angle_correction.r +++ b/R/angle_correction.r @@ -17,48 +17,21 @@ #' @returns A `data.frame` with an additional `angle_correction` column. The corrected angle is given #' by `angle_correction + angle`. #' @examples -#' ## Create a data.frame with some xy-coordinates and all angles pointing North (0 degrees) -#' d <- -#' data.frame( -#' x = seq(1, 2, 0.1), -#' y = seq(50, 51, 0.1), -#' angle = 0 -#' ) |> -#' sf::st_as_sf(coords = c("x", "y"), crs = 4326, remove = FALSE) +#' if (requireNamespace("ggplot2") && requireNamespace("stars")) { +#' library(ggplot2) +#' library(stars) #' -#' ## Create a mockup of ggplot params. Normally this is handled automatically by ggplot2 -#' params_mockup <- -#' c( -#' ggplot2::ggplot() + geom_fields(), -#' list( -#' x_range = range(d$x), -#' y_range = range(d$y), -#' crs = sf::st_crs(4326), -#' default_crs = 4326 -#' ) -#' ) -#' -#' ## When plotting as lon-lat, the angle correction will be zero -#' angle_correction(d, params_mockup, ggplot2::coord_sf(default_crs = 4326)) -#' -#' ## Transform to UTM zone 31N in meters -#' d2 <- d |> sf::st_transform(32631) -#' -#' ## Again get parameter mockup values -#' params_mockup2 <- -#' c( -#' ggplot2::ggplot() + geom_fields(), -#' list( -#' x_range = range(sf::st_coordinates(d2)[,1]), -#' y_range = range(sf::st_coordinates(d2)[,1]), -#' crs = sf::st_crs(32631), -#' default_crs = 4326 -#' ) -#' ) -#' ## in UTM projection in this area (which is slightly tilted) the correction is -#' ## larger than zero -#' angle_correction(d2, params_mockup2, -#' ggplot2::coord_sf(crs = 32631, default_crs = 4326)) +#' ggplot() + +#' geom_fields( +#' data = seawatervelocity, +#' mapping = aes(radius = as.numeric(v), +#' angle = as.numeric(angle), +#' col = as.numeric(v)), +#' ## You can provide the `angle_correction()` as argument explicitly +#' ## (it is already the default). Note that the plotted region requires +#' ## hardly any correction for the angles. +#' .angle_correction = angle_correction) +#' } #' @author Pepijn de Vries #' @export angle_correction <- function(data, panel_params, coord) { diff --git a/README.Rmd b/README.Rmd index 616dfe2..eaca9fc 100644 --- a/README.Rmd +++ b/README.Rmd @@ -22,7 +22,7 @@ knitr::opts_chunk$set( [![version](https://www.r-pkg.org/badges/version/ggfields)](https://CRAN.R-project.org/package=ggfields) [![cran checks](https://badges.cranchecks.info/worst/ggfields.svg)](https://cran.r-project.org/web/checks/check_results_ggfields.html) [![ggfields status badge](https://pepijn-devries.r-universe.dev/badges/ggfields)](https://pepijn-devries.r-universe.dev/ggfields) -[![codecov](https://codecov.io/gh/pepijn-devries/ggfields/graph/badge.svg?token=M8DRDR80RL)](https://codecov.io/gh/pepijn-devries/ggfields) +[![codecov](https://codecov.io/gh/pepijn-devries/ggfields/graph/badge.svg?token=M8DRDR80RL)](https://app.codecov.io/gh/pepijn-devries/ggfields) logo diff --git a/README.md b/README.md index 7638bf8..8484cb2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ checks](https://badges.cranchecks.info/worst/ggfields.svg)](https://cran.r-project.org/web/checks/check_results_ggfields.html) [![ggfields status badge](https://pepijn-devries.r-universe.dev/badges/ggfields)](https://pepijn-devries.r-universe.dev/ggfields) -[![codecov](https://codecov.io/gh/pepijn-devries/ggfields/graph/badge.svg?token=M8DRDR80RL)](https://codecov.io/gh/pepijn-devries/ggfields) +[![codecov](https://codecov.io/gh/pepijn-devries/ggfields/graph/badge.svg?token=M8DRDR80RL)](https://app.codecov.io/gh/pepijn-devries/ggfields) logo @@ -48,7 +48,6 @@ The example below shows how seawater current data can be added to a map: ``` r library(ggplot2) -#> Warning: package 'ggplot2' was built under R version 4.3.3 library(ggfields) library(ggspatial) ## For annotating with Open Street Map ``` diff --git a/gallery/gallery.r b/gallery/gallery.r new file mode 100644 index 0000000..be78a45 --- /dev/null +++ b/gallery/gallery.r @@ -0,0 +1,39 @@ +library(ggplot2) +library(ggfields) +library(basemaps) +library(ggspatial) + +data(seawatervelocity) + +temp <- + basemap_geotif( + st_bbox(seawatervelocity), + map_service = "esri", map_type = "world_imagery") + +topo <- stars::read_stars(temp) + +p <- + ggplot() + + annotation_spatial(topo) + + geom_fields( + data = seawatervelocity, + aes(radius = as.numeric(v), + linewidth = as.numeric(v), + angle = as.numeric(angle), + colour = as.numeric(v)), + max_radius = grid::unit(0.5, "cm"), + arrow = grid::arrow( + type = "closed", + angle = 30, + length = grid::unit(0.05, "cm"))) + + labs(colour = "v[m/s]", + linewidth = "v[m/s]", + radius = "v[m/s]") + + scale_radius_binned(breaks = seq(0, 0.3, by = 0.05)) + + scale_linewidth_binned(breaks = seq(0, 0.3, by = 0.05), range = c(.6, 2.5)) + + scale_colour_viridis_b(breaks = seq(0, 0.3, by = 0.05), + option = "magma", guide = guide_bins()) + + theme(legend.key.size = unit(0.8, "cm")) + +ggsave("gallery/pepijn-devries-ggfields.png", p, width = 350, height = 300, units = "px", + dpi = 100) diff --git a/gallery/pepijn-devries-ggfields.png b/gallery/pepijn-devries-ggfields.png new file mode 100644 index 0000000..acb3105 Binary files /dev/null and b/gallery/pepijn-devries-ggfields.png differ diff --git a/man/angle_correction.Rd b/man/angle_correction.Rd index 7b34dd6..151ade5 100644 --- a/man/angle_correction.Rd +++ b/man/angle_correction.Rd @@ -32,48 +32,21 @@ why this correction is required and how to customize corrections please see \code{vignette("angle_correction")}. } \examples{ -## Create a data.frame with some xy-coordinates and all angles pointing North (0 degrees) -d <- - data.frame( - x = seq(1, 2, 0.1), - y = seq(50, 51, 0.1), - angle = 0 - ) |> - sf::st_as_sf(coords = c("x", "y"), crs = 4326, remove = FALSE) +if (requireNamespace("ggplot2") && requireNamespace("stars")) { + library(ggplot2) + library(stars) -## Create a mockup of ggplot params. Normally this is handled automatically by ggplot2 -params_mockup <- - c( - ggplot2::ggplot() + geom_fields(), - list( - x_range = range(d$x), - y_range = range(d$y), - crs = sf::st_crs(4326), - default_crs = 4326 - ) - ) - -## When plotting as lon-lat, the angle correction will be zero -angle_correction(d, params_mockup, ggplot2::coord_sf(default_crs = 4326)) - -## Transform to UTM zone 31N in meters -d2 <- d |> sf::st_transform(32631) - -## Again get parameter mockup values -params_mockup2 <- - c( - ggplot2::ggplot() + geom_fields(), - list( - x_range = range(sf::st_coordinates(d2)[,1]), - y_range = range(sf::st_coordinates(d2)[,1]), - crs = sf::st_crs(32631), - default_crs = 4326 - ) - ) -## in UTM projection in this area (which is slightly tilted) the correction is -## larger than zero -angle_correction(d2, params_mockup2, - ggplot2::coord_sf(crs = 32631, default_crs = 4326)) + ggplot() + + geom_fields( + data = seawatervelocity, + mapping = aes(radius = as.numeric(v), + angle = as.numeric(angle), + col = as.numeric(v)), + ## You can provide the `angle_correction()` as argument explicitly + ## (it is already the default). Note that the plotted region requires + ## hardly any correction for the angles. + .angle_correction = angle_correction) +} } \author{ Pepijn de Vries diff --git a/man/figures/README-map-1.svg b/man/figures/README-map-1.svg index 011375d..84e00a4 100644 --- a/man/figures/README-map-1.svg +++ b/man/figures/README-map-1.svg @@ -50,7 +50,7 @@ - + diff --git a/tests/testthat/test_anglecorrection.r b/tests/testthat/test_anglecorrection.r index 0e3d2bc..308cf20 100644 --- a/tests/testthat/test_anglecorrection.r +++ b/tests/testthat/test_anglecorrection.r @@ -1,15 +1,10 @@ -params_mockup <- - c( - ggplot2::ggplot() + geom_fields(), - list( - x_range = c(1, 2), - y_range = c(50, 51), - crs = sf::st_crs(4326), - default_crs = 4326 - ) - ) +coord <- ggplot2::coord_sf(crs = 4326) -coord <- ggplot2::coord_sf() +params_mockup <- ggplot2::ggplot_build( + ggplot2::ggplot(NULL, ggplot2::aes(1, 1)) + + ggplot2::lims(x = c(1, 2), y = c(50, 51)) + + coord +)$layout$panel_params[[1]] test_that( "Angle correction won't work on geometries other then point", { @@ -19,7 +14,7 @@ test_that( geometry = sf::st_sfc(sf::st_polygon()) ) |> sf::st_as_sf(crs = 4326) - + angle_correction(data, params_mockup, coord) }) }) |> suppressMessages() @@ -33,7 +28,7 @@ test_that( geometry = sf::st_sfc(sf::st_polygon()) ) |> sf::st_as_sf() - + angle_correction(data, params_mockup, coord) }) }) |> suppressMessages() @@ -48,7 +43,7 @@ test_that( angle = 0 ) |> sf::st_as_sf(coords = c("x", "y"), crs = 4326, remove = FALSE) - + angle_correction(data, params_mockup, coord) }) }) |> suppressMessages() diff --git a/tests/testthat/test_helpers.r b/tests/testthat/test_helpers.r index 2767990..1c92fb0 100644 --- a/tests/testthat/test_helpers.r +++ b/tests/testthat/test_helpers.r @@ -25,18 +25,12 @@ data <- data.frame( ) |> st_as_sf() -params_mockup <- - c( - ggplot() + geom_fields(), - list( - x_range = c(1, 2), - y_range = c(50, 51), - crs = st_crs(4326), - default_crs = 4326 - ) - ) - -coord <- coord_sf() +coord <- ggplot2::coord_sf(crs = 4326, default_crs = 4326) +params_mockup <- ggplot2::ggplot_build( + ggplot2::ggplot(NULL, ggplot2::aes(1, 1)) + + ggplot2::lims(x = c(1, 2), y = c(50, 51)) + + coord +)$layout$panel_params[[1]] test_that( "Prep fields coerces stars to sf", { @@ -50,7 +44,7 @@ test_that( test_that( "Setup params add linejoin and lineend when missing", { expect_true({ - params <- .setup_params_fields(params = list()) + params <- ggfields:::.setup_params_fields(params = list()) typeof(params) == "list" && all(c("linejoin", "lineend") %in% names(params)) }) @@ -86,6 +80,7 @@ test_that( test_that( "Panel draw function returns a gTree object", { testthat::expect_s3_class({ + ggfields:::.draw_panel_fields( self, data, params_mockup, coord, FALSE, grid::unit(0.7, "cm"),