Skip to content

Commit

Permalink
add back geom_debug() for backwards compatibility
Browse files Browse the repository at this point in the history
Not doing so would break 'ggpp' anf 'ggpmisc'.
  • Loading branch information
aphalo committed Apr 30, 2024
1 parent be533ca commit 11ed29f
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 132 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(str,ggplot)
export(GeomDebug)
export(GeomDebugGroup)
export(GeomDebugPanel)
export(GeomNull)
Expand All @@ -15,6 +16,7 @@ export(debug_draw_function)
export(delete_layers)
export(drop_vars)
export(extract_layers)
export(geom_debug)
export(geom_debug_group)
export(geom_debug_panel)
export(geom_null)
Expand Down
16 changes: 7 additions & 9 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ editor_options:

# gginnards 0.2.0

**This is a code breaking update in relation to functions `geom_debug()`,
**This is a code breaking update in relation to functions
`stat_debug_group()` and `stat_debug_panel()`. Both the function signature,
returned values and default arguments have changed.** Function `geom_debug()`
has been renamed. The new and updated functions are more polished and flexible
than they were in earlier versions of the package but can still be made to
produce similar, but not identical, output to functions of the same names from
earlier versions.

- Rename `geom_debug()` into `geom_debug_panel()` and add
`geom_debug_group()`.
returned values and default arguments have changed.** The new and updated
functions are more polished and flexible than they were in earlier versions of
the package but can still be made to produce similar, but not identical, output
to functions of the same names from earlier versions.

- Function `geom_debug()` maintained for backwards compatibility.

- Functions `geom_debug_panel()` and `geom_debug_group()` print summaries of
both `data` and `params` objects.
Expand Down
232 changes: 135 additions & 97 deletions R/geom-debug.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
# used only in the file
# defined to avoid duplicating this list
#
optional.aes <-
c(# ggplot2 at 3.5.1
"group", "subgroup", "order",
"x", "y", "z",
"xintercept", "yintercept",
"slope", "intercept",
"sample",
"xend", "yend",
"ymax", "ymin",
"xmax", "xmin",
"middle", "xmiddle",
"lower", "xlower",
"upper", "xupper",
"width", "height",
"label",
"hjust", "vjust",
"colour", "color", "alpha", "fill",
"shape", "size", "linewidth", "stroke",
"linetype", "lineend", "linejoin",
"angle", "radius",
"family", "fontface", "lineheight",
"map_id", "weight",
"geometry",
# ggpp at 0.5.6
"vp.width", "vp.height",
"npcx", "npcy",
# ggrepel at 0.9.5
"point.size", "segment.angle",
"segment.curvature", "segment.ncp",
"segment.shape", "segment.square",
"segment.squareShape", "segment.inflect",
"segment.debug",
"segment.linetype",
"segment.colour", "segment.color",
"segment.size", "segment.alpha",
# ggtext at 0.1.2
"label.colour", "label.size",
"text.colour",
"box.colour", "box.size",
"halign", "valign",
"orientation"
)

# Null geom ---------------------------------------------------------------

#' A null geom or 'no-op' geom.
Expand Down Expand Up @@ -92,47 +138,7 @@ geom_null <- function(mapping = NULL,
GeomNull <-
ggplot2::ggproto("GeomNull", ggplot2::Geom,
# needed to avoid warnings
optional_aes = c(# ggplot2 at 3.5.1
"group", "subgroup", "order",
"x", "y", "z",
"xintercept", "yintercept",
"slope", "intercept",
"sample",
"xend", "yend",
"ymax", "ymin",
"xmax", "xmin",
"middle", "xmiddle",
"lower", "xlower",
"upper", "xupper",
"width", "height",
"label",
"hjust", "vjust",
"colour", "color", "alpha", "fill",
"shape", "size", "linewidth", "stroke",
"linetype", "lineend", "linejoin",
"angle", "radius",
"family", "fontface", "lineheight",
"map_id", "weight",
"geometry",
# ggpp at 0.5.6
"vp.width", "vp.height",
"npcx", "npcy",
# ggrepel at 0.9.5
"point.size", "segment.angle",
"segment.curvature", "segment.ncp",
"segment.shape", "segment.square",
"segment.squareShape", "segment.inflect",
"segment.debug",
"segment.linetype",
"segment.colour", "segment.color",
"segment.size", "segment.alpha",
# ggtext at 0.1.2
"label.colour", "label.size",
"text.colour",
"box.colour", "box.size",
"halign", "valign",
"orientation"
),
optional_aes = optional.aes,
default_aes = ggplot2::aes(),
draw_key = function(...) {
grid::nullGrob()
Expand All @@ -144,51 +150,6 @@ GeomNull <-
}
)

# used only in the file
# defined to avoid duplicating this list
optional.aes <-
c(# ggplot2 at 3.5.1
"group", "subgroup", "order",
"x", "y", "z",
"xintercept", "yintercept",
"slope", "intercept",
"sample",
"xend", "yend",
"ymax", "ymin",
"xmax", "xmin",
"middle", "xmiddle",
"lower", "xlower",
"upper", "xupper",
"width", "height",
"label",
"hjust", "vjust",
"colour", "color", "alpha", "fill",
"shape", "size", "linewidth", "stroke",
"linetype", "lineend", "linejoin",
"angle", "radius",
"family", "fontface", "lineheight",
"map_id", "weight",
"geometry",
# ggpp at 0.5.6
"vp.width", "vp.height",
"npcx", "npcy",
# ggrepel at 0.9.5
"point.size", "segment.angle",
"segment.curvature", "segment.ncp",
"segment.shape", "segment.square",
"segment.squareShape", "segment.inflect",
"segment.debug",
"segment.linetype",
"segment.colour", "segment.color",
"segment.size", "segment.alpha",
# ggtext at 0.1.2
"label.colour", "label.size",
"text.colour",
"box.colour", "box.size",
"halign", "valign",
"orientation"
)

# Debug geom --------------------------------------------------------------

#' Geoms that print input data to console.
Expand All @@ -207,15 +168,16 @@ optional.aes <-
#' \code{mapping} if there isn't a mapping defined for the plot.
#' @param data A data frame. If specified, overrides the default data frame
#' defined at the top level of the plot.
#' @param dbgfun.data,dbgfun.params The functions as character strings giving
#' their names or as named or anonymous function objects, to be used to
#' summarize the \code{data} and the \code{params} objects received as input
#' by the geometry.
#' @param dbgfun.data.args,dbgfun.params.args A named list of additional
#' arguments to be passed to \code{dbgfun.data} and \code{dbgfun.params}.
#' @param dbgfun.data,dbgfun.params,summary.fun The functions as character
#' strings giving their names or as named or anonymous function objects, to be
#' used to summarize the \code{data} and the \code{params} objects received as
#' input by the geometry.
#' @param dbgfun.data.args,dbgfun.params.args,summary.fun.args A named list of
#' additional arguments to be passed to \code{dbgfun.data} and
#' \code{dbgfun.params}.
#' @param dbgfun.print A function used to print the \code{data} object received
#' as input.
#' @param dbgfun.print.args A named list.
#' @param dbgfun.print.args A named list. Currently ignored!
#' @param position Position adjustment, either as a string, or the result of a
#' call to a position adjustment function.
#' @param stat The statistical transformation to use on the data for this layer,
Expand Down Expand Up @@ -246,12 +208,14 @@ optional.aes <-
#' \code{\link[grid]{nullGrob}}. This geometry used for its text printing side
#' effect.
#'
#' @details The intended use of this geometry is to help explore the data as
#' @details The intended use of \code{geom_debug_panel()} and
#' \code{geom_debug_group()} is to explore the data as
#' they are used in a plot layer to produce graphical output. Geometries
#' can be defined using draw functions that receive as input data coresponding
#' can be defined using draw functions that receive as input data corresponding
#' to a single group at a time, or draw functions that receive as input all
#' data to be drawn in a panel at a time, possibly including multiple
#' groups.
#' groups. Function \code{geom_debug()} is identical to
#' \code{geom_debug_panel()}, and included for backwards compatibility.
#'
#' These \emph{debug} geoms are very unusual in that they do not produce
#' visible graphic output. They "draw" a \code{grid.null()} grob (graphical
Expand Down Expand Up @@ -346,6 +310,15 @@ optional.aes <-
#' geom_debug_panel()
#' }
#'
#' # backwards compatibility
#' ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
#' geom_point() +
#' geom_debug()
#'
#' ggplot(mtcars, aes(cyl, mpg, colour = factor(cyl))) +
#' stat_summary(fun.data = "mean_se") +
#' stat_summary(geom = "debug", fun.data = "mean_se")
#'
geom_debug_panel <- function(mapping = NULL,
data = NULL,
stat = "identity",
Expand Down Expand Up @@ -392,6 +365,55 @@ geom_debug_panel <- function(mapping = NULL,
)
}

# For backwards compatibility we fake the old function signature and name
#
#' @rdname geom_debug_panel
#'
#' @export
#'
geom_debug <- function(mapping = NULL,
data = NULL,
stat = "identity",
summary.fun = "head",
summary.fun.args = list(),
parse = NULL,
orientation = NULL,
nudge_x = 0,
nudge_y = 0,
position = "identity",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
...) {
if (!missing(nudge_x) || !missing(nudge_y)) {
if (!missing(position) && position != "identity") {
rlang::abort("You must specify either `position` or `nudge_x`/`nudge_y`.")
}
# by default we keep the original positions
position <- ggplot2::position_nudge(nudge_x, nudge_y)
}

ggplot2::layer(
geom = GeomDebug,
mapping = mapping,
data = data,
stat = stat,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = rlang::list2(na.rm = na.rm,
...,
dbgfun.data = summary.fun,
dbgfun.data.args = summary.fun.args,
dbgfun.params = NULL,
dbgfun.params.args = NA,
dbgfun.print = print,
dbgfun.print.args = list(),
draw.label = "draw_panel()")
)
}


#' @rdname gginnards-ggproto
#' @format NULL
#' @usage NULL
Expand All @@ -401,7 +423,7 @@ debug_draw_function <- function(data,
coord,
dbgfun.data = "head",
dbgfun.data.args = list(),
dbgfun.params = "summary",
dbgfun.params = NULL,
dbgfun.params.args = list(),
dbgfun.print = "print",
dbgfun.print.args = list(),
Expand Down Expand Up @@ -453,6 +475,22 @@ debug_draw_function <- function(data,
grid::nullGrob()
}

#' @rdname gginnards-ggproto
#' @format NULL
#' @usage NULL
#'
#' @export
GeomDebug <-
ggplot2::ggproto("GeomDebug", ggplot2::Geom,
# needed to avoid warnings
optional_aes = optional.aes,
default_aes = ggplot2::aes(),
draw_panel = debug_draw_function,
draw_key = function(...) {
grid::nullGrob()
}
)

#' @rdname gginnards-ggproto
#' @format NULL
#' @usage NULL
Expand Down
2 changes: 1 addition & 1 deletion R/stat-debug.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @param dbgfun.print A function used to print the summary of the \code{data}
#' object received as input by the statistic, also visible to the geometry,
#' and used by \code{geom_debug()}.
#' @param dbgfun.print.args A named list.
#' @param dbgfun.print.args A named list. Currently ignored!
#' @param position The position adjustment to use for overlapping points on this
#' layer
#' @param show.legend logical. Should this layer be included in the legends?
Expand Down
19 changes: 5 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,9 @@ ggplot(mtcars, aes(cyl, mpg, colour = factor(cyl))) +

#> [1] "PANEL 1; group(s) 1, 2, 3; 'draw_function()' input 'data' (head):"
#> colour x group y ymin ymax PANEL flipped_aes orientation
#> 1 #F8766D 4 1 26.66364 24.11795 29.11000 1 FALSE NA
#> 2 #00BA38 6 2 19.74286 18.70000 20.70036 1 FALSE NA
#> 3 #619CFF 8 3 15.10000 13.83554 16.36464 1 FALSE NA
#> [1] "PANEL 1; group(s) 1, 2, 3; 'draw_function()' input 'params' (summary):"
#> Length Class Mode
#> x 11 ViewScale environment
#> x.sec 11 ViewScale environment
#> x.range 2 -none- numeric
#> y 11 ViewScale environment
#> y.sec 11 ViewScale environment
#> y.range 2 -none- numeric
#> guides 4 Guides environment
#> 1 #F8766D 4 1 26.66364 24.26341 29.21841 1 FALSE NA
#> 2 #00BA38 6 2 19.74286 18.78571 20.65714 1 FALSE NA
#> 3 #619CFF 8 3 15.10000 13.74268 16.37875 1 FALSE NA

We print to the R console `data` as *seen* as input by statistics that
use a *panel function*.
Expand Down Expand Up @@ -286,7 +277,7 @@ citation("gginnards")
#> To cite package 'gginnards' in publications use:
#>
#> Aphalo P (2024). _gginnards: Explore the Innards of 'ggplot2'
#> Objects_. R package version 0.1.2.9000,
#> Objects_. R package version 0.2.0,
#> https://github.com/aphalo/gginnards,
#> <https://www.r4photobiology.info>.
#>
Expand All @@ -296,7 +287,7 @@ citation("gginnards")
#> title = {gginnards: Explore the Innards of 'ggplot2' Objects},
#> author = {Pedro J. Aphalo},
#> year = {2024},
#> note = {R package version 0.1.2.9000, https://github.com/aphalo/gginnards},
#> note = {R package version 0.2.0, https://github.com/aphalo/gginnards},
#> url = {https://www.r4photobiology.info},
#> }
```
Expand Down
Binary file modified man/figures/README-unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-6-2.png
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 11ed29f

Please sign in to comment.