geom_debug: Geom that prints input data to console.

View source: R/geom-debug.R

geom_debugR Documentation

Geom that prints input data to console.

Description

The debug geom is used to print to the console a summary of the data being received by geoms as input data data frame.

Usage

geom_debug(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  summary.fun = "head",
  summary.fun.args = list(),
  parse = NULL,
  nudge_x = 0,
  nudge_y = 0,
  position = "identity",
  na.rm = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE,
  ...
)

geom_debug_npc(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  summary.fun = "head",
  summary.fun.args = list(),
  parse = NULL,
  nudge_x = 0,
  nudge_y = 0,
  position = "identity",
  na.rm = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), is combined with the default mapping at the top level of the plot. You only need to supply mapping if there isn't a mapping defined for the plot.

data

A data frame. If specified, overrides the default data frame defined at the top level of the plot.

stat

The statistical transformation to use on the data for this layer, as a string.

summary.fun

The name of a function as a character string (or a function) to be used to summarize the data object received as input before printing it.

summary.fun.args

A list of additional arguments to be passed to summary.fun.

parse

Ignored. Helps avoid warnings.

nudge_x, nudge_y

Horizontal and vertical adjustments to nudge the starting position. The units for nudge_x and nudge_y are the same as for the data units on the x-axis and y-axis.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

...

other arguments passed on to layer. There are three types of arguments you can use here:

  • Aesthetics: to set an aesthetic to a fixed value, like color = "red" or size = 3.

  • Other arguments to the layer, for example you override the default stat associated with the layer.

  • Other arguments passed on to the stat.

Details

It can be useful when debugging the code of statistics or to learn how the stats and geoms work in 'ggplot2' (>= 2.0.0).

The intended use of this geometry is to help explore the data as they are added to a plot layer. It is very unusual in that it does not produce visible graphic output. It only returns a grid.null() grob (graphical object) when the plot is rendered. Also, differently to normal geometries, it passes its data input as argument to the first parameter of the function passed as argument to summary.fun. The value returned by this function is then printed to the R console. If summary.fun = NULL, the whole data object is printed.

Nudging with nudge_x and nudge_y behave as in geom_text. Arguments passed to position are obeyed. So the effects of positions are reflected in the data object printed or summarized to the R console. The argument passed to parse is currently ignored.

Many aesthetics are defined as optional so that they are accepted silently by geom_debug() and handled by 'ggplot2' as usual.

If the argument passed to summary.fun() is a character string, the name will appear in the header of the printout. However, the function must be available at the time the plot is rendered. If a function is passed as argument, its definition will be saved as part of the "gg" object.

Value

The panel function of this geometry always returns a nullGrob, the legend is is also set to nullGrob. This geometry used for its text printing side effect.

See Also

To access data, scales and grobs in a built ggplot, see ggplot_build.

Examples

# echo to the R console \code{data} as received by geoms
ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug()

ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug(summary.fun = "head", summary.fun.args = list(n = 3))

ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug(summary.fun = "nrow")

ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug(summary.fun = "attributes")

# echo to the R console \code{data} as received by geoms
ggplot(mtcars, aes(cyl, mpg, colour = factor(cyl))) +
  stat_summary(fun.data = "mean_se") +
  stat_summary(fun.data = "mean_se", geom = "debug", summary.fun = NULL)

# shape data is not passed to geometries or statistics
if (requireNamespace("sf", quietly = TRUE)) {
  nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

  ggplot(data = nc) +
    geom_sf(color = "darkblue", fill = "white") +
    geom_debug()
}


gginnards documentation built on May 31, 2023, 9:15 p.m.