Skip to content

Support guide_axis() on CoordTrans #3972

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

Conversation

yutannihilation
Copy link
Member

@yutannihilation yutannihilation commented Apr 29, 2020

Address #3959 partly, fix #4551.

The plan was to support guide_axis() on all coords, but, as it might require some more consideration, I narrowed the scoped of this PR only to CoordCartesian and CoordTrans.

  • Move labels(), setup_panel_guides() and train_panel_guides() to Coord
  • Tweak these methods to support panel_params without ViewScales for backward compatibility
  • Modify each setup_panel_params() to include ViewScale
  • Add tests

library(ggplot2)

do_plot <- function() {
  df <- data.frame(x = 1, y = 1)
  p <- ggplot(df, aes(x, y)) +
    geom_point() +
    guides(
      x = guide_axis(title = "x (primary)"),
      y = guide_axis(title = "y (primary)"),
      x.sec = guide_axis(title = "x (secondary)"),
      y.sec = guide_axis(title = "y (secondary)")
    )
  
  p1 <- p +
    labs(tag = "p1")
  
  p2 <- p +
    scale_x_continuous(position = "top") +
    scale_y_continuous(position = "right") +
    labs(tag = "p2")
  
  p3 <- p1 +
    coord_trans() +
    labs(tag = "p1'")
  
  p4 <- p2 +
    coord_trans() +
    labs(tag = "p2'")
  
  patchwork::wrap_plots(p1, p2, p3, p4, nrow = 2)
}

do_plot()

devtools::load_all("~/GitHub/ggplot2/")
#> ℹ Loading ggplot2
do_plot()

Created on 2022-07-08 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member Author

Hmm, It turned out I failed to fix how CoordTrans treats Inf on #2972.. Will fix in this PR.

@thomasp85
Copy link
Member

is this ready for review?

@yutannihilation
Copy link
Member Author

No, not yet. As this will tweak a lot of internals, I think we should not include this in v3.3.1 to avoid possible breakages on extension packages.

Comment on lines 230 to 231
# TODO: Can I add here? This seems cause cryptic warning "In min(x) : no non-missing arguments to min; returning Inf"
# sec = view_scale_secondary(scale, scale_limits, continuous_ranges$continuous_range_coord),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Currently I'm stuck here...)

@thomasp85
Copy link
Member

ok, no problem

@thomasp85 thomasp85 added this to the ggplot2 3.4.0 milestone Mar 26, 2021
@thomasp85
Copy link
Member

@yutannihilation do you want to take this up again for next release?

@yutannihilation
Copy link
Member Author

Sure. I forgot everything, but I'll try again.

@yutannihilation
Copy link
Member Author

Current progress:

  • CoordCartesian works fine (no change since 2 years ago).
  • CoordTrans works now. There might be still room for improvement.
  • CoordPolar doesn't work at the moment. I'm struggling with how to convert r and theta to x and y, and vice versa.
  • I don't look into CoordSf yet, but since CoordSf is so special, I think I won't be able to include it in this pull request.

@thomasp85
Copy link
Member

Is it possible to get this done for the next release (soon) or should we punt it?

@yutannihilation
Copy link
Member Author

I don't think I can finish CoordPolar and CoordSf, sorry. Is it an option to narrow the scope of this pull request only to CoordCartesian and CoordTrans?

@thomasp85
Copy link
Member

Yes, we can do that

@yutannihilation yutannihilation changed the title WIP: Support guide_axis() on all Coord WIP: Support guide_axis() on CoordTrans Jul 7, 2022
@yutannihilation
Copy link
Member Author

Thanks, I reverted the change on CoordPolar. Should be ready for review soon...

@yutannihilation yutannihilation changed the title WIP: Support guide_axis() on CoordTrans Support guide_axis() on CoordTrans Jul 7, 2022
@yutannihilation
Copy link
Member Author

Okay, I need to add some tests, but this should be basically ready for review now. I think I figured out how things should work.

y.sec = guide_axis(title = "y (secondary)")
)

expect_doppelganger("guide titles with coord_trans()", plot + coord_trans())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to test the label without adding a visual case because then we'll need many for testing similar cases for issues like #4650, but I couldn't find any nice way...

@yutannihilation yutannihilation requested a review from thomasp85 July 8, 2022 13:35
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but @paleolimbot may be good to loop in if he has time

@thomasp85 thomasp85 requested a review from paleolimbot July 26, 2022 20:24
Copy link
Member

@paleolimbot paleolimbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My memory of these internals is poor but I like the approach (move stuff from coord-cartesian to the base coord class where maybe it can be helpful in more future or current coord subclasses), and I appreciate the comments documenting the idiosyncrasies. Thank you!

@yutannihilation
Copy link
Member Author

Thank you for reviewing! I'm happy that I completed this at last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing position value in scale_ interacts strangely with coord_trans()
3 participants