Skip to content

Commit

Permalink
some fixes in stat_prop()
Browse files Browse the repository at this point in the history
separate option to specify labels

geom_*_pyramid() should use proportion of the total
  • Loading branch information
larmarange committed Aug 21, 2024
1 parent 38a990a commit 287167f
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 52 deletions.
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* new sorting option `"prop_lower"` for `gglikert()` (#62)
* new helper `symmetric_limits()` to make a scale symmetric (#66)
* new argument `symmetric` for `gglikert()` (#66)
* new arguments `default_by`, `height` and `height_labeller` for `stat_prop()`
(#69)
* new arguments `default_by`, `height`, `labels` and `labeller` for
`stat_prop()` (#69)
* new geometries: `geom_bar_diverging()`, `geom_bar_likert()`,
`geom_bar_pyramid()` and `geom_text_diverging()`, `geom_text_likert()`,
`geom_text_pyramid()`(#69)
`geom_text_pyramid()` (#69)
* new geometries: `geom_bar_prop()` and `geom_text_prop()` (#69)

# ggstats 0.6.0
Expand Down
31 changes: 19 additions & 12 deletions R/geom_bar_diverging.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#'
#' - `geom_bar_diverging()` is designed for stacked diverging bar plots, using
#' [`position_diverging()`].
#' - `geom_bar_diverging()` is designed for Likert-type items. Using
#' - `geom_bar_likert()` is designed for Likert-type items. Using
#' `position_likert()` (each bar sums to 100%).
#' - `geom_bar_pyramid()` is adapted for population pyramid plots when a factor
#' with two levels is mapped to the **fill** aesthetics. The proportions are,
#' here by default, computed separately for each value of the **fill**
#' aesthetics.
#' - `geom_bar_pyramid()` is similar to `geom_bar_diverging()` but uses
#' proportions of the total instead of counts.
#'
#' To add labels on the bar plots, simply use `geom_text_diverging()`,
#' `geom_text_likert()`, or `geom_text_pyramid()`.
#'
#' @param mapping Optional set of aesthetic mappings.
#' @param data The data to be displayed in this layers.
Expand All @@ -23,7 +24,9 @@
#' see [`stat_prop()`]. Passed only if `stat = "prop"`.
#' @param height Statistic used, by default, to determine the height/width,
#' see [`stat_prop()`]. Passed only if `stat = "prop"`.
#' @param height_labeller Labeller function to format proportions,
#' @param height Statistic used, by default, to determine the labels,
#' see [`stat_prop()`]. Passed only if `stat = "prop"`.
#' @param labeller Labeller function to format labels,
#' see [`stat_prop()`]. Passed only if `stat = "prop"`.
#' @inheritParams position_likert
#' @param width Bar width.
Expand Down Expand Up @@ -120,7 +123,7 @@ geom_bar_pyramid <- function(mapping = NULL,
),
...,
complete = NULL,
default_by = "fill",
default_by = "total",
height = "prop",
reverse = FALSE,
exclude_fill_values = NULL,
Expand All @@ -146,7 +149,8 @@ geom_text_diverging <- function(mapping = NULL,
complete = "fill",
default_by = "total",
height = "count",
height_labeller = label_number_abs(),
labels = "count",
labeller = label_number_abs(),
reverse = FALSE,
exclude_fill_values = NULL,
cutoff = NULL,
Expand All @@ -157,7 +161,8 @@ geom_text_diverging <- function(mapping = NULL,
args$complete <- complete
args$default_by <- default_by
args$height <- height
args$height_labeller <- height_labeller
args$labels <- labels
args$labeller <- labeller
}

args$mapping <- mapping
Expand All @@ -182,7 +187,8 @@ geom_text_likert <- function(mapping = NULL,
complete = "fill",
default_by = "x",
height = "prop",
height_labeller = label_percent_abs(accuracy = 1),
labels = "prop",
labeller = label_percent_abs(accuracy = 1),
reverse = FALSE,
exclude_fill_values = NULL,
cutoff = NULL,
Expand All @@ -205,9 +211,10 @@ geom_text_pyramid <- function(mapping = NULL,
),
...,
complete = NULL,
default_by = "fill",
default_by = "total",
height = "prop",
height_labeller = label_percent_abs(accuracy = 1),
labels = "prop",
labeller = label_percent_abs(accuracy = 1),
reverse = FALSE,
exclude_fill_values = NULL,
cutoff = NULL,
Expand Down
31 changes: 20 additions & 11 deletions R/stat_prop.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
#' @param height Which statistic (`"count"` or `"prop"`) should be used, by
#' default, for determining the height/width of the geometry (accessible
#' through `after_stat(height)`)?
#' @param height_labeller Labeller function to format heights and populate
#' `after_stat(labelled_height)`.
#' @param labels Which statistic (`"prop"` or `"count"`) should be used, by
#' default, for generating formatted labels (accessible through
#' `after_stat(labels)`)?
#' @param labeller Labeller function to format labels and populate
#' `after_stat(labels)`.
#'
#' @section Aesthetics:
#' `stat_prop()` understands the following aesthetics
Expand All @@ -32,8 +35,8 @@
#' \item{`after_stat(count)`}{number of points in bin}
#' \item{`after_stat(prop)`}{computed proportion}
#' \item{`after_stat(height)`}{counts or proportions, according to `height`}
#' \item{`after_stat(labelled_height)`}{formatted heights, according to
#' `height_labeller`}
#' \item{`after_stat(labels)`}{formatted heights, according to `labels` and
#' `labeller`}
#' }
#' @seealso `vignette("stat_prop")`, [ggplot2::stat_count()]. For an alternative
#' approach, see
Expand Down Expand Up @@ -103,15 +106,17 @@ stat_prop <- function(mapping = NULL,
complete = NULL,
default_by = "total",
height = c("count", "prop"),
height_labeller = scales::label_percent(accuracy = .1)) {
labels = c("prop", "count"),
labeller = scales::label_percent(accuracy = .1)) {
params <- list(
na.rm = na.rm,
orientation = orientation,
width = width,
complete = complete,
default_by = default_by,
height = height,
height_labeller = height_labeller,
labels = labels,
labeller = labeller,
...
)
if (!is.null(params$y)) {
Expand Down Expand Up @@ -143,7 +148,7 @@ StatProp <- ggplot2::ggproto("StatProp", ggplot2::Stat,
x = after_stat(height),
y = after_stat(height),
weight = 1,
label = after_stat(labelled_height),
label = after_stat(labels),
by = 1
),
setup_params = function(data, params) {
Expand Down Expand Up @@ -176,9 +181,11 @@ StatProp <- ggplot2::ggproto("StatProp", ggplot2::Stat,
complete = NULL,
default_by = "total",
height = c("count", "prop"),
height_labeller =
labels = c("prop", "count"),
labeller =
scales::label_percent(accuracy = .1)) {
height <- match.arg(height)
labels <- match.arg(labels)
data <- ggplot2::flip_data(data, flipped_aes)
data$weight <- data$weight %||% rep(1, nrow(data))

Expand Down Expand Up @@ -223,7 +230,7 @@ StatProp <- ggplot2::ggproto("StatProp", ggplot2::Stat,
}
panel$prop <- panel$count / ave(panel$count, panel$by, FUN = sum_abs)
panel$height <- panel[[height]]
panel$labelled_height <- height_labeller(panel$height)
panel$labels <- labeller(panel[[labels]])
panel$width <- width
panel$flipped_aes <- flipped_aes

Expand Down Expand Up @@ -267,15 +274,17 @@ geom_text_prop <- function(mapping = NULL,
complete = NULL,
default_by = "x",
height = "prop",
height_labeller =
labels = "prop",
labeller =
scales::label_percent(accuracy = .1)) {

args <- list(...)
if (stat == "prop") {
args$complete <- complete
args$default_by <- default_by
args$height <- height
args$height_labeller <- height_labeller
args$labels <- labels
args$labeller <- labeller
}

args$mapping <- mapping
Expand Down
28 changes: 16 additions & 12 deletions man/geom_bar_diverging.Rd

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

18 changes: 12 additions & 6 deletions man/stat_prop.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/stat_prop/stat-prop-by-character.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/stat_prop/stat-prop-direct-call.svg
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.
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.
Loading

0 comments on commit 287167f

Please sign in to comment.