Description
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")
The following code produces different jitter:
set.seed(1234)
ggplot(df, aes(x, y)) + geom_point(position = position_jitter())
However, this reproduces the jitter from the first example:
set.seed(1234)
ggplot(df, aes(x, y)) + geom_point(position = position_jitter(seed = NULL))
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:
-
The simple way: Don't set
seed = NA
as default inposition_jitter()
. -
The complicated way: Fix the ggproto behavior so the default arguments apply when objects are specified by their name.