Skip to content
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

stat_count() respects uniqueness of x #5520

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 4 additions & 5 deletions R/position-stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,10 @@ PositionStack <- ggproto("PositionStack", Position,
ymax = as.numeric(ifelse(data$ymax == 0, data$ymin, data$ymax))
)

data <- remove_missing(
data,
vars = c("x", "xmin", "xmax", "y"),
name = "position_stack"
)
vars <- intersect(c("x", "xmin", "xmax", "y"), names(data))
missing <- detect_missing(data, vars)
data[missing, vars] <- NA

flip_data(data, params$flipped_aes)
},

Expand Down
3 changes: 1 addition & 2 deletions R/stat-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ StatCount <- ggproto("StatCount", Stat,
x <- data$x
weight <- data$weight %||% rep(1, length(x))

count <- as.numeric(tapply(weight, x, sum, na.rm = TRUE))
count[is.na(count)] <- 0
count <- as.vector(rowsum(weight, x, na.rm = TRUE))

bars <- data_frame0(
count = count,
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-geom-col.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ test_that("geom_col removes columns with parts outside the plot limits", {
ggplotGrob(p + ylim(0.5, 4)),
"Removed 3 rows containing missing values or values outside the scale range"
)
expect_warning( # warning created at build stage
ggplot_build(p + ylim(0, 2.5)),
expect_warning( # warning created at render stage
ggplotGrob(p + ylim(0, 2.5)),
"Removed 1 row containing missing values or values outside the scale range"
)
})
Expand Down
12 changes: 8 additions & 4 deletions tests/testthat/test-scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ test_that("oob affects position values", {
}
base + scale_y_continuous(limits = c(-0,5))

expect_warning(low_censor <- cdata(base + y_scale(c(0, 5), censor)),
low_censor <- cdata(base + y_scale(c(0, 5), censor))
mid_censor <- cdata(base + y_scale(c(3, 7), censor))
handle <- GeomBar$handle_na

expect_warning(low_censor[[1]] <- handle(low_censor[[1]], list(na.rm = FALSE)),
"Removed 1 row containing missing values or values outside the scale range")
expect_warning(mid_censor <- cdata(base + y_scale(c(3, 7), censor)),
"Removed 2 rows containing missing values or values outside the scale range")
expect_warning(mid_censor[[1]] <- handle(mid_censor[[1]], list(na.rm = FALSE)),
"Removed 3 rows containing missing values or values outside the scale range")

low_squish <- cdata(base + y_scale(c(0, 5), squish))
mid_squish <- cdata(base + y_scale(c(3, 7), squish))
Expand All @@ -127,7 +131,7 @@ test_that("oob affects position values", {

# Bars depend on limits and oob
expect_equal(low_censor[[1]]$y, c(0.2, 1))
expect_equal(mid_censor[[1]]$y, c(0.5))
expect_equal(mid_censor[[1]]$y, numeric(0))
expect_equal(low_squish[[1]]$y, c(0.2, 1, 1))
expect_equal(mid_squish[[1]]$y, c(0, 0.5, 1))
})
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-stat-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ test_that("stat_count() checks the aesthetics", {
p <- ggplot(mtcars) + stat_count(aes(factor(gear), mpg))
expect_snapshot_error(ggplot_build(p))
})

test_that("stat_count() respects uniqueness of `x`", {
# For #4609, converting x to factor loses smallest digits, so here we test
# if they are retained
df <- data_frame0(x = c(1, 2, 1, 2) + rep(c(0, 1.01 * .Machine$double.eps), each = 2))
p <- ggplot(df, aes(x)) + stat_count(position = "identity")
data <- layer_data(p)

expect_length(vec_unique(df$x), 4)
expect_equal(data$y, rep(1, 4))
})
Loading