Skip to content

Commit

Permalink
Merge pull request #74 from larmarange/geom_impr
Browse files Browse the repository at this point in the history
some cleaning of internal code
  • Loading branch information
larmarange authored Oct 7, 2024
2 parents 7b8f3de + 705eb25 commit 44a1d88
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 103 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ importFrom(lifecycle,deprecated)
importFrom(scales,percent)
importFrom(stats,weighted.mean)
importFrom(stats,weights)
importFrom(utils,modifyList)
42 changes: 12 additions & 30 deletions R/geom_diverging.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ geom_diverging <- function(mapping = NULL,
position = position,
complete = complete,
default_by = default_by,
stat = StatProp
stat = StatPropCount,
...
)
}

Expand All @@ -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,
...
)
}

Expand All @@ -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,
...
)
}

Expand All @@ -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,
...
)
}

Expand All @@ -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,
...
)
}

Expand All @@ -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,
...
)
}
12 changes: 0 additions & 12 deletions R/ggstats-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 32 additions & 12 deletions R/stat_prop.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
...
)
}

Expand All @@ -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)
)
)
)
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 44a1d88

Please sign in to comment.