Skip to content

Commit

Permalink
wrap weight aesthetic into the dist aesthetic for #41
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Jan 5, 2024
1 parent 18465ee commit d967578
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ importFrom(distributional,generate)
importFrom(distributional,hdr)
importFrom(distributional,is_distribution)
importFrom(distributional,support)
importFrom(distributional,variance)
importFrom(dplyr,"%>%")
importFrom(dplyr,across)
importFrom(dplyr,arrange_at)
Expand Down
2 changes: 1 addition & 1 deletion R/density.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ density_histogram = auto_partial(name = "density_histogram", function(
x_label = as_label(enexpr(x))
x = check_na(x, na.rm)

h = weighted_hist(x, breaks = breaks, align = align)
h = weighted_hist(x, weights = weights, breaks = breaks, align = align)
input_1 = h$breaks[-length(h$breaks)] # first edge of bin
input_2 = h$breaks[-1] # second edge of bin
input_ = (input_1 + input_2)/2 # center of bin
Expand Down
3 changes: 2 additions & 1 deletion R/distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ distr_is_factor_like = function(dist) {
inherits(dist, "rvar_factor") || if (inherits(dist, "distribution")) {
is_factor_like = map_lgl_(vctrs::vec_data(dist), function(d) {
inherits(d, c("dist_categorical", "ggdist__wrapped_categorical")) ||
(inherits(d, "dist_sample") && inherits(distr_get_sample(d), c("character", "factor"))) ||
(inherits(d, c("dist_sample", "ggdist__weighted_sample")) && inherits(distr_get_sample(d), c("character", "factor"))) ||
is.character(vctrs::field(support(vec_restore(list(d), dist_missing())), "x")[[1]])
})
length(dist) > 0 && all(is_factor_like)
Expand Down Expand Up @@ -445,6 +445,7 @@ mean.ggdist__weighted_sample = function(x, ...) {
}
}

#' @importFrom distributional variance
#' @export
variance.ggdist__weighted_sample = function(x, ...) {
if (is.null(x[["weights"]])){
Expand Down
4 changes: 2 additions & 2 deletions R/stat_slabinterval.R
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ StatSlabinterval = ggproto("StatSlabinterval", AbstractStatSlabinterval,
}

# dist aesthetic is not provided but x aesthetic is, and x is not a dist
# this means we need to wrap it as a dist_sample
# this means we need to wrap it as a weighted dist_sample
data = summarise_by(data, c("PANEL", y, "group"), function(d) {
data.frame(dist = dist_sample(list(trans$inverse(d[[x]]))))
data.frame(dist = .dist_weighted_sample(list(trans$inverse(d[[x]])), list(d[["weight"]])))
})
data[[x]] = median(data$dist)
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d967578

Please sign in to comment.