ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

coord_trans/polar() ignore position guides

Open teunbrand opened this issue 4 years ago • 4 comments

While I was trying to making a variant of guide_axis() that displays tick marks at minor breaks (see for example SO), I stumbled on a possible bug in coord_trans() and coord_polar(), which appear to ignore position guides.

Standard cartesian coordinates seem to work just fine.

library(ggplot2)

p <- ggplot(mpg, aes(hwy, cty)) +
  geom_point() +
  guides(x = guide_axis("custom axis"))

p + coord_cartesian()

Polar coordinates don't render the correct axis title.


p + coord_polar()

Transformed coordinates also don't render the correct axis title.


p + coord_trans()

Created on 2020-04-22 by the reprex package (v0.3.0)

The same pattern holds if I specify scale_x_continuous(guide = guide_axis("custom axis)) or use guides(x.sec = guide_axis("custom axis")).

teunbrand avatar Apr 22 '20 21:04 teunbrand

Thanks, confirmed.

Currently, the title of guide is referenced only by CoordCartesian$label() via panel_guide_label():

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-cartesian-.r#L154-L173

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-cartesian-.r#L217-L220

The other Coord classes use the inherited Coord$label():

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-.r#L62

But, it seems we cannot simply transplant the label(). panel_params doesn't contain guides by default, so we need setup_panel_guides() to add it.

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-cartesian-.r#L134

Besides, guides needs to have position, otherwise guide_for_position(guides, position) returns NULL.

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-cartesian-.r#L128-L130

To enable this, we need to include scale in panel_params, which is currently done only in CoordCartesian:

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-cartesian-.r#L99-L104

e.g. CoordTrans doesn't include ViewScale.

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-transform.r#L138-L143

https://github.com/tidyverse/ggplot2/blob/d3d47be055055ebf328aeed0bebb990a635c49ab/R/coord-transform.r#L217-L227

@paleolimbot I didn't actively follow the discussion about ViewScale (sorry...), do you think we can support position guides on other coords than CoordCartesian?

yutannihilation avatar Apr 23 '20 00:04 yutannihilation

No prob! The ViewScale exists because of the SecAxis...both get a ViewScale even though there's only one Scale. I'd prefer the ViewScale not to exist (and have second axes live in the guides framework), but it's designed to be interface-compatible with Scale so that we don't have to rewrite everything if we go that direction.

I think the general idea was to test the guide_axis() stuff with CoordCartesian, but there's no reason (I think) that it couldn't be implemented in the other coords as well. If that's something that would be welcome (and the PR would get reviewed), I could give it a shot (or you could...no problem either way).

paleolimbot avatar Apr 23 '20 00:04 paleolimbot

Thanks, then let me try a PR this time. This would be a nice lesson to get familiar with coords and guides (and even if I fail, the experience should be useful to review your PRs...).

yutannihilation avatar Apr 23 '20 04:04 yutannihilation

coord_trans() was fixed by #3972. coord_polar() (and other coords) stays the same.

yutannihilation avatar Jul 27 '22 00:07 yutannihilation