Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
^README\.Rmd$
^\.github$
^docs$
^gallery$
^pkgdown$
^_pkgdown\.yml$
^cran-comments\.md$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")))
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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
-------------
Expand Down
55 changes: 14 additions & 41 deletions R/angle_correction.r
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- badges: end -->

<img src="man/figures/logo.png" align="right" alt = "logo" class = "pkgdown-hide" />
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<!-- badges: end -->

<img src="man/figures/logo.png" align="right" alt = "logo" class = "pkgdown-hide" />
Expand Down Expand Up @@ -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
```
Expand Down
39 changes: 39 additions & 0 deletions gallery/gallery.r
Original file line number Diff line number Diff line change
@@ -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)
Binary file added gallery/pepijn-devries-ggfields.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 14 additions & 41 deletions man/angle_correction.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/figures/README-map-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 9 additions & 14 deletions tests/testthat/test_anglecorrection.r
Original file line number Diff line number Diff line change
@@ -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", {
Expand All @@ -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()
Expand All @@ -33,7 +28,7 @@ test_that(
geometry = sf::st_sfc(sf::st_polygon())
) |>
sf::st_as_sf()

angle_correction(data, params_mockup, coord)
})
}) |> suppressMessages()
Expand All @@ -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()
21 changes: 8 additions & 13 deletions tests/testthat/test_helpers.r
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand All @@ -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))
})
Expand Down Expand Up @@ -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"),
Expand Down