diff --git a/NAMESPACE b/NAMESPACE index 7e3ae8b..19507b9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -61,4 +61,3 @@ importFrom(lifecycle,deprecated) importFrom(scales,percent) importFrom(stats,weighted.mean) importFrom(stats,weights) -importFrom(utils,modifyList) diff --git a/R/geom_diverging.R b/R/geom_diverging.R index 28d22aa..7bfb839 100644 --- a/R/geom_diverging.R +++ b/R/geom_diverging.R @@ -74,7 +74,8 @@ geom_diverging <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp + stat = StatPropCount, + ... ) } @@ -92,11 +93,8 @@ geom_likert <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop) - ) + stat = StatPropProp, + ... ) } @@ -114,11 +112,8 @@ geom_pyramid <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop) - ) + stat = StatPropProp, + ... ) } @@ -136,11 +131,8 @@ geom_diverging_text <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - label = after_stat(count), - colour = after_scale(hex_bw(.data$fill)) - ) + stat = StatPropCount, + ... ) } @@ -158,13 +150,8 @@ geom_likert_text <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop), - label = scales::percent(after_stat(prop), accuracy = 1), - colour = after_scale(hex_bw(.data$fill)) - ) + stat = StatPropProp, + ... ) } @@ -182,12 +169,7 @@ geom_pyramid_text <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop), - label = scales::percent(after_stat(prop), accuracy = 1), - colour = after_scale(hex_bw(.data$fill)) - ) + stat = StatPropProp, + ... ) } diff --git a/R/ggstats-package.R b/R/ggstats-package.R index 9561fb6..4b7af1d 100644 --- a/R/ggstats-package.R +++ b/R/ggstats-package.R @@ -32,15 +32,3 @@ utils::globalVariables(c("prop", "count")) b } } - -# helper for changing default aes -# @param Stat a ggproto stat -# @param ... new default aes -#' @importFrom utils modifyList -.modify_default_aes <- function(Stat, ...) { - new_aes <- ggplot2::aes(...) - Stat$default_aes <- ggplot2::aes( - !!!utils::modifyList(StatProp$default_aes, new_aes) - ) - Stat -} diff --git a/R/stat_prop.R b/R/stat_prop.R index 3db4609..5e2bcbe 100644 --- a/R/stat_prop.R +++ b/R/stat_prop.R @@ -249,11 +249,8 @@ geom_prop_bar <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop) - ) + stat = StatPropProp, + ... ) } @@ -272,12 +269,35 @@ geom_prop_text <- function(mapping = NULL, position = position, complete = complete, default_by = default_by, - stat = StatProp |> - .modify_default_aes( - x = after_stat(prop), - y = after_stat(prop), - label = scales::percent(after_stat(prop), accuracy = .1), - colour = after_scale(hex_bw(.data$fill)) - ) + stat = StatPropProp, + ... ) } + +# all defaults to after_stat(prop) +StatPropProp <- ggplot2::ggproto( + "StatPropProp", + StatProp, + default_aes = utils::modifyList( + StatProp$default_aes, + ggplot2::aes( + x = after_stat(prop), + y = after_stat(prop), + label = scales::percent(after_stat(prop), accuracy = .1) + ) + ) +) + +# all defaults to after_stat(count) +StatPropCount <- ggplot2::ggproto( + "StatPropCount", + StatProp, + default_aes = utils::modifyList( + StatProp$default_aes, + ggplot2::aes( + x = after_stat(count), + y = after_stat(count), + label = after_stat(count) + ) + ) +) diff --git a/tests/testthat/_snaps/position_likert/geom-likert-and-geom-likert-text.svg b/tests/testthat/_snaps/position_likert/geom-likert-and-geom-likert-text.svg index 421110f..261f2c8 100644 --- a/tests/testthat/_snaps/position_likert/geom-likert-and-geom-likert-text.svg +++ b/tests/testthat/_snaps/position_likert/geom-likert-and-geom-likert-text.svg @@ -67,46 +67,46 @@ -28% -13% -11% -28% -20% -5% -12% -23% -32% -28% -3% -12% -25% -27% -33% -2% -8% -21% -27% -41% -2% -8% -22% -24% -44% -1% -6% -24% -17% -51% -0% -5% -22% -17% -56% -1% -4% -15% -13% -68% +28.3% +13.0% +11.3% +27.7% +19.7% +5.1% +11.8% +22.8% +32.1% +28.3% +3.1% +11.9% +24.8% +27.4% +32.8% +2.1% +8.0% +21.1% +27.4% +41.4% +2.1% +7.9% +21.7% +24.3% +43.9% +1.4% +5.6% +24.4% +17.2% +51.4% +0.5% +5.1% +21.6% +16.9% +56.0% +0.5% +4.0% +15.0% +12.8% +67.7% diff --git a/tests/testthat/_snaps/position_likert/geom-pyramid-and-geom-pyramid-text.svg b/tests/testthat/_snaps/position_likert/geom-pyramid-and-geom-pyramid-text.svg index e2f6f7c..9457c68 100644 --- a/tests/testthat/_snaps/position_likert/geom-pyramid-and-geom-pyramid-text.svg +++ b/tests/testthat/_snaps/position_likert/geom-pyramid-and-geom-pyramid-text.svg @@ -35,14 +35,14 @@ -8% -7% -8% -5% -23% -9% -39% -1% +8.2% +6.6% +8.1% +4.8% +23.2% +8.9% +39.2% +1.0%