Skip to content

Commit

Permalink
allow default subguides and subscales to be set, closes #225
Browse files Browse the repository at this point in the history
  • Loading branch information
mjskay committed Apr 6, 2024
1 parent 1128fae commit 1b6fe2c
Show file tree
Hide file tree
Showing 31 changed files with 297 additions and 84 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -266,10 +266,13 @@ export(stat_slabinterval)
export(stat_spike)
export(subguide_axis)
export(subguide_count)
export(subguide_dots)
export(subguide_inside)
export(subguide_integer)
export(subguide_none)
export(subguide_outside)
export(subguide_slab)
export(subguide_spike)
export(subscale_identity)
export(subscale_thickness)
export(theme_ggdist)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -10,6 +10,9 @@ New features and enhancements:
* More arguments to `stat_slabinterval()` now use `waiver()`s so that they
can be set either in the call to the stat or in the call to the density
estimator (#220).
* Default subscales and subguides can be set by assigning `subscale_thickness`,
`subguide_slab`, `subguide_dots`, and `subguide_spike` in the global
environment (#225).

Bug fixes:

Expand Down
2 changes: 1 addition & 1 deletion R/geom_dotsinterval.R
Expand Up @@ -12,7 +12,7 @@ dots_grob = function(data, x, y, xscale = 1,
name = NULL, gp = gpar(), vp = NULL,
dotsize = 1.07, stackratio = 1, binwidth = NA, layout = "bin",
overlaps = "nudge", overflow = "warn",
subguide = "none",
subguide = "dots",
verbose = FALSE,
orientation = "vertical",
make_points_grob = make_points_grob
Expand Down
17 changes: 12 additions & 5 deletions R/geom_slabinterval.R
Expand Up @@ -173,11 +173,11 @@ draw_slabs = function(
}
})

subguide_grobs = if (identical(subguide, "none")) {
subguide_fun = match_function(subguide, "subguide_")
subguide_grobs = if (identical(subguide_fun(numeric()), zeroGrob())) {
# quick exit, also avoid errors for multiple non-equal axes when not drawing them
list()
} else {
subguide_fun = match_function(subguide, "subguide_")
subguide_params = coord$transform(subguide_params, panel_params)
dlply_(subguide_params, c(y, "side", "justification", "scale"), function(d) {
d$group = NULL
Expand Down Expand Up @@ -601,7 +601,10 @@ GeomSlabinterval = ggproto("GeomSlabinterval", AbstractGeom,
of values to be scaled and then returns a [thickness] vector representing
the scaled values, such as [subscale_thickness()] or [subscale_identity()].
\\item A string giving the name of such a function when prefixed
with `"subscale_"`; e.g. `"thickness"` or `"identity"`.
with `"subscale_"`; e.g. `"thickness"` or `"identity"`. The value
`"thickness"` using the default subscale, which can be modified by
setting [`subguide_thickness`]; see the documentation for that
function.
}
For a comprehensive discussion and examples of slab scaling and normalization, see the
[`thickness` scale article](https://mjskay.github.io/ggdist/articles/thickness.html).
Expand Down Expand Up @@ -684,7 +687,11 @@ GeomSlabinterval = ggproto("GeomSlabinterval", AbstractGeom,
[subguide_none()] (to draw no annotation). See [subguide_axis()]
for a list of possibilities and examples.
\\item A string giving the name of such a function when prefixed
with `"subguide_"`; e.g. `"axis"` or `"none"`.
with `"subguide_"`; e.g. `"axis"` or `"none"`. The values `"slab"`,
`"dots"`, and `"spike"` use the default subguide for their geom
families (no subguide), which can be modified by setting
[`subguide_slab`], [`subguide_dots`], or [`subguide_spike`];
see the documentation for those functions.
}
')
), AbstractGeom$param_docs),
Expand All @@ -701,7 +708,7 @@ GeomSlabinterval = ggproto("GeomSlabinterval", AbstractGeom,
show_slab = TRUE,
show_point = TRUE,
show_interval = TRUE,
subguide = "none",
subguide = "slab",
na.rm = FALSE
),

Expand Down
4 changes: 4 additions & 0 deletions R/geom_spike.R
Expand Up @@ -170,6 +170,10 @@ GeomSpike = ggproto("GeomSpike", GeomSlab,
arrow = '[grid::arrow()] giving the arrow heads to use on the spike, or `NULL` for no arrows.'
), GeomSlab$param_docs),

default_params = defaults(list(
subguide = "spike"
), GeomSlab$default_params),

hidden_params = setdiff(
union("fill_type", GeomSlab$hidden_params),
"arrow"
Expand Down
44 changes: 35 additions & 9 deletions R/subguide.R
@@ -1,8 +1,8 @@
#' Axis sub-guide for thickness scales
#'
#' This is a sub-guide intended for annotating the `thickness` aesthetic
#' in \pkg{ggdist}. It can be used with the `subguide` parameter of
#' [geom_slabinterval()].
#' This is a sub-guide intended for annotating the `thickness` and dot-count
#' subscales in \pkg{ggdist}. It can be used with the `subguide` parameter of
#' [geom_slabinterval()] and [geom_dotsinterval()].
#' @template description-auto-partial-waivable
#'
#' @inheritParams scale_thickness
Expand Down Expand Up @@ -93,7 +93,7 @@ subguide_axis = auto_partial(name = "subguide_axis", function(
grob_width = switch(width., width = grobWidth, height = grobHeight)
position = get_subguide_position(position, orientation)

limits = range(values)
limits = suppressWarnings(range(values))
scale = scale_thickness_shared(breaks = breaks, labels = labels, limits = limits)
scale$train(values)

Expand Down Expand Up @@ -192,16 +192,42 @@ subguide_count = function(..., breaks = scales::breaks_width(1)) {

#' Empty sub-guide for thickness scales
#'
#' This is a blank sub-guide that omits annotations for the `thickness` aesthetic
#' in \pkg{ggdist}. It can be used with the `subguide` parameter of
#' [geom_slabinterval()].
#' This is a blank sub-guide that omits annotations for the `thickness` and
#' dot-count sub-scales in \pkg{ggdist}. It can be used with the `subguide`
#' parameter of [geom_slabinterval()] and [geom_dotsinterval()].
#' @template description-auto-partial-waivable
#'
#' @param ... ignored.
#' @family sub-guides
#' @export
subguide_none = function(...) {
subguide_none = auto_partial(name = "subguide_none", function(values, ...) {
zeroGrob()
}
})

#' @details
#' [subguide_slab()], [subguide_dots()], and [subguide_spike()] are aliases
#' for [subguide_none()] that allow you to change the default subguide used
#' for the [geom_slabinterval()], [geom_dotsinterval()], and [geom_spike()]
#' families. If you overwrite these in the global environment, you can set
#' the corresponding default subguide. For example:
#'
#' ```r
#' subguide_slab = ggdist::subguide_inside(position = "right")
#' ```
#'
#' This will cause [geom_slabinterval()]s to default to having a guide on the
#' right side of the geom.
#' @rdname subguide_axis
#' @export
subguide_slab = subguide_none

#' @rdname subguide_axis
#' @export

subguide_dots = subguide_none
#' @rdname subguide_axis
#' @export
subguide_spike = subguide_none


# helpers -----------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions R/subscale.R
Expand Up @@ -15,6 +15,18 @@
#' - A function that accepts a length-2 [numeric] vector of the automatic
#' limits and returns new limits. Unlike positional scales. Unlike
#' positional scales, these limits will not remove data.
#'
#' @details
#' You can overwrite `subscale_thickness` in the global environment to set
#' the default properties of the thickness subscale. For example:
#'
#' ```r
#' subscale_thickness = ggdist::subscale_thickness(expand = expansion(c(0, 0.05)))
#' ```
#'
#' This will cause [geom_slabinterval()]s to default to a thickness subscale
#' that expands by 5% at the top of the scale. **Always** prefix such a
#' definition with `ggdist::` to avoid infinite loops caused by recursion.
#' @returns A [thickness] vector of the same length as `x` scaled to be between
#' `0` and `1`.
#' @family sub-scales
Expand Down
8 changes: 6 additions & 2 deletions man/geom_blur_dots.Rd

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

8 changes: 6 additions & 2 deletions man/geom_dots.Rd

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

8 changes: 6 additions & 2 deletions man/geom_dotsinterval.Rd

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

13 changes: 10 additions & 3 deletions man/geom_slab.Rd

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

13 changes: 10 additions & 3 deletions man/geom_slabinterval.Rd

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

35 changes: 21 additions & 14 deletions man/geom_spike.Rd

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

0 comments on commit 1b6fe2c

Please sign in to comment.