Skip to content

Unexpected results using guides(x = guide_axis(position = ...)) #4650

Closed

Description

@jtlandis

When attempting to change the axis position with guide_axis it seems that the title is not correctly pulled unless manually specified.

library(ggplot2)

p <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
  geom_point() 

Currently you can swap the placement with scale_(x|y)_*(postion = ...) pretty reliably

p + scale_x_continuous(position = "top")

But trying to swap scales with guides() doesn’t seem to include the title

p + guides(x = guide_axis(position = "top"))

Even though the argument title = waiver() it doesn’t seem to work. But works when manually set.

p + guides(x = guide_axis(position = "top", title = "test"))

Created on 2021-10-27 by the reprex package (v2.0.1)

Activity

  1. added
    bug
    on Oct 28, 2021
  2. thomasp85 commented on Nov 9, 2021

    @thomasp85
    Member

    @paleolimbot is this something you have time to look at (if not, that is fair)? I guess it is mainly your code

  3. paleolimbot commented on Nov 11, 2021

    @paleolimbot
    Member

    Definitely my code! I'll have a look at this tomorrow.

  4. paleolimbot commented on Nov 11, 2021

    @paleolimbot
    Member

    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
    library(ggplot2)
    
    p <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
      geom_point()
    
    # defaults
    p

    # with setting position in scale
    p +
      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 explicitly
    p + 
      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 axis
    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL)) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit x title")) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit y title"))

    Created on 2021-11-11 by the reprex package (v2.0.1)

    # ...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()
    
    # defaults
    p

    # with setting position in scale
    p +
      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 explicitly
    p + 
      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 axis
    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL)) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit x title")) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit y title"))

    Created on 2021-11-11 by the reprex package (v2.0.1)

  5. hadley commented on Apr 19, 2022

    @hadley
    Member

    Somewhat more minimal reprex:

    library(ggplot2)
    
    df <- data.frame(x = 1:2, y = 1:2)
    
    ggplot(df, aes(x, y)) + 
      geom_point() + 
      labs(x = "Long axis label") + 
      guides(x = guide_axis(position = "top"))

    Created on 2022-04-19 by the reprex package (v2.0.1)

  6. yutannihilation commented on Jul 7, 2022

    @yutannihilation
    Member

    While #3972 will provide a slightly better result (at least the axis title won't disappear), it seems something is still wrong.

    devtools::load_all("~/GitHub/ggplot2")
    #> ℹ Loading ggplot2
    
    df <- data.frame(x = 1:2, y = 1:2)
    
    ggplot(df, aes(x, y)) + 
      geom_point() + 
      labs(x = "Long axis label") + 
      guides(x = guide_axis(position = "top"))

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

  7. yutannihilation commented on Jul 8, 2022

    @yutannihilation
    Member

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

    ff2e38f

    devtools::load_all("~/GitHub/ggplot2")
    #> ℹ Loading ggplot2
    
    df <- data.frame(x = 1:2, y = 1:2)
    
    ggplot(df, aes(x, y)) + 
      geom_point() + 
      labs(x = "Long axis label") + 
      guides(x = guide_axis(position = "top"))

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

    Details
    devtools::load_all("~/GitHub/ggplot2")
    #> ℹ Loading ggplot2
    
    p <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
      geom_point()
    
    # defaults
    p

    # with setting position in scale
    p +
      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 explicitly
    p + 
      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 axis
    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = waiver()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive())) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = derive()))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL)) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = NULL))

    p + 
      scale_x_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit x title")) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 10, name = "explicit y title"))

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

  8. teunbrand commented on Aug 11, 2022

    @teunbrand
    Collaborator

    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.

  9. teunbrand commented on Aug 13, 2022

    @teunbrand
    Collaborator

    Another thing I encountered is the following:

    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?

    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.sec guide implicitly has (or should get) position = "bottom".

  10. teunbrand commented on Aug 13, 2022

    @teunbrand
    Collaborator

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

  11. added a commit that references this issue on Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugguides 📏

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions