From f1699bfa5dd9d4d85ecac9477ad874301c293cb5 Mon Sep 17 00:00:00 2001 From: Joseph Larmarange Date: Fri, 5 Jun 2020 18:26:35 +0200 Subject: [PATCH] weighted_mean_sd and weighted_median_iqr instead of mean_sd and median_iqr fix #370 --- NAMESPACE | 4 ++-- R/gg-plots.R | 12 ++++++------ man/ggally_summarise_by.Rd | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index c3fc1f922..75977a035 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/gg-plots.R b/R/gg-plots.R index 2bd9b2bb7..2e6018ce9 100644 --- a/R/gg-plots.R +++ b/R/gg-plots.R @@ -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) { @@ -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, ...) { @@ -1794,9 +1794,9 @@ 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], "]") @@ -1804,9 +1804,9 @@ median_iqr <- function(x, weights = NULL) { #' @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) diff --git a/man/ggally_summarise_by.Rd b/man/ggally_summarise_by.Rd index e483dde69..3fbb3bec7 100644 --- a/man/ggally_summarise_by.Rd +++ b/man/ggally_summarise_by.Rd @@ -2,21 +2,21 @@ % Please edit documentation in R/gg-plots.R \name{ggally_summarise_by} \alias{ggally_summarise_by} -\alias{median_iqr} -\alias{mean_sd} +\alias{weighted_median_iqr} +\alias{weighted_mean_sd} \title{Summarize a continuous variable by each value of a discrete variable} \usage{ ggally_summarise_by( data, mapping, - text_fn = median_iqr, + text_fn = weighted_median_iqr, text_fn_vertical = NULL, ... ) -median_iqr(x, weights = NULL) +weighted_median_iqr(x, weights = NULL) -mean_sd(x, weights = NULL) +weighted_mean_sd(x, weights = NULL) } \arguments{ \item{data}{data set using} @@ -37,9 +37,9 @@ mean_sd(x, weights = NULL) Display summary statistics of a continuous variable for each value of a discrete variable. } \details{ -\code{median_iqr} computes median and interquartile range. +\code{weighted_median_iqr} computes weighted median and interquartile range. -\code{mean_sd} computes mean and standard deviation. +\code{weighted_mean_sd} computes weighted mean and standard deviation. } \examples{ data(tips, package = "reshape") @@ -55,7 +55,7 @@ ggally_summarise_by(tips, mapping = aes(x = day, y = total_bill, color = day)) 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) {