Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
anthonytw opened this issue Nov 9, 2018 · 9 comments
Closed

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

anthonytw opened this issue Nov 9, 2018 · 9 comments

Comments

@anthonytw
Copy link

@anthonytw anthonytw commented Nov 9, 2018

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").

@anthonytw anthonytw changed the title Secondary axis doesn't work with coord_trans Secondary axis doesn't show axis ticks or labels with coord_trans Nov 9, 2018
@clauswilke
Copy link
Member

@clauswilke clauswilke commented Nov 9, 2018

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?

Loading

@dpseidel
Copy link
Member

@dpseidel dpseidel commented Nov 9, 2018

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.

Loading

@yutannihilation
Copy link
Member

@yutannihilation yutannihilation commented Nov 12, 2018

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()

Loading

@ptoche
Copy link

@ptoche ptoche commented Nov 12, 2018

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

Loading

@dpseidel
Copy link
Member

@dpseidel dpseidel commented Dec 16, 2018

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.

Loading

@thomasp85
Copy link
Member

@thomasp85 thomasp85 commented Apr 11, 2019

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

Loading

@dpseidel
Copy link
Member

@dpseidel dpseidel commented Apr 11, 2019

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 👍

Loading

@paleolimbot
Copy link
Member

@paleolimbot paleolimbot commented May 23, 2019

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

Loading

@lock
Copy link

@lock lock bot commented Dec 28, 2019

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/

Loading

@lock lock bot locked and limited conversation to collaborators Dec 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

7 participants