Backround
I was reading this excellent answer on how to place self adjusting text into bars: Resizeable text grobs inside bars
After reading a bit on ggproto
and especially the vignette on Extending ggplot I was wondering why the author had to define the setup_data
routine as follows:
GeomFit <- ggproto("GeomFit", GeomRect,
setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
transform(data,
ymin = pmin(y, 0), ymax = pmax(y, 0),
xmin = x - width / 2, xmax = x + width / 2, width = NULL
)
})
Because this is essentially a copy paste from ggplot2::GeomBar
:
GeomBar$setup_data
# <ggproto method>
# <Wrapper function>
# function (...)
# f(...)
# <Inner function (f)>
# function (data, params)
# {
# data$width <- data$width %||% params$width %||% (resolution(data$x,
# FALSE) * 0.9)
# transform(data, ymin = pmin(y, 0), ymax = pmax(y, 0), xmin = x -
# width/2, xmax = x + width/2, width = NULL)
# }
So I thought I could replace this simply by:
GeomFit <- ggproto("GeomFit", GeomRect,
setup_data = function(self, data, params)
ggproto_parent(GeomBar, self)$setup_data(data, params))
This approach works, but I have my doubts whether this may lead to some unwanted behaviour simply becasue the parent class of GeomFit
is GeomRect
and not GeomBar
.
Question
Is it ok (in the sense of: there are no conditions where this may cause a problem) to use ggproto_parent
to call a function from a class which is not the parent class of my ggproto
object? Why does ggproto_parent
have a parent
argument in the first place? Shouldn't the parent be anyways determined by the second argument of ggproto
?