Author: Pedro J. Aphalo
Date: 2025-11-12

# User Guide: 1 Debugging ggplots

### ‘gginnards’ 0.2.0.2

#### Pedro J. Aphalo

#### 2025-11-12

NOTE: The structure and class of the objects returned by `ggplot()` is dependent on the major version of package ‘ggplot2’. Thus, if code in examples is run under a different major version than 4.0.0.9000, used to build this vignette, the values returned by some code examples can differ drastically from those shown below.

## Introduction

The functions described below can be used to explore the data returned and/or received by input by statistics and the data received as input by geoms. This can be useful when debugging scripts, learning how ‘ggplot2’ works and for debugging stats during their development.

The values extracted are “listed” to the R console by means of calls to `head()` or `print()` by default. However, other functions will be used if passed as arguments in the call.

This vignette demonstrates their use, and at the same time gives a brief description of how ggplot objects are structured.

## Preliminaries

We start by loading and attaching the packages.

    library(tibble)
    library(ggplot2)
    library(gginnards)

We generate some artificial data.

    set.seed(4321)
    # generate artificial data
    x <- 1:100
    y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
    my.data <- data.frame(x,
                          y,
                          group = c("A", "B"),
                          y2 = y * c(0.5, 2),
                          block = c("a", "a", "b", "b"))

We change the default theme to an uncluttered one.

    old_theme <- theme_set(theme_bw())

# ggplot construction

Package ‘ggplot2’ defines its own class system, and function `ggplot()` can be considered as a constructor.

    class(ggplot())

    ## [1] "ggplot2::ggplot" "ggplot"          "ggplot2::gg"     "S7_object"
    ## [5] "gg"

These objects contain all the information needed to render a plot into graphical output, but not the rendered plot itself. They are list-like objects with heterogeneous named members.

The structure of objects of class `"ggplot"` can be explored with R’s method `str()` as is the case for any structured R object. Package ‘gginnards’ defines a a specialization of `str()` for class `"ggplot"`. Our `str()` allows us to see the different slots of these special type of lists. The difference with the default `str()` method is in the values of default arguments, and in the ability to control which components or members are displayed.

We will use the `str()` to follow the step by step construction of a `"ggplot"` object.

If we pass no arguments to the `ggplot()` constructor an empty plot will be rendered if we print it.

    p0 <- ggplot()
    p0

Object `p` contains members, but `"data"`, `"layers"`, `"mapping"`, `"theme"` and `"labels"` are empty lists.

    str(p0)

    ## Object size: 185.5 kB
    ## List of 12
    ##  $ data       : list()
    ##  $ layers     : list()
    ##  $ scales     :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
    ##     add: function
    ##     add_defaults: function
    ##     add_missing: function
    ##     backtransform_df: function
    ##     clone: function
    ##     find: function
    ##     get_scales: function
    ##     has_scale: function
    ##     input: function
    ##     map_df: function
    ##     n: function
    ##     non_position_scales: function
    ##     scales: NULL
    ##     set_palettes: function
    ##     train_df: function
    ##     transform_df: function
    ##     super:  <ggproto object: Class ScalesList, gg>
    ##  $ guides     :Classes 'Guides', 'ggproto', 'gg' <ggproto object: Class Guides, gg>
    ##     add: function
    ##     assemble: function
    ##     build: function
    ##     draw: function
    ##     get_custom: function
    ##     get_guide: function
    ##     get_params: function
    ##     get_position: function
    ##     guides: NULL
    ##     merge: function
    ##     missing: <ggproto object: Class GuideNone, Guide, gg>
    ##         add_title: function
    ##         arrange_layout: function
    ##         assemble_drawing: function
    ##         available_aes: any
    ##         build_decor: function
    ##         build_labels: function
    ##         build_ticks: function
    ##         build_title: function
    ##         draw: function
    ##         draw_early_exit: function
    ##         elements: list
    ##         extract_decor: function
    ##         extract_key: function
    ##         extract_params: function
    ##         get_layer_key: function
    ##         hashables: list
    ##         measure_grobs: function
    ##         merge: function
    ##         override_elements: function
    ##         params: list
    ##         process_layers: function
    ##         setup_elements: function
    ##         setup_params: function
    ##         train: function
    ##         transform: function
    ##         super:  <ggproto object: Class GuideNone, Guide, gg>
    ##     package_box: function
    ##     print: function
    ##     process_layers: function
    ##     setup: function
    ##     subset_guides: function
    ##     train: function
    ##     update_params: function
    ##     super:  <ggproto object: Class Guides, gg>
    ##  $ mapping    : <ggplot2::mapping>  Named list()
    ##  $ theme      : <theme>  Named list()
    ##   ..@ complete: logi FALSE
    ##   ..@ validate: logi TRUE
    ##  $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
    ##     aspect: function
    ##     backtransform_range: function
    ##     clip: on
    ##     default: TRUE
    ##     distance: function
    ##     draw_panel: function
    ##     expand: TRUE
    ##     is_free: function
    ##     is_linear: function
    ##     labels: function
    ##     limits: list
    ##     modify_scales: function
    ##     range: function
    ##     ratio: NULL
    ##     render_axis_h: function
    ##     render_axis_v: function
    ##     render_bg: function
    ##     render_fg: function
    ##     reverse: none
    ##     setup_data: function
    ##     setup_layout: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     train_panel_guides: function
    ##     transform: function
    ##     super:  <ggproto object: Class CoordCartesian, Coord, gg>
    ##  $ facet      :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
    ##     attach_axes: function
    ##     attach_strips: function
    ##     compute_layout: function
    ##     draw_back: function
    ##     draw_front: function
    ##     draw_labels: function
    ##     draw_panel_content: function
    ##     draw_panels: function
    ##     finish_data: function
    ##     format_strip_labels: function
    ##     init_gtable: function
    ##     init_scales: function
    ##     map_data: function
    ##     params: list
    ##     set_panel_size: function
    ##     setup_data: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     shrink: TRUE
    ##     train_scales: function
    ##     vars: function
    ##     super:  <ggproto object: Class FacetNull, Facet, gg>
    ##  $ layout     :Classes 'Layout', 'ggproto', 'gg' <ggproto object: Class Layout, gg>
    ##     coord: NULL
    ##     coord_params: list
    ##     facet: NULL
    ##     facet_params: list
    ##     finish_data: function
    ##     get_scales: function
    ##     layout: NULL
    ##     map_position: function
    ##     panel_params: NULL
    ##     panel_scales_x: NULL
    ##     panel_scales_y: NULL
    ##     render: function
    ##     render_labels: function
    ##     reset_scales: function
    ##     resolve_label: function
    ##     setup: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     train_position: function
    ##     super:  <ggproto object: Class Layout, gg>
    ##  $ labels     : <ggplot2::labels>  Named list()
    ##  $ meta       : list()
    ##  $ plot_env   :<environment: R_GlobalEnv>

If we pass an argument to parameter `data` the data is “copied” into the list slot with name `data`. (The copy is not necessarily identical to the argument, here `my.data` as the class and other attributes may be modified. `ggplot()` is a generic method, and especializations for `data` atguments of classes derived or different from `data.frame` can be defined.) As we also map the data to aesthetics, this mapping is stored in slot `maaping`.

    p1 <- ggplot(data = my.data, aes(x, y, colour = group))
    str(p1)

    ## Object size: 192.6 kB
    ## List of 12
    ##  $ data       :'data.frame': 100 obs. of  5 variables:
    ##  $ layers     : list()
    ##  $ scales     :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
    ##     add: function
    ##     add_defaults: function
    ##     add_missing: function
    ##     backtransform_df: function
    ##     clone: function
    ##     find: function
    ##     get_scales: function
    ##     has_scale: function
    ##     input: function
    ##     map_df: function
    ##     n: function
    ##     non_position_scales: function
    ##     scales: NULL
    ##     set_palettes: function
    ##     train_df: function
    ##     transform_df: function
    ##     super:  <ggproto object: Class ScalesList, gg>
    ##  $ guides     :Classes 'Guides', 'ggproto', 'gg' <ggproto object: Class Guides, gg>
    ##     add: function
    ##     assemble: function
    ##     build: function
    ##     draw: function
    ##     get_custom: function
    ##     get_guide: function
    ##     get_params: function
    ##     get_position: function
    ##     guides: NULL
    ##     merge: function
    ##     missing: <ggproto object: Class GuideNone, Guide, gg>
    ##         add_title: function
    ##         arrange_layout: function
    ##         assemble_drawing: function
    ##         available_aes: any
    ##         build_decor: function
    ##         build_labels: function
    ##         build_ticks: function
    ##         build_title: function
    ##         draw: function
    ##         draw_early_exit: function
    ##         elements: list
    ##         extract_decor: function
    ##         extract_key: function
    ##         extract_params: function
    ##         get_layer_key: function
    ##         hashables: list
    ##         measure_grobs: function
    ##         merge: function
    ##         override_elements: function
    ##         params: list
    ##         process_layers: function
    ##         setup_elements: function
    ##         setup_params: function
    ##         train: function
    ##         transform: function
    ##         super:  <ggproto object: Class GuideNone, Guide, gg>
    ##     package_box: function
    ##     print: function
    ##     process_layers: function
    ##     setup: function
    ##     subset_guides: function
    ##     train: function
    ##     update_params: function
    ##     super:  <ggproto object: Class Guides, gg>
    ##  $ mapping    : <ggplot2::mapping> List of 3
    ##  .. $ x     : language ~x
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ y     : language ~y
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ colour: language ~group
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  $ theme      : <theme>  Named list()
    ##   ..@ complete: logi FALSE
    ##   ..@ validate: logi TRUE
    ##  $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
    ##     aspect: function
    ##     backtransform_range: function
    ##     clip: on
    ##     default: TRUE
    ##     distance: function
    ##     draw_panel: function
    ##     expand: TRUE
    ##     is_free: function
    ##     is_linear: function
    ##     labels: function
    ##     limits: list
    ##     modify_scales: function
    ##     range: function
    ##     ratio: NULL
    ##     render_axis_h: function
    ##     render_axis_v: function
    ##     render_bg: function
    ##     render_fg: function
    ##     reverse: none
    ##     setup_data: function
    ##     setup_layout: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     train_panel_guides: function
    ##     transform: function
    ##     super:  <ggproto object: Class CoordCartesian, Coord, gg>
    ##  $ facet      :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
    ##     attach_axes: function
    ##     attach_strips: function
    ##     compute_layout: function
    ##     draw_back: function
    ##     draw_front: function
    ##     draw_labels: function
    ##     draw_panel_content: function
    ##     draw_panels: function
    ##     finish_data: function
    ##     format_strip_labels: function
    ##     init_gtable: function
    ##     init_scales: function
    ##     map_data: function
    ##     params: list
    ##     set_panel_size: function
    ##     setup_data: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     shrink: TRUE
    ##     train_scales: function
    ##     vars: function
    ##     super:  <ggproto object: Class FacetNull, Facet, gg>
    ##  $ layout     :Classes 'Layout', 'ggproto', 'gg' <ggproto object: Class Layout, gg>
    ##     coord: NULL
    ##     coord_params: list
    ##     facet: NULL
    ##     facet_params: list
    ##     finish_data: function
    ##     get_scales: function
    ##     layout: NULL
    ##     map_position: function
    ##     panel_params: NULL
    ##     panel_scales_x: NULL
    ##     panel_scales_y: NULL
    ##     render: function
    ##     render_labels: function
    ##     reset_scales: function
    ##     resolve_label: function
    ##     setup: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     train_position: function
    ##     super:  <ggproto object: Class Layout, gg>
    ##  $ labels     : <ggplot2::labels>  Named list()
    ##  $ meta       : list()
    ##  $ plot_env   :<environment: R_GlobalEnv>

    str(p1, max.level = 2, components = "data")

    ## Object size: 5.3 kB
    ## List of 1
    ##  $ data:'data.frame':    100 obs. of  5 variables:
    ##   ..$ x    : int [1:100] 1 2 3 4 5 ...
    ##   ..$ y    : num [1:100] -27205 -14243 ...
    ##   ..$ group: chr [1:100] "A" "B" ...
    ##   ..$ y2   : num [1:100] -13603 -28485 ...
    ##   ..$ block: chr [1:100] "a" "a" ...

    all.equal(my.data, p1$data)

    ## [1] TRUE

A geometry adds a layer.

    p2 <- p1 + geom_point()
    str(p2)

    ## Object size: 193.3 kB
    ## List of 12
    ##  $ data       :'data.frame': 100 obs. of  5 variables:
    ##  $ layers     :List of 1
    ##  $ scales     :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
    ##     add: function
    ##     add_defaults: function
    ##     add_missing: function
    ##     backtransform_df: function
    ##     clone: function
    ##     find: function
    ##     get_scales: function
    ##     has_scale: function
    ##     input: function
    ##     map_df: function
    ##     n: function
    ##     non_position_scales: function
    ##     scales: list
    ##     set_palettes: function
    ##     train_df: function
    ##     transform_df: function
    ##     super:  <ggproto object: Class ScalesList, gg>
    ##  $ guides     :Classes 'Guides', 'ggproto', 'gg' <ggproto object: Class Guides, gg>
    ##     add: function
    ##     assemble: function
    ##     build: function
    ##     draw: function
    ##     get_custom: function
    ##     get_guide: function
    ##     get_params: function
    ##     get_position: function
    ##     guides: NULL
    ##     merge: function
    ##     missing: <ggproto object: Class GuideNone, Guide, gg>
    ##         add_title: function
    ##         arrange_layout: function
    ##         assemble_drawing: function
    ##         available_aes: any
    ##         build_decor: function
    ##         build_labels: function
    ##         build_ticks: function
    ##         build_title: function
    ##         draw: function
    ##         draw_early_exit: function
    ##         elements: list
    ##         extract_decor: function
    ##         extract_key: function
    ##         extract_params: function
    ##         get_layer_key: function
    ##         hashables: list
    ##         measure_grobs: function
    ##         merge: function
    ##         override_elements: function
    ##         params: list
    ##         process_layers: function
    ##         setup_elements: function
    ##         setup_params: function
    ##         train: function
    ##         transform: function
    ##         super:  <ggproto object: Class GuideNone, Guide, gg>
    ##     package_box: function
    ##     print: function
    ##     process_layers: function
    ##     setup: function
    ##     subset_guides: function
    ##     train: function
    ##     update_params: function
    ##     super:  <ggproto object: Class Guides, gg>
    ##  $ mapping    : <ggplot2::mapping> List of 3
    ##  .. $ x     : language ~x
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ y     : language ~y
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ colour: language ~group
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  $ theme      : <theme>  Named list()
    ##   ..@ complete: logi FALSE
    ##   ..@ validate: logi TRUE
    ##  $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
    ##     aspect: function
    ##     backtransform_range: function
    ##     clip: on
    ##     default: TRUE
    ##     distance: function
    ##     draw_panel: function
    ##     expand: TRUE
    ##     is_free: function
    ##     is_linear: function
    ##     labels: function
    ##     limits: list
    ##     modify_scales: function
    ##     range: function
    ##     ratio: NULL
    ##     render_axis_h: function
    ##     render_axis_v: function
    ##     render_bg: function
    ##     render_fg: function
    ##     reverse: none
    ##     setup_data: function
    ##     setup_layout: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     train_panel_guides: function
    ##     transform: function
    ##     super:  <ggproto object: Class CoordCartesian, Coord, gg>
    ##  $ facet      :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
    ##     attach_axes: function
    ##     attach_strips: function
    ##     compute_layout: function
    ##     draw_back: function
    ##     draw_front: function
    ##     draw_labels: function
    ##     draw_panel_content: function
    ##     draw_panels: function
    ##     finish_data: function
    ##     format_strip_labels: function
    ##     init_gtable: function
    ##     init_scales: function
    ##     map_data: function
    ##     params: list
    ##     set_panel_size: function
    ##     setup_data: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     shrink: TRUE
    ##     train_scales: function
    ##     vars: function
    ##     super:  <ggproto object: Class FacetNull, Facet, gg>
    ##  $ layout     :Classes 'Layout', 'ggproto', 'gg' <ggproto object: Class Layout, gg>
    ##     coord: NULL
    ##     coord_params: list
    ##     facet: NULL
    ##     facet_params: list
    ##     finish_data: function
    ##     get_scales: function
    ##     layout: NULL
    ##     map_position: function
    ##     panel_params: NULL
    ##     panel_scales_x: NULL
    ##     panel_scales_y: NULL
    ##     render: function
    ##     render_labels: function
    ##     reset_scales: function
    ##     resolve_label: function
    ##     setup: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     train_position: function
    ##     super:  <ggproto object: Class Layout, gg>
    ##  $ labels     : <ggplot2::labels>  Named list()
    ##  $ meta       : list()
    ##  $ plot_env   :<environment: R_GlobalEnv>

A `summary()` method that produces a more compact output is available in recent versions of ‘ggplot2’. However, it does not reveal the internal structure of the objects.

    summary(p2)

    ## data: x, y, group, y2, block [100x5]
    ## mapping:  x = ~x, y = ~y, colour = ~group
    ## faceting:  <empty>
    ## -----------------------------------
    ## geom_point: na.rm = FALSE
    ## stat_identity: na.rm = FALSE
    ## position_identity

    str(p2, max.level = 2, components = "mapping")

    ## Object size: 71.4 kB
    ## List of 1
    ##  $ mapping: <ggplot2::mapping> List of 3
    ##  .. $ x     : language ~x
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ y     : language ~y
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ colour: language ~group
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>

    p3 <- p2 + theme_classic()
    str(p3)

    ## Object size: 21.1 MB
    ## List of 12
    ##  $ data       :'data.frame': 100 obs. of  5 variables:
    ##  $ layers     :List of 1
    ##  $ scales     :Classes 'ScalesList', 'ggproto', 'gg' <ggproto object: Class ScalesList, gg>
    ##     add: function
    ##     add_defaults: function
    ##     add_missing: function
    ##     backtransform_df: function
    ##     clone: function
    ##     find: function
    ##     get_scales: function
    ##     has_scale: function
    ##     input: function
    ##     map_df: function
    ##     n: function
    ##     non_position_scales: function
    ##     scales: list
    ##     set_palettes: function
    ##     train_df: function
    ##     transform_df: function
    ##     super:  <ggproto object: Class ScalesList, gg>
    ##  $ guides     :Classes 'Guides', 'ggproto', 'gg' <ggproto object: Class Guides, gg>
    ##     add: function
    ##     assemble: function
    ##     build: function
    ##     draw: function
    ##     get_custom: function
    ##     get_guide: function
    ##     get_params: function
    ##     get_position: function
    ##     guides: NULL
    ##     merge: function
    ##     missing: <ggproto object: Class GuideNone, Guide, gg>
    ##         add_title: function
    ##         arrange_layout: function
    ##         assemble_drawing: function
    ##         available_aes: any
    ##         build_decor: function
    ##         build_labels: function
    ##         build_ticks: function
    ##         build_title: function
    ##         draw: function
    ##         draw_early_exit: function
    ##         elements: list
    ##         extract_decor: function
    ##         extract_key: function
    ##         extract_params: function
    ##         get_layer_key: function
    ##         hashables: list
    ##         measure_grobs: function
    ##         merge: function
    ##         override_elements: function
    ##         params: list
    ##         process_layers: function
    ##         setup_elements: function
    ##         setup_params: function
    ##         train: function
    ##         transform: function
    ##         super:  <ggproto object: Class GuideNone, Guide, gg>
    ##     package_box: function
    ##     print: function
    ##     process_layers: function
    ##     setup: function
    ##     subset_guides: function
    ##     train: function
    ##     update_params: function
    ##     super:  <ggproto object: Class Guides, gg>
    ##  $ mapping    : <ggplot2::mapping> List of 3
    ##  .. $ x     : language ~x
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ y     : language ~y
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  .. $ colour: language ~group
    ##  ..  ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
    ##  $ theme      : <theme> List of 144
    ##  .. $ line                            : <ggplot2::element_line>
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ lineend      : chr "butt"
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : chr "black"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ rect                            : <ggplot2::element_rect>
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ text                            : <ggplot2::element_text>
    ##  ..  ..@ family       : chr ""
    ##  ..  ..@ face         : chr "plain"
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ size         : num 11
    ##  ..  ..@ hjust        : num 0.5
    ##  ..  ..@ vjust        : num 0.5
    ##  ..  ..@ angle        : num 0
    ##  ..  ..@ lineheight   : num 0.9
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 0
    ##  ..  ..@ debug        : logi FALSE
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ title                           : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ point                           : <ggplot2::element_point>
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ shape        : num 19
    ##  ..  ..@ size         : num 1.5
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ stroke       : num 0.5
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ polygon                         : <ggplot2::element_polygon>
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ geom                            : <ggplot2::element_geom>
    ##  ..  ..@ ink        : chr "black"
    ##  ..  ..@ paper      : chr "white"
    ##  ..  ..@ accent     : chr "#3366FF"
    ##  ..  ..@ linewidth  : num 0.5
    ##  ..  ..@ borderwidth: num 0.5
    ##  ..  ..@ linetype   : int 1
    ##  ..  ..@ bordertype : int 1
    ##  ..  ..@ family     : chr ""
    ##  ..  ..@ fontsize   : num 3.87
    ##  ..  ..@ pointsize  : num 1.5
    ##  ..  ..@ pointshape : num 19
    ##  ..  ..@ colour     : NULL
    ##  ..  ..@ fill       : NULL
    ##  .. $ spacing                         : 'simpleUnit' num 5.5points
    ##  ..  ..- attr(*, "unit")= int 8
    ##  .. $ margins                         : <ggplot2::margin> num [1:4] 5.5 5.5 5.5 5.5
    ##  .. $ aspect.ratio                    : NULL
    ##  .. $ axis.title                      : NULL
    ##  .. $ axis.title.x                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 2.75 0 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.x.top                : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 0
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 2.75 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.x.bottom             : NULL
    ##  .. $ axis.title.y                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : num 90
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.75 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.y.left               : NULL
    ##  .. $ axis.title.y.right              : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : num -90
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 2.75
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text                       : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : 'rel' num 0.8
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 2.2 0 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x.top                 : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 0
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 2.2 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x.bottom              : NULL
    ##  .. $ axis.text.y                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 1
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.2 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.y.left                : NULL
    ##  .. $ axis.text.y.right               : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 2.2
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.theta                 : NULL
    ##  .. $ axis.text.r                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0.5
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.2 0 2.2
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.ticks                      : <ggplot2::element_line>
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ lineend      : NULL
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.ticks.x                    : NULL
    ##  .. $ axis.ticks.x.top                : NULL
    ##  .. $ axis.ticks.x.bottom             : NULL
    ##  .. $ axis.ticks.y                    : NULL
    ##  .. $ axis.ticks.y.left               : NULL
    ##  .. $ axis.ticks.y.right              : NULL
    ##  .. $ axis.ticks.theta                : NULL
    ##  .. $ axis.ticks.r                    : NULL
    ##  .. $ axis.minor.ticks.x.top          : NULL
    ##  .. $ axis.minor.ticks.x.bottom       : NULL
    ##  .. $ axis.minor.ticks.y.left         : NULL
    ##  .. $ axis.minor.ticks.y.right        : NULL
    ##  .. $ axis.minor.ticks.theta          : NULL
    ##  .. $ axis.minor.ticks.r              : NULL
    ##  .. $ axis.ticks.length               : 'rel' num 0.5
    ##  .. $ axis.ticks.length.x             : NULL
    ##  .. $ axis.ticks.length.x.top         : NULL
    ##  .. $ axis.ticks.length.x.bottom      : NULL
    ##  .. $ axis.ticks.length.y             : NULL
    ##  .. $ axis.ticks.length.y.left        : NULL
    ##  .. $ axis.ticks.length.y.right       : NULL
    ##  .. $ axis.ticks.length.theta         : NULL
    ##  .. $ axis.ticks.length.r             : NULL
    ##  .. $ axis.minor.ticks.length         : 'rel' num 0.75
    ##  .. $ axis.minor.ticks.length.x       : NULL
    ##  .. $ axis.minor.ticks.length.x.top   : NULL
    ##  .. $ axis.minor.ticks.length.x.bottom: NULL
    ##  .. $ axis.minor.ticks.length.y       : NULL
    ##  .. $ axis.minor.ticks.length.y.left  : NULL
    ##  .. $ axis.minor.ticks.length.y.right : NULL
    ##  .. $ axis.minor.ticks.length.theta   : NULL
    ##  .. $ axis.minor.ticks.length.r       : NULL
    ##  .. $ axis.line                       : <ggplot2::element_line>
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ lineend      : chr "square"
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.line.x                     : NULL
    ##  .. $ axis.line.x.top                 : NULL
    ##  .. $ axis.line.x.bottom              : NULL
    ##  .. $ axis.line.y                     : NULL
    ##  .. $ axis.line.y.left                : NULL
    ##  .. $ axis.line.y.right               : NULL
    ##  .. $ axis.line.theta                 : NULL
    ##  .. $ axis.line.r                     : NULL
    ##  .. $ legend.background               : <ggplot2::element_rect>
    ##  ..  ..@ fill         : NULL
    ##  ..  ..@ colour       : logi NA
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.margin                   : NULL
    ##  .. $ legend.spacing                  : 'rel' num 2
    ##  .. $ legend.spacing.x                : NULL
    ##  .. $ legend.spacing.y                : NULL
    ##  .. $ legend.key                      : NULL
    ##  .. $ legend.key.size                 : 'simpleUnit' num 1.2lines
    ##  ..  ..- attr(*, "unit")= int 3
    ##  .. $ legend.key.height               : NULL
    ##  .. $ legend.key.width                : NULL
    ##  .. $ legend.key.spacing              : NULL
    ##  .. $ legend.key.spacing.x            : NULL
    ##  .. $ legend.key.spacing.y            : NULL
    ##  .. $ legend.key.justification        : NULL
    ##  .. $ legend.frame                    : NULL
    ##  .. $ legend.ticks                    : NULL
    ##  .. $ legend.ticks.length             : 'rel' num 0.2
    ##  .. $ legend.axis.line                : NULL
    ##  .. $ legend.text                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : 'rel' num 0.8
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.text.position            : NULL
    ##  .. $ legend.title                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.title.position           : NULL
    ##  .. $ legend.position                 : chr "right"
    ##  .. $ legend.position.inside          : NULL
    ##  .. $ legend.direction                : NULL
    ##  .. $ legend.byrow                    : NULL
    ##  .. $ legend.justification            : chr "center"
    ##  .. $ legend.justification.top        : NULL
    ##  .. $ legend.justification.bottom     : NULL
    ##  .. $ legend.justification.left       : NULL
    ##  .. $ legend.justification.right      : NULL
    ##  .. $ legend.justification.inside     : NULL
    ##  ..  [list output truncated]
    ##   ..@ complete: logi TRUE
    ##   ..@ validate: logi TRUE
    ##  $ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto', 'gg' <ggproto object: Class CoordCartesian, Coord, gg>
    ##     aspect: function
    ##     backtransform_range: function
    ##     clip: on
    ##     default: TRUE
    ##     distance: function
    ##     draw_panel: function
    ##     expand: TRUE
    ##     is_free: function
    ##     is_linear: function
    ##     labels: function
    ##     limits: list
    ##     modify_scales: function
    ##     range: function
    ##     ratio: NULL
    ##     render_axis_h: function
    ##     render_axis_v: function
    ##     render_bg: function
    ##     render_fg: function
    ##     reverse: none
    ##     setup_data: function
    ##     setup_layout: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     train_panel_guides: function
    ##     transform: function
    ##     super:  <ggproto object: Class CoordCartesian, Coord, gg>
    ##  $ facet      :Classes 'FacetNull', 'Facet', 'ggproto', 'gg' <ggproto object: Class FacetNull, Facet, gg>
    ##     attach_axes: function
    ##     attach_strips: function
    ##     compute_layout: function
    ##     draw_back: function
    ##     draw_front: function
    ##     draw_labels: function
    ##     draw_panel_content: function
    ##     draw_panels: function
    ##     finish_data: function
    ##     format_strip_labels: function
    ##     init_gtable: function
    ##     init_scales: function
    ##     map_data: function
    ##     params: list
    ##     set_panel_size: function
    ##     setup_data: function
    ##     setup_panel_params: function
    ##     setup_params: function
    ##     shrink: TRUE
    ##     train_scales: function
    ##     vars: function
    ##     super:  <ggproto object: Class FacetNull, Facet, gg>
    ##  $ layout     :Classes 'Layout', 'ggproto', 'gg' <ggproto object: Class Layout, gg>
    ##     coord: NULL
    ##     coord_params: list
    ##     facet: NULL
    ##     facet_params: list
    ##     finish_data: function
    ##     get_scales: function
    ##     layout: NULL
    ##     map_position: function
    ##     panel_params: NULL
    ##     panel_scales_x: NULL
    ##     panel_scales_y: NULL
    ##     render: function
    ##     render_labels: function
    ##     reset_scales: function
    ##     resolve_label: function
    ##     setup: function
    ##     setup_panel_guides: function
    ##     setup_panel_params: function
    ##     train_position: function
    ##     super:  <ggproto object: Class Layout, gg>
    ##  $ labels     : <ggplot2::labels>  Named list()
    ##  $ meta       : list()
    ##  $ plot_env   :<environment: R_GlobalEnv>

Themes are stored as nested lists. To keep the output short we use `max.level = 2` although using `max.level = 3` would be needed to see all nested members.

    str(p3, max.level = 2, components = "theme")

    ## Object size: 21 MB
    ## List of 1
    ##  $ theme: <theme> List of 144
    ##  .. $ line                            : <ggplot2::element_line>
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ lineend      : chr "butt"
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : chr "black"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ rect                            : <ggplot2::element_rect>
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ text                            : <ggplot2::element_text>
    ##  ..  ..@ family       : chr ""
    ##  ..  ..@ face         : chr "plain"
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ size         : num 11
    ##  ..  ..@ hjust        : num 0.5
    ##  ..  ..@ vjust        : num 0.5
    ##  ..  ..@ angle        : num 0
    ##  ..  ..@ lineheight   : num 0.9
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 0
    ##  ..  ..@ debug        : logi FALSE
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ title                           : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ point                           : <ggplot2::element_point>
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ shape        : num 19
    ##  ..  ..@ size         : num 1.5
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ stroke       : num 0.5
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ polygon                         : <ggplot2::element_polygon>
    ##  ..  ..@ fill         : chr "white"
    ##  ..  ..@ colour       : chr "black"
    ##  ..  ..@ linewidth    : num 0.5
    ##  ..  ..@ linetype     : num 1
    ##  ..  ..@ linejoin     : chr "round"
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ geom                            : <ggplot2::element_geom>
    ##  ..  ..@ ink        : chr "black"
    ##  ..  ..@ paper      : chr "white"
    ##  ..  ..@ accent     : chr "#3366FF"
    ##  ..  ..@ linewidth  : num 0.5
    ##  ..  ..@ borderwidth: num 0.5
    ##  ..  ..@ linetype   : int 1
    ##  ..  ..@ bordertype : int 1
    ##  ..  ..@ family     : chr ""
    ##  ..  ..@ fontsize   : num 3.87
    ##  ..  ..@ pointsize  : num 1.5
    ##  ..  ..@ pointshape : num 19
    ##  ..  ..@ colour     : NULL
    ##  ..  ..@ fill       : NULL
    ##  .. $ spacing                         : 'simpleUnit' num 5.5points
    ##  ..  ..- attr(*, "unit")= int 8
    ##  .. $ margins                         : <ggplot2::margin> num [1:4] 5.5 5.5 5.5 5.5
    ##  .. $ aspect.ratio                    : NULL
    ##  .. $ axis.title                      : NULL
    ##  .. $ axis.title.x                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 2.75 0 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.x.top                : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 0
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 2.75 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.x.bottom             : NULL
    ##  .. $ axis.title.y                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : num 90
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.75 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.title.y.left               : NULL
    ##  .. $ axis.title.y.right              : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : num -90
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 2.75
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text                       : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : 'rel' num 0.8
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 1
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 2.2 0 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x.top                 : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : num 0
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 2.2 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.x.bottom              : NULL
    ##  .. $ axis.text.y                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 1
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.2 0 0
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.y.left                : NULL
    ##  .. $ axis.text.y.right               : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 0 0 2.2
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.text.theta                 : NULL
    ##  .. $ axis.text.r                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0.5
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : <ggplot2::margin> num [1:4] 0 2.2 0 2.2
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.ticks                      : <ggplot2::element_line>
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ lineend      : NULL
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.ticks.x                    : NULL
    ##  .. $ axis.ticks.x.top                : NULL
    ##  .. $ axis.ticks.x.bottom             : NULL
    ##  .. $ axis.ticks.y                    : NULL
    ##  .. $ axis.ticks.y.left               : NULL
    ##  .. $ axis.ticks.y.right              : NULL
    ##  .. $ axis.ticks.theta                : NULL
    ##  .. $ axis.ticks.r                    : NULL
    ##  .. $ axis.minor.ticks.x.top          : NULL
    ##  .. $ axis.minor.ticks.x.bottom       : NULL
    ##  .. $ axis.minor.ticks.y.left         : NULL
    ##  .. $ axis.minor.ticks.y.right        : NULL
    ##  .. $ axis.minor.ticks.theta          : NULL
    ##  .. $ axis.minor.ticks.r              : NULL
    ##  .. $ axis.ticks.length               : 'rel' num 0.5
    ##  .. $ axis.ticks.length.x             : NULL
    ##  .. $ axis.ticks.length.x.top         : NULL
    ##  .. $ axis.ticks.length.x.bottom      : NULL
    ##  .. $ axis.ticks.length.y             : NULL
    ##  .. $ axis.ticks.length.y.left        : NULL
    ##  .. $ axis.ticks.length.y.right       : NULL
    ##  .. $ axis.ticks.length.theta         : NULL
    ##  .. $ axis.ticks.length.r             : NULL
    ##  .. $ axis.minor.ticks.length         : 'rel' num 0.75
    ##  .. $ axis.minor.ticks.length.x       : NULL
    ##  .. $ axis.minor.ticks.length.x.top   : NULL
    ##  .. $ axis.minor.ticks.length.x.bottom: NULL
    ##  .. $ axis.minor.ticks.length.y       : NULL
    ##  .. $ axis.minor.ticks.length.y.left  : NULL
    ##  .. $ axis.minor.ticks.length.y.right : NULL
    ##  .. $ axis.minor.ticks.length.theta   : NULL
    ##  .. $ axis.minor.ticks.length.r       : NULL
    ##  .. $ axis.line                       : <ggplot2::element_line>
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ lineend      : chr "square"
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ arrow        : logi FALSE
    ##  ..  ..@ arrow.fill   : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ axis.line.x                     : NULL
    ##  .. $ axis.line.x.top                 : NULL
    ##  .. $ axis.line.x.bottom              : NULL
    ##  .. $ axis.line.y                     : NULL
    ##  .. $ axis.line.y.left                : NULL
    ##  .. $ axis.line.y.right               : NULL
    ##  .. $ axis.line.theta                 : NULL
    ##  .. $ axis.line.r                     : NULL
    ##  .. $ legend.background               : <ggplot2::element_rect>
    ##  ..  ..@ fill         : NULL
    ##  ..  ..@ colour       : logi NA
    ##  ..  ..@ linewidth    : NULL
    ##  ..  ..@ linetype     : NULL
    ##  ..  ..@ linejoin     : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.margin                   : NULL
    ##  .. $ legend.spacing                  : 'rel' num 2
    ##  .. $ legend.spacing.x                : NULL
    ##  .. $ legend.spacing.y                : NULL
    ##  .. $ legend.key                      : NULL
    ##  .. $ legend.key.size                 : 'simpleUnit' num 1.2lines
    ##  ..  ..- attr(*, "unit")= int 3
    ##  .. $ legend.key.height               : NULL
    ##  .. $ legend.key.width                : NULL
    ##  .. $ legend.key.spacing              : NULL
    ##  .. $ legend.key.spacing.x            : NULL
    ##  .. $ legend.key.spacing.y            : NULL
    ##  .. $ legend.key.justification        : NULL
    ##  .. $ legend.frame                    : NULL
    ##  .. $ legend.ticks                    : NULL
    ##  .. $ legend.ticks.length             : 'rel' num 0.2
    ##  .. $ legend.axis.line                : NULL
    ##  .. $ legend.text                     : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : 'rel' num 0.8
    ##  ..  ..@ hjust        : NULL
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.text.position            : NULL
    ##  .. $ legend.title                    : <ggplot2::element_text>
    ##  ..  ..@ family       : NULL
    ##  ..  ..@ face         : NULL
    ##  ..  ..@ italic       : chr NA
    ##  ..  ..@ fontweight   : num NA
    ##  ..  ..@ fontwidth    : num NA
    ##  ..  ..@ colour       : NULL
    ##  ..  ..@ size         : NULL
    ##  ..  ..@ hjust        : num 0
    ##  ..  ..@ vjust        : NULL
    ##  ..  ..@ angle        : NULL
    ##  ..  ..@ lineheight   : NULL
    ##  ..  ..@ margin       : NULL
    ##  ..  ..@ debug        : NULL
    ##  ..  ..@ inherit.blank: logi TRUE
    ##  .. $ legend.title.position           : NULL
    ##  .. $ legend.position                 : chr "right"
    ##  .. $ legend.position.inside          : NULL
    ##  .. $ legend.direction                : NULL
    ##  .. $ legend.byrow                    : NULL
    ##  .. $ legend.justification            : chr "center"
    ##  .. $ legend.justification.top        : NULL
    ##  .. $ legend.justification.bottom     : NULL
    ##  .. $ legend.justification.left       : NULL
    ##  .. $ legend.justification.right      : NULL
    ##  .. $ legend.justification.inside     : NULL
    ##  ..  [list output truncated]
    ##   ..@ complete: logi TRUE
    ##   ..@ validate: logi TRUE

# Data mappings in ggplots

How does mapping work? Geometries (geoms) and statistics (stats) do not “see” the original variable names, instead the `data` passed to them is named according to the aesthetics user variables are mapped to. Geoms and stats work in tandem, with geoms doing the actual plotting and stats summarizing or transforming the data. It can be instructive to be able to see what data is received as input by a geom or stat, and what data is returned by a stat.

Both geoms and stats can have either panel- or group functions. Panel functions receive as input the subset of the data that corresponds to a whole panel, mapped to the aesthetics and with factors indicating the grouping (set by the user by mapping to a discrete scale). Group functions receive as input the subset of data corresponding to a single group based on the mapping, and called once for each group present in a panel.

The motivation for writing the “debug” stats and geoms included in package ‘gginnards’ is that at the moment it is in many cases not possible to set breakpoints inside the code of stats and geoms, because frequently nameless panel and group functions are stored within list-like `"ggplot"` objects as seen above.

This can make it tedious to analyse how these functions work, as one may need to add `print` statements to their definitions to see the data. I wrote the “debug” stats and geoms as tools to help in the development of my packages ‘ggpmisc’ and ‘ggspectra’, and as a way of learning myself how data are passed around within the different components of a `ggplot` object when it is printed.

# Data input to geometries

Data pass through a statistics before being received by a geometry. However, many geometries, like `geom_point()` and `geom_line()` use by default `stat_identity()` which simply relays the unmodified data to the geometries.

The debug geometries and statistics in package ‘gginnards’ by default do not add any graphical element to the plot but instead they make visible the `data` as received as their input.

The geometry `geom_debug_panel()` uses `stat_identity()` by default. Here the same data as rendered by `geom_point()` is printed as a tibble to the R console. We can see that the columns are named according to the aesthetics the variables in the user-supplied data have been mapped. In the case of colour, the levels of the factor have been replaced by colour definitions. Columns `PANEL` and `group` have been also added.

    ggplot(mpg, aes(cyl, hwy, colour = factor(cyl))) +
      geom_point() +
      geom_debug_panel()

    ## [1] "PANEL 1; group(s) 1, 2, 3, 4; 'draw_panel()' input 'data' (head):"
    ##   x  y  colour PANEL group
    ## 1 4 29 #F8766D     1     1
    ## 2 4 29 #F8766D     1     1
    ## 3 4 31 #F8766D     1     1
    ## 4 4 30 #F8766D     1     1
    ## 5 6 26 #00BFC4     1     3
    ## 6 6 26 #00BFC4     1     3
    ## [1] "PANEL 1; group(s) 1, 2, 3, 4; 'draw_panel()' 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
    ## reverse  1     -none-    character
    ## guides   4     Guides    environment

Below we show how `geom_debug_panel()` can be used together with functions that take a data frame as input and return a value that can be printed. We use here `head()` but other functions such `summary()`, `nrow()` and `colnames()` as well as user defined functions can be useful when `data` is large. As shown here, additional arguments can be passed by name to the function.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      geom_debug_panel(dbgfun.data = head, dbgfun.data.args = list(n = 3))

    ## [1] "PANEL 1; group(s) 1, 2; 'draw_panel()' input 'data' (anonymous function):"
    ##   x         y  colour PANEL group
    ## 1 1 -27205.45 #F8766D     1     1
    ## 2 2 -14242.65 #00BFC4     1     2
    ## 3 3  45790.92 #F8766D     1     1
    ## [1] "PANEL 1; group(s) 1, 2; 'draw_panel()' 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
    ## reverse  1     -none-    character
    ## guides   4     Guides    environment

When using a statistic that modifies the data, we can pass `geom_debug_panel()` as argument in the call to this statistic. In this way the data printed to the console will be those returned by the statistics and received by the geometry.

    ggplot(mpg, aes(cyl, hwy)) +
      stat_summary(fun.data = "mean_se") +
      stat_summary(fun.data = "mean_se", geom = "debug_panel")

    ## [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
    ##   x group        y     ymin     ymax PANEL flipped_aes orientation
    ## 1 4    -1 28.80247 28.30080 29.30414     1       FALSE          NA
    ## 2 5    -1 28.75000 28.50000 29.00000     1       FALSE          NA
    ## 3 6    -1 22.82278 22.40812 23.23745     1       FALSE          NA
    ## 4 8    -1 17.62857 17.23865 18.01849     1       FALSE          NA

As shown above an important use of `geom_debug_panel()` it to display the data returned by a statistic and received as input by geometries. Not all extensions to ‘ggplot2’ document all the computed variables returned by statistics. In other cases like in the next example, the values returned will depend on the arguments passed. While in the previous example the statistic returned a data frame with one row per group, here the returned data frame has 160 rows. The data are by default plotted as a line with a confidence band.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_smooth(method = "lm", formula = y ~ poly(x, 2)) +
      stat_smooth(method = "lm", formula = y ~ poly(x, 2),
                  geom = "debug_panel", dbgfun.data = head)

    ## [1] "PANEL 1; group(s) 1, 2; 'draw_function()' input 'data' (anonymous function):"
    ##          x         y      ymin     ymax       se flipped_aes  colour PANEL
    ## 1 1.000000 23456.375 -26310.65 73223.40 24738.29       FALSE #F8766D     1
    ## 2 2.240506 18461.879 -28860.45 65784.21 23523.08       FALSE #F8766D     1
    ## 3 3.481013 13882.703 -31095.30 58860.71 22357.76       FALSE #F8766D     1
    ## 4 4.721519  9718.845 -33018.17 52455.86 21243.80       FALSE #F8766D     1
    ## 5 5.962025  5970.307 -34632.24 46572.85 20182.79       FALSE #F8766D     1
    ## 6 7.202532  2637.088 -35940.96 41215.13 19176.45       FALSE #F8766D     1
    ##   group orientation
    ## 1     1          NA
    ## 2     1          NA
    ## 3     1          NA
    ## 4     1          NA
    ## 5     1          NA
    ## 6     1          NA

# Data input to statistics

Statistics can be defined to operate on data corresponding to a whole panel or separately on data corresponding to each individual group, as created by mapping aesthetics to factors. The statistics described below print a summary of their `data` input by default to the console. These statistics, in addition return a data frame containing summary information including `labels` suitable for “plotting” with `geom = "text"` or `geom = "label"`. However, package ‘gginnards’ defines a “null” geom, `geom_null()`, which is used as default by the debug statistics. This geom is similar to the more recently added `ggplot2::geom_blank()`.

    ggplot(my.data, aes(x, y)) +
      geom_null()

Using geom “null” allows to add the debug stats for the side effect of console output without altering the rendering of the plot when there is at least one other plot layer. The default geom `"null"` does not alter the rendering of the plot or print to the console the `data` output by the debug stats.

Because of the way ‘ggplot2’ works, the values are listed to the console at the time when the `ggplot` object is printed. As shown here, no other geom or stat is required, however in the remaining examples we add `geom_point()` to make the data also visible in the plot.

    ggplot(my.data, aes(x, y)) +
      stat_debug_group()

    ## [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
    ##   x          y PANEL group
    ## 1 1 -27205.450     1    -1
    ## 2 2 -14242.651     1    -1
    ## 3 3  45790.918     1    -1
    ## 4 4  53731.420     1    -1
    ## 5 5  -8028.578     1    -1
    ## 6 6 102863.943     1    -1

In the absence of facets or groups we get the printout of a single data frame, which is similar to that returned by `geom_debug_panel()`. Without grouping, group is set to `-1` for all observations. As the we override the default geom with `geom_debug_panel()` a summary computed by the stat is also printed to the console.

    ggplot(my.data, aes(x, y)) +
      geom_point() +
      stat_debug_group(geom = "debug_panel")

    ## [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
    ##   x          y PANEL group
    ## 1 1 -27205.450     1    -1
    ## 2 2 -14242.651     1    -1
    ## 3 3  45790.918     1    -1
    ## 4 4  53731.420     1    -1
    ## 5 5  -8028.578     1    -1
    ## 6 6 102863.943     1    -1
    ## [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
    ##   x          y PANEL group
    ## 1 1 -27205.450     1    -1
    ## 2 2 -14242.651     1    -1
    ## 3 3  45790.918     1    -1
    ## 4 4  53731.420     1    -1
    ## 5 5  -8028.578     1    -1
    ## 6 6 102863.943     1    -1

In a plot with no grouping, there is no difference in the `data` input for `compute_panel()` and `compute_group()` functions except for the order of the variables or columns in the data frame (this applies in general to ggplot statistics).

    ggplot(my.data, aes(x, y)) +
      geom_point() +
      stat_debug_panel()

    ## [1] "PANEL 1; group(s) -1; 'compute_panel()' input 'data' (head):"
    ##   x          y PANEL group
    ## 1 1 -27205.450     1    -1
    ## 2 2 -14242.651     1    -1
    ## 3 3  45790.918     1    -1
    ## 4 4  53731.420     1    -1
    ## 5 5  -8028.578     1    -1
    ## 6 6 102863.943     1    -1

By mapping the `colour` aesthetic we create a grouping. In the case, `compute_group()` is called with the data subset by group, and a separate data frame is displayed for each call `compute_group()`, corresponding each to a level in the mapped factor. In this case `group` takes as values positive consecutive integers. As a factor was mapped to colour, colour is encoded as a factor.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_debug_group()

    ## [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (head):"
    ##     x          y colour PANEL group
    ## 1   1 -27205.450      A     1     1
    ## 3   3  45790.918      A     1     1
    ## 5   5  -8028.578      A     1     1
    ## 7   7 -18547.282      A     1     1
    ## 9   9  79924.325      A     1     1
    ## 11 11  -2823.736      A     1     1
    ## [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
    ##     x         y colour PANEL group
    ## 2   2 -14242.65      B     1     2
    ## 4   4  53731.42      B     1     2
    ## 6   6 102863.94      B     1     2
    ## 8   8  13080.52      B     1     2
    ## 10 10 -44711.50      B     1     2
    ## 12 12  23839.55      B     1     2

Without facets, we still have only one panel.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_debug_panel()

    ## [1] "PANEL 1; group(s) 1, 2; 'compute_panel()' input 'data' (head):"
    ##   x          y colour PANEL group
    ## 1 1 -27205.450      A     1     1
    ## 2 2 -14242.651      B     1     2
    ## 3 3  45790.918      A     1     1
    ## 4 4  53731.420      B     1     2
    ## 5 5  -8028.578      A     1     1
    ## 6 6 102863.943      B     1     2

When we map the same factor to a different aesthetic the data remain similar, except for the column named after the aesthetic, in this case `shape`.

    ggplot(my.data, aes(x, y, shape = group)) +
      geom_point() +
      stat_debug_group()

    ## [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (head):"
    ##     x          y shape PANEL group
    ## 1   1 -27205.450     A     1     1
    ## 3   3  45790.918     A     1     1
    ## 5   5  -8028.578     A     1     1
    ## 7   7 -18547.282     A     1     1
    ## 9   9  79924.325     A     1     1
    ## 11 11  -2823.736     A     1     1
    ## [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
    ##     x         y shape PANEL group
    ## 2   2 -14242.65     B     1     2
    ## 4   4  53731.42     B     1     2
    ## 6   6 102863.94     B     1     2
    ## 8   8  13080.52     B     1     2
    ## 10 10 -44711.50     B     1     2
    ## 12 12  23839.55     B     1     2

Facets based on factors create panels within a plot. Here we create a plot with both facets and grouping. In this case, for each panel the `compute_panel()` function is called once with a subset of the data that corresponds to one panel, but not split by groups. For our example, it is called twice.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_debug_panel(dbgfun.data = "nrow") +
      facet_wrap(~block)

    ## [1] "PANEL 1; group(s) 1, 2; 'compute_panel()' input 'data' (nrow):"
    ## [1] 50
    ## [1] "PANEL 2; group(s) 1, 2; 'compute_panel()' input 'data' (nrow):"
    ## [1] 50

with grouping and facets, within each panel the `compute_group()` function is called for each group, in total four times.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_debug_group(dbgfun.data = "nrow") +
      facet_wrap(~block)

    ## [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (nrow):"
    ## [1] 25
    ## [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (nrow):"
    ## [1] 25
    ## [1] "PANEL 2; group(s) 1; 'compute_group()' input 'data' (nrow):"
    ## [1] 25
    ## [1] "PANEL 2; group(s) 2; 'compute_group()' input 'data' (nrow):"
    ## [1] 25

# Controlling the debug output

In the examples above we have demonstrated the use of the statistics and geometries using default arguments. Here we show examples of generation of other types of debug output.

`stat_debug_group()` and `stat_debug_panel()` return summary data that can be inspected using a geometry in addition to printing the data received as argument. If we use `geom_debug_panel()` a summary is printed to the console. With two groups, we get two summaries when we use `stat_debug_group()`.

    ggplot(my.data, aes(x, y, shape = group)) +
      geom_point() +
      stat_debug_group(geom = "debug_panel")

    ## [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (head):"
    ##     x          y shape PANEL group
    ## 1   1 -27205.450     A     1     1
    ## 3   3  45790.918     A     1     1
    ## 5   5  -8028.578     A     1     1
    ## 7   7 -18547.282     A     1     1
    ## 9   9  79924.325     A     1     1
    ## 11 11  -2823.736     A     1     1
    ## [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
    ##     x         y shape PANEL group
    ## 2   2 -14242.65     B     1     2
    ## 4   4  53731.42     B     1     2
    ## 6   6 102863.94     B     1     2
    ## 8   8  13080.52     B     1     2
    ## 10 10 -44711.50     B     1     2
    ## 12 12  23839.55     B     1     2
    ## [1] "PANEL 1; group(s) 1, 2; 'draw_function()' input 'data' (head):"
    ##    x          y shape PANEL group
    ## 1  1 -27205.450    16     1     1
    ## 2  3  45790.918    16     1     1
    ## 3  5  -8028.578    16     1     1
    ## 4  7 -18547.282    16     1     1
    ## 5  9  79924.325    16     1     1
    ## 6 11  -2823.736    16     1     1

If we use `stat_debug_panel()` we get a single summary.

    ggplot(my.data, aes(x, y, shape = group)) +
      geom_point() +
      stat_debug_panel(geom = "debug_panel")

    ## [1] "PANEL 1; group(s) 1, 2; 'compute_panel()' input 'data' (head):"
    ##   x          y shape PANEL group
    ## 1 1 -27205.450     A     1     1
    ## 2 2 -14242.651     B     1     2
    ## 3 3  45790.918     A     1     1
    ## 4 4  53731.420     B     1     2
    ## 5 5  -8028.578     A     1     1
    ## 6 6 102863.943     B     1     2
    ## [1] "PANEL 1; group(s) 1, 2; 'draw_function()' input 'data' (head):"
    ##   x          y shape PANEL group
    ## 1 1 -27205.450    16     1     1
    ## 2 2 -14242.651    17     1     2
    ## 3 3  45790.918    16     1     1
    ## 4 4  53731.420    17     1     2
    ## 5 5  -8028.578    16     1     1
    ## 6 6 102863.943    17     1     2

In principle one can use other geoms to annotate the plot with the debug summary. In this case we silence all output to the R console and use the stat as any other ggplot stat.

    ggplot(my.data, aes(x, y, colour = group)) +
      geom_point() +
      stat_debug_group(geom = "text",
                       mapping = aes(label = sprintf("group = %i",
                                                     after_stat(group))),
                       dbgfun.data = function(x) {NULL})

    ## [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (anonymous function):"
    ## NULL
    ## [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (anonymous function):"
    ## NULL