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 labels misaligned after coord_polar #3185

Closed
linzi-sg opened this issue Mar 14, 2019 · 3 comments · Fixed by #3278
Closed

Secondary axis labels misaligned after coord_polar #3185

linzi-sg opened this issue Mar 14, 2019 · 3 comments · Fixed by #3278
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@linzi-sg
Copy link


When converting a plot with secondary axis transformation from Cartesian to polar coordinates, I noticed two issues:

  1. Label positions for the secondary axis become mis-aligned to match those of the primary axis;

  2. When the secondary axis had fewer labels than the primary axis, the secondary axis label values are recycled to fill all the positions used by primary axis labels.

Reprex with annotated plots below:

library(ggplot2)

p <- ggplot(data.frame(x = LETTERS[1:3],
                       y = c(19, 22, 14)),
            aes(x, y)) +
  geom_col() +
  scale_y_continuous(
    name = "miles per gallon",
    sec.axis = sec_axis(name = "km per litre",
                        trans = ~. * 0.425))

cowplot::plot_grid(
  p + ggtitle("Correct labels"), 
  p + ggtitle("Wrong labels") +
    coord_polar(), 
  ncol = 2)

annotated plot

I suspect this is caused by the render_axis_v function in CoordPolar, more specifically the following section, which sets the axis label positions of the secondary axis to match those of the primary axis:

x <- r_rescale(self, panel_params$r.major, panel_params) + 0.5
panel_params$r.major <- x
if (!is.null(panel_params$r.sec.major)) {
  panel_params$r.sec.major <- x
}

I was able to get the expected result when I modified the function to replace panel_params$r.sec.major <- x with the following line (essentially repeating the calculation done in r_rescale for secondary axis breaks & range):

panel_params$r.sec.major <- scales::rescale(panel_params$r.sec.major, 
                                            c(0, 0.4), 
                                            panel_params$r.sec.range) + 0.5

modified coord_polar function

@thomasp85 thomasp85 added the bug an unexpected problem or unintended behavior label Apr 11, 2019
@thomasp85
Copy link
Member

@dpseidel you are the latest one to look at secondary axes - can you have a look?

@thomasp85 thomasp85 added this to the ggplot2 3.2.0 milestone Apr 11, 2019
@dpseidel
Copy link
Collaborator

Yep. I'll take a look asap.

@lock
Copy link

lock bot commented Oct 27, 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/

@lock lock bot locked and limited conversation to collaborators Oct 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants