Closed
Description
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)