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

Weighted ellipses #6186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* Added `weight` aesthetic for `stat_ellipse()` (@teunbrand, #5272)
* Custom and raster annotation now respond to scale transformations, and can
use AsIs variables for relative placement (@teunbrand based on
@yutannihilation's prior work, #3120)
Expand Down
13 changes: 10 additions & 3 deletions R/stat-ellipse.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @param segments The number of segments to be used in drawing the ellipse.
#' @inheritParams layer
#' @inheritParams geom_point
#' @eval rd_aesthetics("stat", "ellipse")
#' @export
#' @examples
#' ggplot(faithful, aes(waiting, eruptions)) +
Expand Down Expand Up @@ -76,6 +77,8 @@
#' @export
StatEllipse <- ggproto("StatEllipse", Stat,
required_aes = c("x", "y"),
optional_aes = "weight",
dropped_aes = "weight",

setup_params = function(data, params) {
params$type <- params$type %||% "t"
Expand All @@ -96,6 +99,9 @@
dfn <- 2
dfd <- nrow(data) - 1

weight <- data$weight %||% rep(1, nrow(data))
weight <- weight / sum(weight)

if (!type %in% c("t", "norm", "euclid")) {
cli::cli_inform("Unrecognized ellipse type")
ellipse <- matrix(NA_real_, ncol = 2)
Expand All @@ -104,11 +110,12 @@
ellipse <- matrix(NA_real_, ncol = 2)
} else {
if (type == "t") {
v <- MASS::cov.trob(data[,vars])
# Prone to convergence problems when `sum(weight) != nrow(data)`
v <- MASS::cov.trob(data[,vars], wt = weight * nrow(data))
} else if (type == "norm") {
v <- stats::cov.wt(data[,vars])
v <- stats::cov.wt(data[,vars], wt = weight)

Check warning on line 116 in R/stat-ellipse.R

View check run for this annotation

Codecov / codecov/patch

R/stat-ellipse.R#L116

Added line #L116 was not covered by tests
} else if (type == "euclid") {
v <- stats::cov.wt(data[,vars])
v <- stats::cov.wt(data[,vars], wt = weight)

Check warning on line 118 in R/stat-ellipse.R

View check run for this annotation

Codecov / codecov/patch

R/stat-ellipse.R#L118

Added line #L118 was not covered by tests
v$cov <- diag(rep(min(diag(v$cov)), 2))
}
shape <- v$cov
Expand Down
12 changes: 12 additions & 0 deletions man/stat_ellipse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.