ggdebug v0.1.1
ggdebug
is a package for debugging ggplot2 stats.
I created this package to help in developing new ggplot2
stats, and trying
to decipher and understand how existing ones work.
ggdebug v0.1.1 now includes a function for determining the name of the Geom which
is being used with the current Stat - ggdebug::get_geom()
You can install from GitHub with:
# install.packages("remotes")
remotes::install_github("coolbutuseless/ggdebug")
Determine the Geom from within a Stat
For good reasons(?) within the Grammar of Graphics, the Stat and the Geom should work independently.
For other reasons, I sometimes want to do weird things within a Stat depending on which Geom it is being used with.
Finding the name of the Geom is not obvious, so I’ve included get_geom()
which
interrogates the frame stack to find the Layer which contains the Stat, and finds
the name of the Geom contained within that Layer.
If anyone knows of a better way of doing this, please get in touch!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a Stat to test with
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stat_oath_breaker <- function(mapping = NULL, data = NULL,
geom = "point",
position = "identity",
...,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = StatOathBreaker,
geom = geom,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
na.rm = FALSE,
...
)
)
}
StatOathBreaker <- ggproto(
"StatOathBreaker", Stat,
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Call `get_geom()` from within the Stat
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setup_params = function(data, params) {
message("StatOathBreaker being used with: ", ggdebug::get_geom())
params
},
compute_layer = function(data, scales, params) {
data
}
)
ggplot(mtcars) +
geom_line(aes(mpg, wt), stat = 'oath_breaker')
StatOathBreaker being used with: GeomLine