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

`position_nudge` doesn't work when called as `position = "nudge"` #1428

Closed
garrettgman opened this Issue Dec 11, 2015 · 6 comments

Comments

Projects
None yet
3 participants
@garrettgman
Member

garrettgman commented Dec 11, 2015

Here's the example from ?position_nudge, which works.

df <- data.frame(
  x = c(1,3,2,5),
  y = c("a","c","d","c")
)

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y))

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), position = position_nudge(y = -0.1))

But the code breaks when you call position = "nudge", as you might do with other positions.

ggplot(df, aes(x, y)) +
  geom_point() +
  geom_text(aes(label = y), position = "nudge")

There's no warning message, the text layer just disappears.

This is bad for me because I'd prefer to be able to teach beginners all five position adjustments without having to teach them the position = position_nudge() syntax. The second syntax is a bit overwhelming for someone new to R since it contains a function inside an argument. (We begin new R students with ggplot2 because visualizations are cool.)

@hadley

This comment has been minimized.

Member

hadley commented Dec 12, 2015

Why don't you just call nudge_y = -0.1? I think I've tweaked the geoms enough that you shouldn't need to teach the long position form at all in an intro call - you can use nudge_x and nudge_y in geom_jitter(), and width and height in geom_jitter().

@hadley hadley closed this Dec 12, 2015

@garrettgman

This comment has been minimized.

Member

garrettgman commented Dec 12, 2015

I'm interested in teaching students how to think about position adjustments. I'm happy to give them shortcuts, but I want the principle to work as well.

nudge_y = -0.1 is a great feature. But this pattern makes ggplot2 seem confusing and inconsistent:

  • position = "jitter" - does what you expect
  • position = "dodge" - does what you expect
  • position = "stack" - does what you expect
  • position = "identity" - does what you expect
  • position = "fill" - does what you expect
  • position = "nudge" - silent fail
@garrettgman

This comment has been minimized.

Member

garrettgman commented Dec 13, 2015

Sorry to be a gadfly here.

Thinking about this more, I think that position_nudge() is not a proper position adjustment since it does not handle collisions within the same layer. This suggests that nudge_x and nudge_y might fit into the grammar as parameters of position_identity(), e.g. position_identity(nudge_x = 0, nudge_y = 0).

That solution would take effort to implement, but then we'd:

  • avoid position = "nudge"
  • retain the x = 0 and y = 0 defaults mentioned in issue #1429
  • help ensure that everything in the grammar of graphics still applies to ggplot2
  • be able to teach nudge at a higher (e.g. theoretical) level because it meshes with the grammar
  • avoid confusing users by suggesting that syntax patterns that work for positions adjustments would also work for nudge.

Alternatively, you could make nudge_x and nudge_y parameters of a layer (in the abstract).

@has2k1

This comment has been minimized.

Contributor

has2k1 commented Dec 15, 2015

@garrettgman, Both position_jitter and position_nudge do not handle collisions. Collision detection and handling is not a necessity for position adjustments, rather it is just what some position adjustments will also do to give a sensible looking plot.

About the default values for position_nudge, zero-zero nudge does indeed seem to be the best option.

nudge_x nudge_y
0 0
0 *
* 0
* *

As the main use-case for nudging is when labelling, a useful nudge value will most likely be plot specific and in many cases along a single axis. If you try to have a default non-zero constant adjustment along any or both axes, then you force users to override both.

@garrettgman

This comment has been minimized.

Member

garrettgman commented Dec 15, 2015

@has2k1 your argument is defensible. I think it depends on how far you want to stray from Leland's grammar which called position adjustments "collision modifiers." Leland's grammar can certainly be improved upon; it didn't have layers for example. This may be one of those improvements.

However, position = "nudge" still fails with no error message (it silently removes the text geom from the plot), which makes me feel that this issue is not completely resolved.

@hadley

This comment has been minimized.

Member

hadley commented Dec 15, 2015

Ok, that is a bug. I'll fix in the next release.

@hadley hadley reopened this Dec 15, 2015

@hadley hadley added the bug label Dec 15, 2015

@hadley hadley added this to the v2.0.1 milestone Dec 15, 2015

@hadley hadley closed this in b4ae84b Jan 26, 2016

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.