aphalo / ggpp

Grammar of graphics extensions to 'ggplot2'
118 stars 10 forks source link

stat_functions() #42

Closed aphalo closed 1 year ago

aphalo commented 1 year ago

stat_function() from 'ggplot2' works like an annotation, and accepts a single function. This does not match the expectations of the grammar of graphics. In the new stat_functions() the functions would be in a list column in data, the code in the stat would compute y values for each function from x values if available in data. Unlike stat_function(), x values would not be generated automatically, but possibly sparse vectors giving a range could be populated by interpolation (opposite to stat_summary() the returned data object would have more rows than the input data object).

If 'ggplot2' code can do the aesthetic mapping as it does for whole data frames and gg objects for insets, this would be fairly easy to implement.

An alternative would be to prepare a pull request to 'ggplot2' revising stat_function() to accept vectors or lists as geom_hline() and similar functions do.

The idea was triggered by the necessarily complex and inelegant code included in a question at StackOverflow as example, not by the question itself.

aphalo commented 1 year ago

The list of functions would then be mapped to the y or x aesthetics, and orientation would be set automatically, based on this or overriden through parameter orientation.

aphalo commented 1 year ago

Now I know how this stat should work! Aesthetic mappings without redundancy can be achieved. Map list of functions to y, and map range to xmin and xmax or, possibly, functions to x and range to ymin and ymax. This gives one function for each row in data making it easy to use, but making it possible to fully support the grammar of graphics.

aphalo commented 1 year ago

Initial version mplemented and working. No unit tests yet, but examples do work as expected. Parameter orientation is not yet implemented.