ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

position_jitterdodge() failed when attribute `fill` is not specified

Open qxxxd opened this issue 6 years ago • 5 comments

Hello,

I was trying to produce a scatter plot with points labelled by the variable text and dodged by the color variable group. The position_dodge() function worked fine, as shown in the 1st figure below. But when I tried to jitter the points using position_jitterdodge(), this function would fail unless I added the fill attribute in the aes(). The desired output is shown in the 2nd figure below.

The version of ggplot2 is 3.2.1.

I'm not sure if this is a bug or intended?

Thank you for any help.

library(ggplot2)
# pseudo data
set.seed(123)
d <- data.frame(x = rep(as.character(1:3), each = 4),
                y = rnorm(12),
                group = rep(as.character(1:2), 6),
                text = rep(LETTERS[1:4], 3))

# using position_jitter()
pos <- position_jitter(width = 0.2, 
                   seed = 10)
ggplot(d, aes(x= x, y = y,
              color = group,
              label = text)) +
  geom_point(position = pos, size = 5) + 
  geom_text(position = pos, size = 4, color = "white")


# using position_jitterdodge() WITHOUT `fill` attribute
pos <- position_jitterdodge(jitter.width = 0.2,
                            dodge.width = 0.5,
                            seed = 10)
ggplot(d, aes(x= x, y = y,
              color = group,
              label = text)) +
  geom_point(position = pos, size = 5) + 
  geom_text(position = pos, size = 4, color = "white")

#> Error: position_jitterdodge() requires at least one aesthetic to dodge by

# using position_jitterdodge() WITH `fill` attribute
ggplot(d, aes(x= x, y = y,
              fill = group,
              color = group,
              label = text)) +
  geom_point(position = pos, size = 5) + 
  geom_text(position = pos, size = 4, color = "white")

Created on 2019-12-04 by the reprex package (v0.3.0)

qxxxd avatar Dec 05 '19 02:12 qxxxd

Likely related to #3612, though it's surprising that setting the color aesthetic is not sufficient here.

clauswilke avatar Dec 05 '19 03:12 clauswilke

This seems to be happening specifically because color is set manually in geom_text(). However, surprisingly, setting a group aesthetic does not fix things here (unlike in #3612).

library(ggplot2)

# pseudo data
set.seed(123)
d <- data.frame(x = rep(as.character(1:3), each = 4),
                y = rnorm(12),
                group = rep(as.character(1:2), 6),
                text = rep(LETTERS[1:4], 3))

pos <- position_jitterdodge(jitter.width = 0.2,
                            dodge.width = 0.5,
                            seed = 10)

# works
ggplot(d, aes(x= x, y = y,
              color = group,
              label = text)) +
  geom_text(position = pos, size = 4)


# doesn't work, color is set explicitly
ggplot(d, aes(x= x, y = y,
              color = group,
              label = text)) +
  geom_text(position = pos, size = 4, color = "black")
#> Error: `position_jitterdodge()` requires at least one aesthetic to dodge by
# works
ggplot(d, aes(x= x, y = y,
              color = group,
              fill = group,
              label = text)) +
  geom_text(position = pos, size = 4, color = "black")


# doesn't work
ggplot(d, aes(x= x, y = y,
              color = group,
              group = group,
              label = text)) +
  geom_text(position = pos, size = 4, color = "black")
#> Error: `position_jitterdodge()` requires at least one aesthetic to dodge by

Created on 2019-12-04 by the reprex package (v0.3.0)

clauswilke avatar Dec 05 '19 03:12 clauswilke

@karawoo do you have any knowledge about this logic?

https://github.com/tidyverse/ggplot2/blob/87e9b85dd9f2a294f339d88a353d0c11c851489d/R/position-jitterdodge.R#L50-L55

Why is group ignored here? Sorry if this not your table but you are the "dodge-master" in my head 🙂

thomasp85 avatar Apr 20 '21 08:04 thomasp85

I haven't touched jitterdodge so I don't really know, but looking at it I can't see any reason why "group" should not be included.

karawoo avatar Apr 20 '21 19:04 karawoo

The only thing I can see is that group will be derived from the other aesthetics if not given explicitly which may lead to some sort of compounding in the two next calls...

I'll try to experiment with it and see if it have any negative consequences

thomasp85 avatar Apr 21 '21 10:04 thomasp85