Skip to content

position = "jitter" and position = position_jitter() have different behavior #2507

Closed
@clauswilke

Description

@clauswilke

I noticed today that position = "jitter" is not the same as position = position_jitter(), and the behavior described in the documentation for position = position_jitter() cannot be obtained for position = "jitter". The underlying issue is a ggproto behavior that has caught me off guard many times (see below).

First the reprex. We start with a simple plot with jitter:

df <- data.frame(x = c(1, 2, 1, 2),
                 y = c(1, 1, 2, 2))

set.seed(1234)
ggplot(df, aes(x, y)) + geom_point(position = "jitter")

screen shot 2018-04-01 at 12 11 41 am

The following code produces different jitter:

set.seed(1234)
ggplot(df, aes(x, y)) + geom_point(position = position_jitter())

screen shot 2018-04-01 at 12 11 51 am

However, this reproduces the jitter from the first example:

set.seed(1234)
ggplot(df, aes(x, y)) + geom_point(position = position_jitter(seed = NULL))

screen shot 2018-04-01 at 12 11 58 am

The problem is that position = "jitter" does not use the default arguments defined in position_jitter(), and therefore seed is set to NULL, not to NA, when we use position = "jitter". This same problem happens generally with ggproto, e.g. when we call a geom with geom_*() vs. geom = "*", and it has bitten me many times in different scenarios.

There are two ways to fix this particular problem:

  1. The simple way: Don't set seed = NA as default in position_jitter().

  2. The complicated way: Fix the ggproto behavior so the default arguments apply when objects are specified by their name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorpositions 🥇

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions