r-lib / scales

Tools for ggplot2 scales
https://scales.r-lib.org
Other
398 stars 108 forks source link

Feature request (and offer for help): optionally supply derivatives of transforms and inverses #322

Closed mjskay closed 11 months ago

mjskay commented 2 years ago

Something that comes up regularly when visualizing analytical distributions on transformed scales is the need to correct the density function for the scale transformation (the so-called "Jacobian correction"), which requires the derivative of the inverse of the scale transformation.

For example, {ggdist} allows visualizing analytical distributions on transformed scales and automatically determines the Jacobian in order to do this correctly. E.g. a log-normal distribution plotted on a log scale should look like a normal distribution (without this correction it would not):

library(ggplot2)
library(magrittr)
library(ggdist)

data.frame(dist = "lnorm") %>%
  ggplot(aes(y = 0, dist = dist, arg1 = log(10), arg2 = 2*log(10))) +
  stat_dist_halfeye() +
  scale_x_log10(breaks = 10^seq(-5,7, by = 2))

Created on 2021-12-28 by the reprex package (v2.0.1)

In {ggdist} we currently do this first by attempting to get the symbolic derivative by applying stats::D() to the scale transformation function definition (this usually fails) and then fall back to numerical derivatives. Naturally, the latter are not perfect and will fail in some cases, degrading the user experience.

The ask / offer

It occurred to me that this can't be the only application where it would be helpful to know the derivative of the $transform() and/or $inverse() of a scale transformation. So I would like to propose adding two additional fields to the trans objects in {scales}:

These would be optional fields (may be NULL) that not all transformations are required to have. This would retain backwards compatibility, and would also work for my purposes (as I can continue falling back to symbolic and numerical differentiation as needed), but would (1) immediately improve the user experience with existing scale functions by supplying known derivatives in this package and (2) allow users to easily fix cases with manual scales when numerical methods fail by supplying the derivative manually.

If there is interest in something like this, I am happy to supply a PR that creates these fields and supplies derivatives for the existing transformations in R/trans-numeric.r. Thanks!

hadley commented 2 years ago

This would be great if you're interested in doing it 😄

mjskay commented 2 years ago

Cool! Definitely interested in doing it 😊. I will take a stab in advance of the next iteration of ggdist, probably this summer.