Hey @mrmsdbdl, sorry for the late reply! With what you're telling me, it seems like this kind of plot you want your extension to produce involves a few parts, including:
- a combination of geoms (ie. points + segments + vertical and horizontal lines);
- altered scales; and
- altered ticks (or just the look of the ticks?)
You've said that it's easy for you to produce a plot like this yourself with ggplot2
but that you're trying to work out how to package this. I wonder if you're not overthinking things by trying to create custom components for all aspects of this. After all, a function can either return a completed (or partially completed) ggplot or a list of ggplot components that can be added to another plot. For example:
#' @export
ggsand <- function(data, xline = 0, yline = 0) {
ggplot(data, aes(x = x, y = y)) +
geom_hline(yintercept = yline) +
geom_vline(xintercept = xline) +
geom_point() +
geom_segment() + # or geom_path
scale_x_continuous(limits = c(0, 10)) +
scale_y_continuous(limits = c(3, 9)) + # or whatever custom limits you want
theme(
axis.tick.x = element_line(size = 3))
}
Or:
sand_elements <- list(
geom_hline(yintercept = 0), # er, these might need to be fixed
geom_vline(xintercept = 0), # or assumed you do it this way
geom_point(),
geom_segment(), # or geom_path
scale_x_continuous(limits = c(0, 10)),
scale_y_continuous(limits = c(3, 9)), # or whatever custom limits you want
theme(
axis.tick.x = element_line(size = 3)))
ggplot(data, aes(x = x, y = y)) + sand_elements
Now, that doesn't help you with your custom geom/stat: it sounds like you want geom_segment
or geom_points
(or both) to be doing some sort of computation on the given data before drawing the segments/points. So you'll need a custom stat or geom(s) for that. But if you take this approach, it means, you can leverage ggplot's existing modularity as much as possible and just focus on the part where you actually need it to do something new (rather than trying to come up with a custom component that completely rewrites the whole build process).
You can have a package that exports both the custom component and also the helper function to build the complete plot (so that people can decide whether they want the easy plot generation or to do it themselves), or you can choose to just export the helper function and just use the custom component to abstract the hard work away yourself. What do you think?