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

geom_text throws error when angle = NA #2757

Closed
baderstine opened this issue Jul 13, 2018 · 11 comments · Fixed by #5162
Closed

geom_text throws error when angle = NA #2757

baderstine opened this issue Jul 13, 2018 · 11 comments · Fixed by #5162
Labels
feature a feature request or enhancement layers 📈

Comments

@baderstine
Copy link

baderstine commented Jul 13, 2018

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

@batpigandme
Copy link
Contributor

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.

@baderstine
Copy link
Author

baderstine commented Jul 13, 2018

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)

@batpigandme
Copy link
Contributor

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.

@clauswilke
Copy link
Member

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

@hadley
Copy link
Member

hadley commented Jul 23, 2018

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

@baderstine
Copy link
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?

@baderstine
Copy link
Author

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

@clauswilke
Copy link
Member

Send a friendly requests to @pmur002.

@pmur002
Copy link
Contributor

pmur002 commented Sep 23, 2018

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!

@baderstine
Copy link
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).

@pmur002
Copy link
Contributor

pmur002 commented Sep 25, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants