You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unexpected results using guides(x = guide_axis(position = ...)) #4650
Ok...I did a bit of sleuthing. I'm going to suggest that this not get fixed until (1) second axes have been deprecated in favour of axis guides (with a transform) and (2) the axis guides are being used on all the Coord subclasses. Or perhaps one of you can see a better solution that's staring me in the nose!
# with setting position in scalep+
scale_x_continuous(position="top") +
scale_y_continuous(position="right")
# when title = waiver() and specified in guides()p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver()),
)
# when title set explicitlyp+
guides(
x= guide_axis(position="top", title="explicit x title"),
y= guide_axis(position="right", title="explicit y title"),
)
# what happens if you set labels(x, y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
x="x label specified in labels()",
y="y label specified in labels()"
)
# what happens if you set labels(sec.x, sec.y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
sec.x="sec.x label specified in labels()",
sec.y="sec.y label specified in labels()"
)
# what happens when you specify a second axisp+
scale_x_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver())) +
scale_y_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver()))
# ...and what happens when you do literally all of that using coord_flip()
library(ggplot2)
p<- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
coord_flip()
# defaultsp
# with setting position in scalep+
scale_x_continuous(position="top") +
scale_y_continuous(position="right")
# when title = waiver() and specified in guides()p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver()),
)
# when title set explicitlyp+
guides(
x= guide_axis(position="top", title="explicit x title"),
y= guide_axis(position="right", title="explicit y title"),
)
# what happens if you set labels(x, y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
x="x label specified in labels()",
y="y label specified in labels()"
)
# what happens if you set labels(sec.x, sec.y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
sec.x="sec.x label specified in labels()",
sec.y="sec.y label specified in labels()"
)
# what happens when you specify a second axisp+
scale_x_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver())) +
scale_y_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver()))
Adding some tweak to Layout$xlabel() and Layout$ylabel() seems to work at least with CoordCartesian`. Not sure if I can push this into the next release at the moment...
# with setting position in scalep+
scale_x_continuous(position="top") +
scale_y_continuous(position="right")
# when title = waiver() and specified in guides()p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver()),
)
# when title set explicitlyp+
guides(
x= guide_axis(position="top", title="explicit x title"),
y= guide_axis(position="right", title="explicit y title"),
)
# what happens if you set labels(x, y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
x="x label specified in labels()",
y="y label specified in labels()"
)
# what happens if you set labels(sec.x, sec.y)p+
guides(
x= guide_axis(position="top", title= waiver()),
y= guide_axis(position="right", title= waiver())
) +
labs(
sec.x="sec.x label specified in labels()",
sec.y="sec.y label specified in labels()"
)
# what happens when you specify a second axisp+
scale_x_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver())) +
scale_y_continuous(sec.axis= sec_axis(trans=~.*10, name= waiver()))
I can probably take this along in #4879. It appears as if we can drop the panel_guide_label and Coord$labels, if we evaluate the guide titles in Layout$xlabel and Layout$ylabel, which puts us in a position to fix this issue.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.1
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
guides(x= guide_axis(position="top", title="Foo"),
x.sec= guide_axis(title="Bar"))
#> Warning: guide_axis(): Discarding guide on merge. Do you have more than one#> guide with the same position?#> Warning: guide_axis(): Discarding guide on merge. Do you have more than one#> guide with the same position?
This should work now with the PR, but it is nowhere near finished yet. I've taken an approach similar to yutannihilation's approach (thanks for the inspiration!), but with some extra finagling around for coord_flip().
Activity
thomasp85 commentedon Nov 9, 2021
@paleolimbot is this something you have time to look at (if not, that is fair)? I guess it is mainly your code
paleolimbot commentedon Nov 11, 2021
Definitely my code! I'll have a look at this tomorrow.
paleolimbot commentedon Nov 11, 2021
Ok...I did a bit of sleuthing. I'm going to suggest that this not get fixed until (1) second axes have been deprecated in favour of axis guides (with a transform) and (2) the axis guides are being used on all the Coord subclasses. Or perhaps one of you can see a better solution that's staring me in the nose!
Relevant code:
https://github.com/tidyverse/ggplot2/blob/main/R/coord-cartesian-.r#L154-L173
https://github.com/tidyverse/ggplot2/blob/main/R/layout.R#L234-L258
Implementation that fixes this problem but creates many more:
main...paleolimbot:issue-4650-position-scale-label
Situations to consider:
Details
Created on 2021-11-11 by the reprex package (v2.0.1)
Created on 2021-11-11 by the reprex package (v2.0.1)
hadley commentedon Apr 19, 2022
Somewhat more minimal reprex:
Created on 2022-04-19 by the reprex package (v2.0.1)
yutannihilation commentedon Jul 7, 2022
While #3972 will provide a slightly better result (at least the axis title won't disappear), it seems something is still wrong.
Created on 2022-07-07 by the reprex package (v2.0.1)
yutannihilation commentedon Jul 8, 2022
Adding some tweak to
Layout$xlabel()andLayout$ylabel() seems to work at least withCoordCartesian`. Not sure if I can push this into the next release at the moment...ff2e38f
Created on 2022-07-08 by the reprex package (v2.0.1)
Details
Created on 2022-07-08 by the reprex package (v2.0.1)
teunbrand commentedon Aug 11, 2022
I can probably take this along in #4879. It appears as if we can drop the
panel_guide_labelandCoord$labels, if we evaluate the guide titles inLayout$xlabelandLayout$ylabel, which puts us in a position to fix this issue.teunbrand commentedon Aug 13, 2022
Another thing I encountered is the following:
Created on 2022-08-13 by the reprex package (v2.0.1)
Where I'd think it is not unreasonable to presume that the
x.secguide implicitly has (or should get)position = "bottom".teunbrand commentedon Aug 13, 2022
This should work now with the PR, but it is nowhere near finished yet. I've taken an approach similar to yutannihilation's approach (thanks for the inspiration!), but with some extra finagling around for
coord_flip().Convert guides to ggproto (#4879)