Skip to content

Commit e157700

Browse files
committed
Adds support for different grids in geom_streamline()
1 parent 35c0d41 commit e157700

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- `geom_streamline()` gets a `start` argument to manually specify starting locations for the integration.
66

7+
- `geom_streamline()` handles groups defined in different grids better.
8+
79
## Bug fixes
810

911
- Errors in the mismatched spatial positioning of `WaveFlux()` calculation results and the internal calculation of the Coriolis parameter(`f`) have been corrected. Additionally, robust adjustments have been made to data at low latitudes (near the equator).
@@ -12,6 +14,8 @@
1214

1315
- Fixes return value of `FitWave()` to always return integer `k`.
1416

17+
18+
1519
# metR 0.18.3
1620

1721
## Bug fixes

R/geom_streamline.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,17 @@ StatStreamline <- ggplot2::ggproto(
269269
ggplot2::Stat,
270270
required_aes = c("x", "y", "dx", "dy"),
271271
setup_params = function(data, params) {
272-
# M <- with(data, max(Mag(dx, dy), na.rm = T))
273272
m <- with(data, mean(Mag(dx, dy), na.rm = TRUE)) # No me gustaaaa
274-
r <- min(
275-
ggplot2::resolution(data$x, zero = FALSE),
276-
ggplot2::resolution(data$y, zero = FALSE)
277-
)
273+
274+
# Supports using different (regular) grids for different groups,
275+
# but uses the same paramters for all to make fair comparisons
276+
r <- data.table::as.data.table(data)[,
277+
.(
278+
rx = ggplot2::resolution(x, zero = FALSE),
279+
ry = ggplot2::resolution(y, zero = FALSE)
280+
),
281+
by = group
282+
][, min(rx, ry)]
278283

279284
if (is.null(params$dt)) {
280285
params$dt <- r / m / params$res
@@ -292,6 +297,16 @@ StatStreamline <- ggplot2::ggproto(
292297
return(params)
293298
},
294299
setup_data = function(data, params) {
300+
regular_test <- data.table::as.data.table(data)[,
301+
.(is_regular = .is.regular_grid(x, y)),
302+
by = group
303+
]
304+
if (any(!regular_test$is_regular)) {
305+
stopf(
306+
"'x' and 'y' do not define a regular grid. If using multiple groups with different grids, set the 'group' aesthetic explicitly."
307+
)
308+
}
309+
295310
data$dx[is.na(data$dx)] <- 0
296311
data$dy[is.na(data$dy)] <- 0
297312
data

R/helpfunctions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ if (getRversion() >= "2.15.1") {
296296
xs <- data.table::uniqueN(data$x)
297297
ys <- data.table::uniqueN(data$y)
298298

299-
# Conditinos for regular grid
299+
# Conditions for regular grid
300300
# 1. each y has the same number of unique values of x
301301
# 2. each x has the same number of unique values of y
302302
regularity <- sum(abs(ys - ny)) == 0 & sum(abs(xs - nx)) == 0

0 commit comments

Comments
 (0)