Skip to content

sec_axis formula behaviour #2974

Closed
Closed

Description

@Ilia-Kosenkov

I fail to understand how the transformation formula of sec_axis behaves if you have a non-linear transformation (that is, something other than ~a*. + b). The results become even worse if the primary axis is transformed using scale_*_*. Here is a reprex:

 library(ggplot2)

    print(ggplot() +
        geom_point(aes(x = 1:10, y = rep(5, 10))) +
        scale_x_continuous(sec.axis = sec_axis(~log10(.))))

    packageVersion("ggplot2")
#> [1] '3.1.0.9000'

Created on 2018-11-01 by the reprex package (v0.2.1.9000)
Now, in the docs (or here, in the comments of axis_secondary.R) there is no mention of any limitation on the transformation formula. One can show that the ~log10(.) formula can be evaluated using the same technique as shown in axis_secondary.R. Here is another reprex:

    library(rlang)
    ff <- ~log10(.)
    eval_tidy(f_rhs(ff), data = list(. = 1:10), env = f_env(ff))
#>  [1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700 0.7781513 0.8450980
#>  [8] 0.9030900 0.9542425 1.0000000

    packageVersion("rlang")
#> [1] '0.2.1'

Created on 2018-11-01 by the reprex package (v0.2.1.9000)

The provided formula is a monotonic transformation, as is required by the code.
You can see that the scale is not logarithmic. Instead, it looks like an equally spaced linear scale between log10 modified limits. But in that case, supposing you have primary axis with c(1, 10) limits, I would expect the secondary to at least have its limits at log10(c(1, 10)) == c(0, 1) and those c(0, 1) breaks aligned with the c(1, 10) on the primary axis.
If we dive deep into the plot structure, assuming that the first reprex plot is p, then if I execute p$scales$scales[[1]]$secondary.axis$break_info(c(1, 10), p$scales$scales[[1]]), it gives the proper range and minor/major ticks, equally spaced between 0 and 1, assuming this call does what I think it does. However, the alignment issue on the plot is still there.
At this point, maybe an inverse transformation formula should be allowed to the sec_axis in such complicated cases, or the documentation/runtime checks should inform about using non-linear formula.
As a result, to get other than dup_axis ticks on the secondary axis, one needs to manually calculate breaks/labels in the scale and apply them explicitly.

As a final argument in favour of such transformations, there are cases in my field, where you want to have two different scales for an axis, which have a 1-to-1 relationship. Examples are: frequencies/periods, frequencies/wavelengths, magnitudes (an astronomical unit proportional to -2.5 log10(Flux)) / fluxes, which require 1 / . and log(.) transformations and can be possibly combined with scale_*_log10.

Activity

  1. dpseidel commented on Nov 14, 2018

    @dpseidel
    Collaborator

    I'm going to close this as a duplicate of #2978 and refer you to the discussion over there. This alignment issue is caused by a bug introduced in 3.1.0 and will be patched shortly.

    I did want to mention that yes, yours is an appropriate way to extract the breaks info, although layer_scales(p)$x$break_info() is cleaner and will give you both secondary and primary x axis breaks. It's worth noting that both this call and your original use an unexpanded range whereas the plot axes are built with minor expansion by default. In this case, to see results that match the 6 ticks plotted on the secondary axis, pass in the expanded range, c(.55, 10.45), to either call, e.g. layer_scales(p)$x$break_info(c(.55, 10.45))

  2. lock commented on May 13, 2019

    @lock

    This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

  3. locked and limited conversation to collaborators on May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions