@@ -76,6 +76,8 @@ stat_ellipse <- function(mapping = NULL, data = NULL,
76
76
# ' @export
77
77
StatEllipse <- ggproto(" StatEllipse" , Stat ,
78
78
required_aes = c(" x" , " y" ),
79
+ optional_aes = " weight" ,
80
+ dropped_aes = " weight" ,
79
81
80
82
setup_params = function (data , params ) {
81
83
params $ type <- params $ type %|| % " t"
@@ -96,6 +98,9 @@ calculate_ellipse <- function(data, vars, type, level, segments){
96
98
dfn <- 2
97
99
dfd <- nrow(data ) - 1
98
100
101
+ weight <- data $ weight %|| % rep(1 , nrow(data ))
102
+ weight <- weight / sum(weight )
103
+
99
104
if (! type %in% c(" t" , " norm" , " euclid" )) {
100
105
cli :: cli_inform(" Unrecognized ellipse type" )
101
106
ellipse <- matrix (NA_real_ , ncol = 2 )
@@ -104,11 +109,12 @@ calculate_ellipse <- function(data, vars, type, level, segments){
104
109
ellipse <- matrix (NA_real_ , ncol = 2 )
105
110
} else {
106
111
if (type == " t" ) {
107
- v <- MASS :: cov.trob(data [,vars ])
112
+ # Prone to convergence problems when `sum(weight) != nrow(data)`
113
+ v <- MASS :: cov.trob(data [,vars ], wt = weight * nrow(data ))
108
114
} else if (type == " norm" ) {
109
- v <- stats :: cov.wt(data [,vars ])
115
+ v <- stats :: cov.wt(data [,vars ], wt = weight )
110
116
} else if (type == " euclid" ) {
111
- v <- stats :: cov.wt(data [,vars ])
117
+ v <- stats :: cov.wt(data [,vars ], wt = weight )
112
118
v $ cov <- diag(rep(min(diag(v $ cov )), 2 ))
113
119
}
114
120
shape <- v $ cov
0 commit comments