Skip to content
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@
* Standardised the calculation of `width`, which are now implemented as
aesthetics (@teunbrand, #2800).
* Stricter check on `register_theme_elements(element_tree)` (@teunbrand, #6162)
* Added `weight` aesthetic for `stat_ellipse()` (@teunbrand, #5272)

# ggplot2 3.5.1

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.

Loading