Skip to content

geom_text throws error when angle = NA #2757

Closed

Description

@baderstine

When using geom_text() if the angle parameter is set using a variable whose value can be NA then geom_text() throws an error (Error in validDetails.text(x) : invalid 'rot' value) when it encounters the NA value. Desired behavior would be to handle this gracefully and simply NOT plot the NA value (as would happen with other aesthetics).

library(ggplot2)
df <- data.frame(x=seq(1,10,1), y=seq(1,10,1), angle=c(seq(0,80,10),NA))

# this works:
ggplot(df[1:9,], aes(x=x, y=y)) + geom_text(label="-", aes(angle=angle))

# this breaks:
ggplot(df, aes(x=x, y=y)) + geom_text(label="-", aes(angle=angle))
# with error:
# Error in validDetails.text(x) : invalid 'rot' value
# also a confusing error because the user did not pass a 'rot' parameter.
# that 'rot' == 'rotation' == 'angle' may not be entirely clear to users.

image

Activity

  1. batpigandme commented on Jul 13, 2018

    @batpigandme
    Member

    Desired behavior would be to handle this gracefully and simply NOT plot the NA value (as would happen with other aesthetics).

    Possible hitch in this — with an optional aesthetic you would presumably just not pass a value to it at all if you don't want to use it. Because it's an angle of a label, if the label exists, but the angle does not, I'm not sure it would be expected the label not be plotted at all because of a missing angle.

  2. baderstine commented on Jul 13, 2018

    @baderstine
    Author

    I disagree that 'you would presumably just not pass a value to it at all if you don't want to use it'.
    This happens all the time with other aesthetics. For example, when an NA value is encountered by the color aesthetic it (rather arbitrarily) chooses grey and continues to make the plot. It doesn't throw an error that says "Error in validDetails.text(x) : invalid 'color' value".

    ggplot(df, aes(x=x, y=y)) + geom_text(label="-", aes(color=angle))
    # works perfectly fine

    image

    My point is that NA values to "angle" should be handled as gracefully as NA values to other aesthetics (whether optional or not), rather than prevent the plot from returning. I do not believe the function's job is to decide whether the user is doing something silly or not. It is to do what the user requested.

    If the user passes angle = NA, either:

    1. don't show the label (in my mind this is really what the user asked for, even if they may have done so mistakenly), or
    2. arbitrarily choose angle = 0 (presumably this is as "reasonable" a default angle as grey is for a default color)
  3. batpigandme commented on Jul 13, 2018

    @batpigandme
    Member

    arbitrarily choose angle = 0 (presumably this is as "reasonable" a default angle as grey is for a default color)

    This makes more sense (to me), since presumably a label has been passed, but an angle has not.

  4. clauswilke commented on Jul 13, 2018

    @clauswilke
    Member

    Yes, I implement this behavior for themes. It would make sense to implement it for geoms as well.

  5. hadley commented on Jul 23, 2018

    @hadley
    Member

    I think it makes more sense for this to be fixed in grid, not in ggplot2.

  6. baderstine commented on Jul 23, 2018

    @baderstine
    Author

    aha... in the source for geom-text.r, the actual function called is textGrob(), and angle is passed to the rot= parameter. So nothing to fix in ggplot2. It appears that grid is now part of base r?

  7. baderstine commented on Sep 21, 2018

    @baderstine
    Author

    Not sure how I would request that to be fixed in grid...

  8. clauswilke commented on Sep 21, 2018

    @clauswilke
    Member

    Send a friendly requests to @pmur002.

  9. pmur002 commented on Sep 23, 2018

    @pmur002
    Contributor

    You called ?

    I want to have a bit of a think about consistency here - as pointed out, col=NA draws nothing (usually), though I also buy the argument that it would be unfortunate that if you accidentally produced rot=NA that nothing gets drawn (silently). But this is now on my list!

  10. baderstine commented on Sep 24, 2018

    @baderstine
    Author

    I've updated my previous comments with images of the plots that are produced. To be clear, col=NA does appear to plot the point as I showed above... "when an NA value is encountered by the color aesthetic it (rather arbitrarily) chooses grey and continues to make the plot." (silently)

    To be honest, I was thinking of the very specific use case of wind direction plots. In this use case, if you choose rot=0 to replace rot=NA you might implying a wind direction exists where one doesn't. In other use cases, arbitrarily using 0 when rot=NA may be perfectly fine. I think it makes sense to do what other aesthetics do (i.e., don't exit with an error).

  11. pmur002 commented on Sep 25, 2018

    @pmur002
    Contributor

    Thanks for clarifying. The color=NA converting to grey is, I think, a 'ggplot2' behaviour (possibly an attempt to represent missing values rather than just drop them?). In the core graphics ('graphics' or 'grid'), col=NA translates to "transparent" ...

    plot(df$x, df$y, col=df$angle/10 + 1, pch=16)
    
    grid.newpage()
    pushViewport(plotViewport(), dataViewport(df$x, df$y))
    grid.rect()
    grid.xaxis()
    grid.yaxis()
    grid.points(df$x, df$y, gp=gpar(col=df$angle/10+1), pch=16)
    

    There is still a good argument for having rot=NA produce something other than an error (in 'grid'), but there may also be a 'ggplot2' layer of behaviour to add on top of that.

  12. added
    feature
    on Jun 17, 2019
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

    featurelayers 📈

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions