stat_debug_group
and stat_debug_panel
apply a function to data
and, most importantly,
echo to the R console their data
input or a summary of it.
Usage
stat_debug_group(
mapping = NULL,
data = NULL,
geom = "null",
fun.data = "I",
fun.data.args = list(),
dbgfun.data = "head",
dbgfun.data.args = list(),
geom.dbgfun.data = "head",
geom.dbgfun.data.args = list(),
geom.dbgfun.params = NULL,
geom.dbgfun.params.args = list(),
dbgfun.print = "print",
dbgfun.print.args = list(),
position = "identity",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
...
)
stat_debug_panel(
mapping = NULL,
data = NULL,
geom = "null",
fun.data = "I",
fun.data.args = list(),
dbgfun.data = "head",
dbgfun.data.args = list(),
geom.dbgfun.data = "head",
geom.dbgfun.data.args = list(),
geom.dbgfun.params = NULL,
geom.dbgfun.params.args = list(),
dbgfun.print = "print",
dbgfun.print.args = list(),
position = "identity",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE,
...
)
Arguments
- mapping
The aesthetic mapping, usually constructed with
aes
oraes_
. Only needs to be set at the layer level if you are overriding the plot defaults.- data
A layer specific dataset - only needed if you want to override the plot defaults.
- geom
The geometric object to use display the data
- fun.data
A function taking a data frame as its first argument and returning a data frame. This function does the computations generating the value passed from the statistic to the downstream geometry.
- fun.data.args
A named list of additional arguments to be passed to
fun.data
.- dbgfun.data, geom.dbgfun.data, geom.dbgfun.params
A functions used to summarise the
data
andparameters
objects received as input by the statistic and geometry.- dbgfun.data.args, geom.dbgfun.data.args, geom.dbgfun.params.args
A named list of arguments.
- dbgfun.print
A function used to print the summary of the
data
object received as input by the statistic, also visible to the geometry, and used bygeom_debug()
.- dbgfun.print.args
A named list. Currently ignored!
- position
The position adjustment to use for overlapping points on this layer
- na.rm
a logical value indicating whether NA values should be stripped before the computation proceeds.
- show.legend
logical. Should this layer be included in the legends?
NA
, the default, includes if any aesthetics are mapped.FALSE
never includes, andTRUE
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
. This can include aesthetics whose values you want to set, not map. Seelayer
for more details.
Value
A copy of its data
input, which is an object of class
"data.frame"
or inheriting from "data.frame"
.
Details
These stats are meant to be used for the side-effect of printing to
the console the data
object received as input by the
compute_group()
or compute_panel()
function, or a summary of
it. These data
objects are the same as those received as input by
any other statistics passed the same arguments. By default, the applied
function is I()
, the identity function.
In principle any geom can be passed as argument to override geom =
"null"
. However, geom = "debug_panel"
and geom =
"debug_group"
are treated as special cases and functions
geom.dbgfun.data
and geom.dbgfun.params
, and lists
geom.dbgfun.data.args
and geom.dbgfun.params.args
renamed and
passed to the geometry. Arguments passed to these four formal parameters
are not passed to other geometries.
Keep in mind that this stat sets default mappings only for the x
and/or y aesthetics, additional mappings can be set using
aes()
, possibly together with after_stat()
.
Computed variables
- x
x at centre of range
- y
y at centre of range
- nrow
nrow()
ofdata
object- ncol
ncol()
ofdata
object- colnames
colnames()
ofdata
object- colclasses
class()
ofx
andy
columns indata
object- group
all distinct values in group as passed in
data
object- PANEL
all distinct values in PANEL as passed in
data
object
Examples
my.df <- data.frame(x = rep(1:10, 2),
y = rep(c(1,2), c(10,10)) + rnorm(20),
group = rep(c("A","B"), c(10,10)))
# by default head() is used to show the top rows of the data object
# and geom_null() to silence the data returned by the stat
ggplot(my.df, aes(x,y)) +
geom_point() +
stat_debug_group()
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
# geom_debug prints the data returned by the stat
ggplot(my.df, 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 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
#> [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
# geom_debug prints the data returned by the stat
ggplot(my.df, aes(x,y)) +
geom_point() +
stat_debug_group(geom = "debug_panel",
geom.dbgfun.params = "summary")
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
#> [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
#> [1] "PANEL 1; group(s) -1; 'draw_function()' input 'params' (summary):"
#> Length Class Mode
#> x 11 ViewScale environment
#> x.sec 11 ViewScale environment
#> x.range 2 -none- numeric
#> y 11 ViewScale environment
#> y.sec 11 ViewScale environment
#> y.range 2 -none- numeric
#> guides 4 Guides environment
# to print only the the data returned by the stat
# we pass as summary function a function that always returns NULL
ggplot(my.df, aes(x,y)) +
geom_point() +
stat_debug_group(geom = "debug_panel",
dbgfun.data = function(x) {NULL})
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (anonymous function):"
#> NULL
#> [1] "PANEL 1; group(s) -1; 'draw_function()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
ggplot(my.df, aes(x,y)) +
geom_point() +
stat_debug_group(aes(label = paste("group:", group)),
geom = "text")
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
#> label x y PANEL group
#> 1 group: A 1 1.935363 1 -1
#> 2 group: A 2 1.176489 1 -1
#> 3 group: A 3 1.243685 1 -1
#> 4 group: A 4 2.623549 1 -1
#> 5 group: A 5 1.112038 1 -1
#> 6 group: A 6 0.866003 1 -1
# here we show all the data object
ggplot(my.df, aes(x,y)) +
geom_point() +
stat_debug_group(dbgfun.data = "I")
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (I):"
#> x y PANEL group
#> 1 1 1.93536319 1 -1
#> 2 2 1.17648861 1 -1
#> 3 3 1.24368546 1 -1
#> 4 4 2.62354888 1 -1
#> 5 5 1.11203808 1 -1
#> 6 6 0.86600299 1 -1
#> 7 7 -0.91008747 1 -1
#> 8 8 0.72076276 1 -1
#> 9 9 0.68655402 1 -1
#> 10 10 2.06730788 1 -1
#> 11 1 2.07003485 1 -1
#> 12 2 1.36087668 1 -1
#> 13 3 1.95003510 1 -1
#> 14 4 1.74851656 1 -1
#> 15 5 2.44479712 1 -1
#> 16 6 4.75541758 1 -1
#> 17 7 2.04653138 1 -1
#> 18 8 2.57770907 1 -1
#> 19 9 2.11819487 1 -1
#> 20 10 0.08827951 1 -1
# with grouping
ggplot(my.df, 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 1.935363 A 1 1
#> 2 2 1.176489 A 1 1
#> 3 3 1.243685 A 1 1
#> 4 4 2.623549 A 1 1
#> 5 5 1.112038 A 1 1
#> 6 6 0.866003 A 1 1
#> [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
#> x y colour PANEL group
#> 11 1 2.070035 B 1 2
#> 12 2 1.360877 B 1 2
#> 13 3 1.950035 B 1 2
#> 14 4 1.748517 B 1 2
#> 15 5 2.444797 B 1 2
#> 16 6 4.755418 B 1 2
ggplot(my.df, 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 1.935363 A 1 1
#> 2 2 1.176489 A 1 1
#> 3 3 1.243685 A 1 1
#> 4 4 2.623549 A 1 1
#> 5 5 1.112038 A 1 1
#> 6 6 0.866003 A 1 1
ggplot(my.df, aes(x, y, colour = group)) +
geom_point() +
stat_debug_group(dbgfun.data = "nrow")
#> [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (nrow):"
#> [1] 10
#> [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (nrow):"
#> [1] 10
ggplot(my.df, aes(x, y)) +
geom_point() +
facet_wrap(~group) +
stat_debug_group()
#> [1] "PANEL 1; group(s) -1; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 -1
#> 2 2 1.176489 1 -1
#> 3 3 1.243685 1 -1
#> 4 4 2.623549 1 -1
#> 5 5 1.112038 1 -1
#> 6 6 0.866003 1 -1
#> [1] "PANEL 2; group(s) -1; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 1 1 2.070035 2 -1
#> 2 2 1.360877 2 -1
#> 3 3 1.950035 2 -1
#> 4 4 1.748517 2 -1
#> 5 5 2.444797 2 -1
#> 6 6 4.755418 2 -1
# by default head() is used to show the top rows of data object
ggplot(my.df, aes(group,y)) +
geom_point() +
stat_debug_group()
#> [1] "PANEL 1; group(s) 1; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 1 1 1.935363 1 1
#> 2 1 1.176489 1 1
#> 3 1 1.243685 1 1
#> 4 1 2.623549 1 1
#> 5 1 1.112038 1 1
#> 6 1 0.866003 1 1
#> [1] "PANEL 1; group(s) 2; 'compute_group()' input 'data' (head):"
#> x y PANEL group
#> 11 2 2.070035 1 2
#> 12 2 1.360877 1 2
#> 13 2 1.950035 1 2
#> 14 2 1.748517 1 2
#> 15 2 2.444797 1 2
#> 16 2 4.755418 1 2