Skip to contents

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.

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.

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.

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()

#> [1] "Summary (head) of input 'data' to 'draw_panel()':"
#>    colour x    y PANEL group
#> 1 #00BA38 6 21.0     1     2
#> 2 #00BA38 6 21.0     1     2
#> 3 #F8766D 4 22.8     1     1
#> 4 #00BA38 6 21.4     1     2
#> 5 #619CFF 8 18.7     1     3
#> 6 #00BA38 6 18.1     1     2

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

#> [1] "Summary (head) of input 'data' to 'draw_panel()':"
#>    colour x    y PANEL group
#> 1 #00BA38 6 21.0     1     2
#> 2 #00BA38 6 21.0     1     2
#> 3 #F8766D 4 22.8     1     1

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

#> [1] "Summary (nrow) of input 'data' to 'draw_panel()':"
#> [1] 32

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

#> [1] "Summary (attributes) of input 'data' to 'draw_panel()':"
#> $names
#> [1] "colour" "x"      "y"      "PANEL"  "group" 
#> 
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#> [26] 26 27 28 29 30 31 32
#> 

# 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)

#> [1] "Input 'data' to 'draw_panel()':"
#>    colour x group        y     ymin     ymax PANEL flipped_aes
#> 1 #F8766D 4     1 26.66364 25.30387 28.02340     1       FALSE
#> 2 #00BA38 6     2 19.74286 19.19346 20.29225     1       FALSE
#> 3 #619CFF 8     3 15.10000 14.41580 15.78420     1       FALSE

# 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()
}

#> [1] "Summary (head) of input 'data' to 'draw_panel()':"
#>   PANEL group
#> 1     1    -1
#> 2     1    -1
#> 3     1    -1
#> 4     1    -1
#> 5     1    -1
#> 6     1    -1