Skip to content

Allow 'weight' to be dropped in stats #5218

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 4 commits into from
Mar 14, 2023
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Fixed spurious warnings when the `weight` was used in `stat_bin_2d()`,
`stat_boxplot()`, `stat_contour()`, `stat_bin_hex()` and `stat_quantile()`
(@teunbrand, #5216).
* Various type checks and their messages have been standardised
(@teunbrand, #4834).
* The `layer_data()`, `layer_scales()` and `layer_grob()` now have the default
Expand Down
4 changes: 3 additions & 1 deletion R/stat-bin2d.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ StatBin2d <- ggproto("StatBin2d", Stat,
out$density <- out$count / sum(out$count, na.rm = TRUE)
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
out
}
},

dropped_aes = "weight" # No longer available after transformation
)

dual_param <- function(x, default = list(x = NULL, y = NULL)) {
Expand Down
5 changes: 4 additions & 1 deletion R/stat-binhex.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ StatBinhex <- ggproto("StatBinhex", Stat,
out$value <- NULL

out
}
},

# weight is no longer available after transformation
dropped_aes = "weight"
)

2 changes: 1 addition & 1 deletion R/stat-boxplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ StatBoxplot <- ggproto("StatBoxplot", Stat,
non_missing_aes = "weight",
# either the x or y aesthetic will get dropped during
# statistical transformation, depending on the orientation
dropped_aes = c("x", "y"),
dropped_aes = c("x", "y", "weight"),
setup_data = function(self, data, params) {
data <- flip_data(data, params$flipped_aes)
data$x <- data$x %||% 0
Expand Down
6 changes: 4 additions & 2 deletions R/stat-contour.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ StatContour <- ggproto("StatContour", Stat,

required_aes = c("x", "y", "z"),
default_aes = aes(order = after_stat(level)),
dropped_aes = "z", # z gets dropped during statistical transformation
# z and weight get dropped during statistical transformation
dropped_aes = c("z", "weight"),

setup_params = function(data, params) {
params$z.range <- range(data$z, na.rm = TRUE, finite = TRUE)
Expand Down Expand Up @@ -120,7 +121,8 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,

required_aes = c("x", "y", "z"),
default_aes = aes(order = after_stat(level), fill = after_stat(level)),
dropped_aes = "z", # z gets dropped during statistical transformation
# z and weight get dropped during statistical transformation
dropped_aes = c("z", "weight"),

setup_params = function(data, params) {
params$z.range <- range(data$z, na.rm = TRUE, finite = TRUE)
Expand Down
5 changes: 4 additions & 1 deletion R/stat-quantilemethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ StatQuantile <- ggproto("StatQuantile", Stat,
method.args = method.args
)
vec_rbind0(!!!result)
}
},

# weight is no longer available after transformation
dropped_aes = "weight"
)

quant_pred <- function(quantile, data, method, formula, weight, grid,
Expand Down