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
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.
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.
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
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:
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
arbitrarily choose angle = 0 (presumably this is as "reasonable" a default angle as grey is for a default color)
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?
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!
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).
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" ...
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.
Activity
batpigandme commentedon Jul 13, 2018
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 commentedon 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
coloraesthetic 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".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:
batpigandme commentedon Jul 13, 2018
This makes more sense (to me), since presumably a label has been passed, but an angle has not.
clauswilke commentedon Jul 13, 2018
Yes, I implement this behavior for themes. It would make sense to implement it for geoms as well.
hadley commentedon Jul 23, 2018
I think it makes more sense for this to be fixed in grid, not in ggplot2.
baderstine commentedon Jul 23, 2018
aha... in the source for geom-text.r, the actual function called is
textGrob(), andangleis passed to therot=parameter. So nothing to fix inggplot2. It appears thatgridis now part of base r?baderstine commentedon Sep 21, 2018
Not sure how I would request that to be fixed in grid...
clauswilke commentedon Sep 21, 2018
Send a friendly requests to @pmur002.
pmur002 commentedon 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 commentedon Sep 24, 2018
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=0to replacerot=NAyou 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 commentedon 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" ...
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.