Skip to content

Restore data order after collide #3680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2019
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
9 changes: 5 additions & 4 deletions R/position-collide.r
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ collide <- function(data, width = NULL, name, strategy,
# Reorder by x position, then on group. The default stacking order reverses
# the group in order to match the legend order.
if (reverse) {
data <- data[order(data$xmin, data$group), ]
ord <- order(data$xmin, data$group)
} else {
data <- data[order(data$xmin, -data$group), ]
ord <- order(data$xmin, -data$group)
}
data <- data[ord, ]

# Check for overlap
intervals <- as.numeric(t(unique(data[c("xmin", "xmax")])))
Expand All @@ -54,15 +55,15 @@ collide <- function(data, width = NULL, name, strategy,
}

if (!is.null(data$ymax)) {
dapply(data, "xmin", strategy, ..., width = width)
data <- dapply(data, "xmin", strategy, ..., width = width)
} else if (!is.null(data$y)) {
data$ymax <- data$y
data <- dapply(data, "xmin", strategy, ..., width = width)
data$y <- data$ymax
data
} else {
abort("Neither y nor ymax defined")
}
data[match(seq_along(ord), ord), ]
}

# Alternate version of collide() used by position_dodge2()
Expand Down
2 changes: 1 addition & 1 deletion tests/figs/deps.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- vdiffr-svg-engine: 1.0
- vdiffr: 0.3.0
- vdiffr: 0.3.1
- freetypeharfbuzz: 0.2.5
53 changes: 53 additions & 0 deletions tests/figs/position-stack/area-stacking.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions tests/figs/scales-breaks-and-labels/functional-limits.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 20 additions & 6 deletions tests/testthat/test-position-stack.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
context("position_stack")

test_that("data is sorted prior to stacking", {
test_that("data keeps its order after stacking", {
df <- data_frame(
x = rep(c(1:10), 3),
var = rep(c("a", "b", "c"), 10),
Expand All @@ -9,7 +9,8 @@ test_that("data is sorted prior to stacking", {
p <- ggplot(df, aes(x = x, y = y, fill = var)) +
geom_area(position = "stack")
dat <- layer_data(p)
expect_true(all(dat$group == 3:1))
expect_true(all(dat$group == rep(1:3, each = 10)))
expect_true(all(dat$x == df$x))
})

test_that("negative and positive values are handled separately", {
Expand All @@ -21,8 +22,8 @@ test_that("negative and positive values are handled separately", {
p <- ggplot(df, aes(x, y, fill = factor(g))) + geom_col()
dat <- layer_data(p)

expect_equal(dat$ymin[dat$x == 1], c(0, -1, 1))
expect_equal(dat$ymax[dat$x == 1], c(1, 0, 2))
expect_equal(dat$ymin[dat$x == 1], c(1, -1, 0))
expect_equal(dat$ymax[dat$x == 1], c(2, 0, 1))

expect_equal(dat$ymin[dat$x == 2], c(0, -3))
expect_equal(dat$ymax[dat$x == 2], c(2, 0))
Expand All @@ -49,12 +50,25 @@ test_that("data with no extent is stacked correctly", {
p0 <- base + geom_text(aes(label = y), position = position_stack(vjust = 0))
p1 <- base + geom_text(aes(label = y), position = position_stack(vjust = 1))

expect_equal(layer_data(p0)$y, c(-75, -115))
expect_equal(layer_data(p1)$y, c(0, -75))
expect_equal(layer_data(p0)$y, c(-115, -75))
expect_equal(layer_data(p1)$y, c(-75, 0))
})

test_that("position_stack() can stack correctly when ymax is NA", {
df <- data_frame(x = c(1, 1), y = c(1, 1))
p <- ggplot(df, aes(x, y, ymax = NA_real_)) + geom_point(position = "stack")
expect_equal(layer_data(p)$y, c(1, 2))
})

# Visual tests ------------------------------------------------------------

test_that("Stacking produces the expected output", {
data <- data_frame(
x = rep(1:4, each = 2),
category = rep(c("A","B"), 4),
value = c(0, 0, 2, 1, 3, 6, -4, 3)
)
p <- ggplot(data, aes(x = x, y = value, fill = category)) +
geom_area(stat = "identity")
expect_doppelganger("Area stacking", p)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-position_dodge.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ test_that("can control whether to preserve total or individual width", {
p_single <- ggplot(df, aes(x, fill = y)) +
geom_bar(position = position_dodge(preserve = "single"), width = 1)

expect_equal(layer_data(p_total)$x, c(1, 2.25, 1.75))
expect_equal(layer_data(p_single)$x, c(0.75, 2.25, 1.75))
expect_equal(layer_data(p_total)$x, c(1, 1.75, 2.25))
expect_equal(layer_data(p_single)$x, c(0.75, 1.75, 2.25))
})