Skip to content

Commit

Permalink
weighted_mean_sd and weighted_median_iqr instead of mean_sd and media…
Browse files Browse the repository at this point in the history
…n_iqr

fix #370
  • Loading branch information
larmarange authored and schloerke committed Jun 5, 2020
1 parent 6279186 commit f1699bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ export(mapping_string)
export(mapping_swap_x_y)
export(max1)
export(mean0)
export(mean_sd)
export(median_iqr)
export(min0)
export(model_beta_label)
export(model_beta_variables)
Expand All @@ -118,6 +116,8 @@ export(stat_prop)
export(stat_weighted_mean)
export(uppertriangle)
export(v1_ggmatrix_theme)
export(weighted_mean_sd)
export(weighted_median_iqr)
export(wrap)
export(wrap_fn_with_param_arg)
export(wrap_fn_with_params)
Expand Down
12 changes: 6 additions & 6 deletions R/gg-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ ggally_autopointDiag <- function(data, mapping, ...) {
#' ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), size = 6)
#'
#' # change statistic to display
#' ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), text_fn = mean_sd)
#' ggally_summarise_by(tips, mapping = aes(x = total_bill, y = day), text_fn = weighted_mean_sd)
#'
#' # custom stat function
#' weighted_sum <- function(x, weights = NULL) {
Expand All @@ -1729,7 +1729,7 @@ ggally_autopointDiag <- function(data, mapping, ...) {
ggally_summarise_by <- function(
data,
mapping,
text_fn = median_iqr,
text_fn = weighted_median_iqr,
text_fn_vertical = NULL,
...)
{
Expand Down Expand Up @@ -1794,19 +1794,19 @@ ggally_summarise_by <- function(
#' @param x a numeric vector
#' @param weights an optional numeric vectors of weights. If \code{NULL}, equal weights of 1 will be taken into account.
#' @details
#' \code{median_iqr} computes median and interquartile range.
#' \code{weighted_median_iqr} computes weighted median and interquartile range.
#' @export
median_iqr <- function(x, weights = NULL) {
weighted_median_iqr <- function(x, weights = NULL) {
require_namespaces("Hmisc")
s <- round(Hmisc::wtd.quantile(x, weights = weights, probs = c(.25, .5, .75), na.rm = TRUE), digits = 1)
paste0("Median: ", s[2], " [", s[1], "-", s[3], "]")
}

#' @rdname ggally_summarise_by
#' @details
#' \code{mean_sd} computes mean and standard deviation.
#' \code{weighted_mean_sd} computes weighted mean and standard deviation.
#' @export
mean_sd <- function(x, weights = NULL) {
weighted_mean_sd <- function(x, weights = NULL) {
require_namespaces("Hmisc")
m <- round(Hmisc::wtd.mean(x, weights = weights, na.rm = TRUE), digits = 1)
sd <- round(sqrt(Hmisc::wtd.var(x, weights = weights, na.rm = TRUE)), digits = 1)
Expand Down
16 changes: 8 additions & 8 deletions man/ggally_summarise_by.Rd

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

0 comments on commit f1699bf

Please sign in to comment.