Skip to content

Secondary axis doesn't show axis ticks or labels with coord_trans #2990

Closed

Description

@anthonytw

Exactly the same issue as #2072, which I take it was fixed since it is closed.

The secondary axis ticks and labels do not show up when coord_trans is present, even if using the identity transformation. The axis label ("name" parameter) does still appear.

For example, roughly taking from the examples in sec_axis:

ggplot(mtcars, aes(cyl, mpg)) + geom_point()  +
    scale_y_continuous(sec.axis = sec_axis(~.+10, name='MPG + 10'))

works as expected, but

ggplot(mtcars, aes(cyl, mpg)) + geom_point()  +
    scale_y_continuous(sec.axis = sec_axis(~.+10, name='MPG + 10')) +
    coord_trans()

loses the axis ticks and labels (but keeps the title "MPG + 10").

Activity

  1. changed the title [-]Secondary axis doesn't work with coord_trans[/-] [+]Secondary axis doesn't show axis ticks or labels with coord_trans[/+] on Nov 9, 2018
  2. clauswilke commented on Nov 9, 2018

    @clauswilke
    Member

    Reprex:

    library(ggplot2)
    ggplot(mtcars, aes(cyl, mpg)) + geom_point()  +
      scale_y_continuous(sec.axis = sec_axis(~.+10, name='MPG + 10'))

    ggplot(mtcars, aes(cyl, mpg)) + geom_point()  +
      scale_y_continuous(sec.axis = sec_axis(~.+10, name='MPG + 10')) +
      coord_trans()

    Created on 2018-11-09 by the reprex package (v0.2.1)

    @dpseidel Is this distinct from #2978?

  3. dpseidel commented on Nov 9, 2018

    @dpseidel
    Collaborator

    It seems so. At least it predates my changes from #2796 and is present with version 3.0.0. I'll need to look into this further to be certain. Will do when I'm back at my desktop.

  4. yutannihilation commented on Nov 12, 2018

    @yutannihilation
    Member

    This problem comes from here in CoordTrans$setup_panel_params(); out is redefined so the params for the secondary axis (out$sec.range, out$sec.major, out$sec.minor, etc) are lost.

    ggplot2/R/coord-transform.r

    Lines 192 to 195 in 868fdb7

    out <- list(
    range = out$range, labels = out$labels,
    major = out$major_source, minor = out$minor_source
    )

    If I replace the code with below, at least the labels are shown (the locations are wrong, though).

      out$major <- out$major_source
      out$minor <- out$minor_source
      out$arrange <- scale$axis_order()
  5. ptoche commented on Nov 12, 2018

    @ptoche

    If out$sec.major ends up having length 0, then the axis is removed following the logic explained here: #2983 (comment), I guess.

  6. dpseidel commented on Dec 16, 2018

    @dpseidel
    Collaborator

    I played around with this a little on my branch containing my most recent sec.axis patch (#3040) and can confirm the misalignment is still an issue. Interestingly, it's unique to all the other misalignment issues. It seems that when coord_trans() is specified sec.axis is passed the unexpanded range -- the transformed range of the data, not the transformed range of the primary scale (as it is when no coord is specified). This leads to misalignment relative to the primary axis.

    # loading local dpseidel/revert2796 + suggested coord_trans patch
    library(ggplot2)
    
    c <- ggplot(mtcars, aes(cyl, mpg)) +
      geom_point() +
      scale_y_continuous(sec.axis = sec_axis(~ . + 10, name = "MPG + 10")) +
      coord_trans()
    

    A screenshot of output from within debug(get("break_info", AxisSecondary)) to demonstrate the actual break specifications calculated:

    screen shot 2018-12-16 at 10 44 58 am

    Notably, and likely a separate bug worth flagging in its own issue, the layer_scales function runs the break_info function with the expected, expanded, range even when passed coord_trans() and returns properly aligned break values; a bug I have encountered when debugging other sec.axis issues and which can give users a false reading of the position of tick axes (as it does below):

    layer_scales(c)$y$break_info()
    #> $range
    #> [1] 10.4 33.9
    #> 
    #> $labels
    #> [1] "15" "20" "25" "30"
    #> 
    #> $major
    #> [1] 0.1957447 0.4085106 0.6212766 0.8340426
    #> 
    #> $minor
    #> [1] 0.0893617 0.1957447 0.3021277 0.4085106 0.5148936 0.6212766 0.7276596
    #> [8] 0.8340426 0.9404255
    #> 
    #> $major_source
    #> [1] 15 20 25 30
    #> 
    #> $minor_source
    #> [1] 12.5 15.0 17.5 20.0 22.5 25.0 27.5 30.0 32.5
    #> 
    #> $sec.range
    #> [1] 20.4 43.9
    #> 
    #> $sec.labels
    #> [1] "25" "30" "35" "40"
    #> 
    #> $sec.major
    #> [1] 0.1957447 0.4085106 0.6212766 0.8340426
    #> 
    #> $sec.minor
    #> [1] 0.0893617 0.1957447 0.3021277 0.4085106 0.5148936 0.6212766 0.7276596
    #> [8] 0.8340426 0.9404255
    #> 
    #> $sec.major_source
    #> [1] 25 30 35 40
    #> 
    #> $sec.minor_source
    #> [1] 22.5 25.0 27.5 30.0 32.5 35.0 37.5 40.0 42.5

    Created on 2018-12-16 by the reprex package (v0.2.1)

    Since this is a scale expansion issue, misalignment is fixed if one specifies expand = c(0,0) but we will likely want to dig deeper into this (and merge an appropriate coord_trans patch) once the sec.axis refactoring is complete.

  7. thomasp85 commented on Apr 11, 2019

    @thomasp85
    Member

    @dpseidel do you have any idea how to handle this most gracefully?

  8. dpseidel commented on Apr 11, 2019

    @dpseidel
    Collaborator

    I'll need to look back into it. I'll plan my own private dev day this weekend to dig into this and other open axis/scale issues 👍

  9. paleolimbot commented on May 23, 2019

    @paleolimbot
    Member

    This seems related to #3338 in that coord_trans() is handling scale expansion differently than other coords.

  10. added
    bug
    on May 23, 2019
  11. lock commented on Dec 28, 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/

  12. locked and limited conversation to collaborators on Dec 28, 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

    bugcoord 🗺️scales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions