Skip to content

Commit 4a607ca

Browse files
authored
fix nudging with multiple values. closes #2977 (#3024)
1 parent a8e9668 commit 4a607ca

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

R/position-nudge.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ PositionNudge <- ggproto("PositionNudge", Position,
4747

4848
compute_layer = function(data, params, panel) {
4949
# transform only the dimensions for which non-zero nudging is requested
50-
if (params$x != 0) {
51-
if (params$y != 0) {
50+
if (any(params$x != 0)) {
51+
if (any(params$y != 0)) {
5252
transform_position(data, function(x) x + params$x, function(y) y + params$y)
5353
} else {
5454
transform_position(data, function(x) x + params$x, NULL)
5555
}
56-
} else if (params$y != 0) {
56+
} else if (any(params$y != 0)) {
5757
transform_position(data, NULL, function(y) y + params$y)
5858
} else {
5959
data # if both x and y are 0 we don't need to transform

tests/testthat/test-position-nudge.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
context("position_nudge")
22

33
test_that("nudging works in both dimensions simultaneously", {
4+
# individual nudge value
45
df <- data_frame(x = 1:3)
56

67
p <- ggplot(df, aes(x, x, xmax = x, xmin = x, ymax = x, ymin = x)) +
@@ -14,6 +15,22 @@ test_that("nudging works in both dimensions simultaneously", {
1415
expect_equal(data$y, 3:5)
1516
expect_equal(data$ymin, 3:5)
1617
expect_equal(data$ymax, 3:5)
18+
19+
# multiple nudge values, including zero
20+
df <- data_frame(x = c(1, 2, 1))
21+
22+
p <- ggplot(df, aes(x, x, xmax = x, xmin = x, ymax = x, ymin = x)) +
23+
geom_point(position = position_nudge(x = c(0, -1, 0), y = c(0, 1, 2)))
24+
25+
data <- layer_data(p)
26+
27+
expect_equal(data$x, c(1, 1, 1))
28+
expect_equal(data$xmin, c(1, 1, 1))
29+
expect_equal(data$xmax, c(1, 1, 1))
30+
expect_equal(data$y, c(1, 3, 3))
31+
expect_equal(data$ymin, c(1, 3, 3))
32+
expect_equal(data$ymax, c(1, 3, 3))
33+
1734
})
1835

1936
test_that("nudging works in individual dimensions", {
@@ -30,6 +47,17 @@ test_that("nudging works in individual dimensions", {
3047
expect_equal(data$xmin, 2:4)
3148
expect_equal(data$xmax, 2:4)
3249

50+
# multiple nudge values, including zero
51+
p <- ggplot(df, aes(x = x, xmax = x, xmin = x)) +
52+
layer(geom = Geom, stat = StatIdentity, position = position_nudge(x = c(0, -1, -2)))
53+
54+
data <- layer_data(p)
55+
56+
expect_equal(data$x, c(1, 1, 1))
57+
expect_equal(data$xmin, c(1, 1, 1))
58+
expect_equal(data$xmax, c(1, 1, 1))
59+
60+
3361
# nudging in y
3462
# use an empty layer so can test individual aesthetics
3563
p <- ggplot(df, aes(y = x, ymax = x, ymin = x)) +
@@ -40,4 +68,15 @@ test_that("nudging works in individual dimensions", {
4068
expect_equal(data$y, 3:5)
4169
expect_equal(data$ymin, 3:5)
4270
expect_equal(data$ymax, 3:5)
71+
72+
# multiple nudge values, including zero
73+
p <- ggplot(df, aes(y = x, ymax = x, ymin = x)) +
74+
layer(geom = Geom, stat = StatIdentity, position = position_nudge(y = c(0, -1, -2)))
75+
76+
data <- layer_data(p)
77+
78+
expect_equal(data$y, c(1, 1, 1))
79+
expect_equal(data$ymin, c(1, 1, 1))
80+
expect_equal(data$ymax, c(1, 1, 1))
81+
4382
})

0 commit comments

Comments
 (0)