Skip to contents

Extensions to 'ggplot2' providing low-level debug tools: statistics and geometries echoing their data argument. Layer manipulation: deletion, insertion, extraction and reordering of layers. Deletion of unused variables from the data object embedded in "ggplot" objects.

Details

The new facilities for cleanly defining new stats and geoms added to package 'ggplot2' in version 2.0.0 gave origin to this package. I needed tools to help me learn how layers work and to debug the extensions to 'ggplot2' that I was developing. I share them through this package in the hope that they will help other users of 'ggplot2' understand how this this very popular graphics package works internally. The vignettes provide examples of how to use these tools both for debugging and learning how ggplots are stored.

Extensions provided:

  • "Debug" stats and a "debug" geom that print to the console a summary of their data input.

  • Functions for inspecting and manipulating the list of layers of a ggplot object.

  • Functions for exploring and manipulating the data embedded in ggplot objects, including dropping unused variables.

References

Package 'tidyverse' web site at https://www.tidyverse.org/
Package 'ggplot2' documentation at https://ggplot2.tidyverse.org/
Package 'ggplot2' source code at https://github.com/tidyverse/ggplot2

Author

Maintainer: Pedro J. Aphalo pedro.aphalo@helsinki.fi (ORCID)

Examples

# echo the 'data' and 'params' as received by 'draw_panel()'
ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug_panel()

#> [1] "PANEL 1; group(s) 1, 2, 3; 'draw_panel()' input 'data' (head):"
#>    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
#> [1] "PANEL 1; group(s) 1, 2, 3; '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    
#> guides   4     Guides    environment

# echo the 'data' and 'params' as received by 'draw_group()'
ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  geom_debug_group()

#> [1] "PANEL 1; group(s) 1; 'draw_group()' input 'data' (head):"
#>     colour x    y PANEL group
#> 3  #F8766D 4 22.8     1     1
#> 8  #F8766D 4 24.4     1     1
#> 9  #F8766D 4 22.8     1     1
#> 18 #F8766D 4 32.4     1     1
#> 19 #F8766D 4 30.4     1     1
#> 20 #F8766D 4 33.9     1     1
#> [1] "PANEL 1; group(s) 1; 'draw_group()' 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] "PANEL 1; group(s) 2; 'draw_group()' input 'data' (head):"
#>     colour x    y PANEL group
#> 1  #00BA38 6 21.0     1     2
#> 2  #00BA38 6 21.0     1     2
#> 4  #00BA38 6 21.4     1     2
#> 6  #00BA38 6 18.1     1     2
#> 10 #00BA38 6 19.2     1     2
#> 11 #00BA38 6 17.8     1     2
#> [1] "PANEL 1; group(s) 2; 'draw_group()' 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] "PANEL 1; group(s) 3; 'draw_group()' input 'data' (head):"
#>     colour x    y PANEL group
#> 5  #619CFF 8 18.7     1     3
#> 7  #619CFF 8 14.3     1     3
#> 12 #619CFF 8 16.4     1     3
#> 13 #619CFF 8 17.3     1     3
#> 14 #619CFF 8 15.2     1     3
#> 15 #619CFF 8 10.4     1     3
#> [1] "PANEL 1; group(s) 3; 'draw_group()' 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

# echo the 'data' and 'params' as received by 'draw_panel()'
ggplot(mtcars, aes(cyl, mpg, colour = factor(cyl))) +
  stat_summary(fun.data = "mean_se") +
  stat_summary(fun.data = "mean_se", geom = "debug_panel")

#> [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 25.30387 28.02340     1       FALSE          NA
#> 2 #00BA38 6     2 19.74286 19.19346 20.29225     1       FALSE          NA
#> 3 #619CFF 8     3 15.10000 14.41580 15.78420     1       FALSE          NA

# echo the 'data' received by 'compute_panel()'
ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  stat_debug_panel()

#> [1] "PANEL 1; group(s) 1, 2, 3; 'compute_panel()' input 'data' (head):"
#>   x    y colour PANEL group
#> 1 6 21.0      6     1     2
#> 2 6 21.0      6     1     2
#> 3 4 22.8      4     1     1
#> 4 6 21.4      6     1     2
#> 5 8 18.7      8     1     3
#> 6 6 18.1      6     1     2

# echo the 'data' received by 'compute_group()'
ggplot(mtcars, aes(cyl, mpg, color = factor(cyl))) +
  geom_point() +
  stat_debug_group()

#> [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (head):"
#>    x    y colour PANEL group
#> 3  4 22.8      4     1     1
#> 8  4 24.4      4     1     1
#> 9  4 22.8      4     1     1
#> 18 4 32.4      4     1     1
#> 19 4 30.4      4     1     1
#> 20 4 33.9      4     1     1
#> [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
#>    x    y colour PANEL group
#> 1  6 21.0      6     1     2
#> 2  6 21.0      6     1     2
#> 4  6 21.4      6     1     2
#> 6  6 18.1      6     1     2
#> 10 6 19.2      6     1     2
#> 11 6 17.8      6     1     2
#> [1] "PANEL 1; group(s) 3; 'compute_group()' input 'data' (head):"
#>    x    y colour PANEL group
#> 5  8 18.7      8     1     3
#> 7  8 14.3      8     1     3
#> 12 8 16.4      8     1     3
#> 13 8 17.3      8     1     3
#> 14 8 15.2      8     1     3
#> 15 8 10.4      8     1     3