Skip to content

Commit

Permalink
Add miminal working version of `stat_propzero
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmlft committed Nov 21, 2020
1 parent e8bce9c commit ab2eb47
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ URL: https://github.com/milanmlft/MMmotley
BugReports: https://github.com/milanmlft/MMmotley/issues
Imports:
ggplot2,
scales
scales,
dplyr
Suggests:
testthat,
vdiffr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(format_percentage)
export(geom_pval_hist)
export(gg_pval_hist)
export(save_plots)
export(stat_propzero)
importFrom(ggplot2,aes)
importFrom(ggplot2,expansion)
importFrom(ggplot2,geom_histogram)
Expand Down
24 changes: 22 additions & 2 deletions R/stat-propzero.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@
#' @export
#'
#' @examples
stat_propzero <- function(mapping = NULL, ...) {
stat_propzero <- function(mapping = NULL, data = NULL, geom = "text",
position = "identity", na.rm = FALSE,
inherit.aes = TRUE,
...) {
# TODO: replace `layer` with `annotate`?
# Might allow more flexible positioning of text annotation
ggplot2::layer(
stat = StatPropZero, data = data, mapping = mapping, geom = geom,
position = position, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...)
)
}


# FIXME: figure out how to supply `format` arugment to format the proportions
# e.g. as percentages or rounded decimals
# ggproto object for stat_propzero
StatPropZero <- ggplot2::ggproto("StatPropZero", ggplot2::Stat,
required_aes = c("x", "y"),
compute_group = function(data, scales) {
grouped <- dplyr::group_by(data, x)
prop_zero <- dplyr::summarise(grouped, label = mean(y == 0))
data.frame(x = prop_zero$x, y = 0, label = prop_zero$label)
}
)

0 comments on commit ab2eb47

Please sign in to comment.