-
Notifications
You must be signed in to change notification settings - Fork 2.1k
position_jitterdodge() failed when attribute fill
is not specified
#3656
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
Comments
Likely related to #3612, though it's surprising that setting the |
This seems to be happening specifically because 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) |
@karawoo do you have any knowledge about this logic? ggplot2/R/position-jitterdodge.R Lines 50 to 55 in 87e9b85
Why is |
I haven't touched jitterdodge so I don't really know, but looking at it I can't see any reason why |
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 |
I think the data should only be dodged by group, not any of the other variables. library(ggplot2)
p <- ggplot(mpg, aes("x", displ)) +
geom_point(position = position_jitterdodge())
# Throws error
p
#> Error in `geom_point()`:
#> ! Problem while computing position.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `setup_params()`:
#> ! `position_jitterdodge()` requires at least one aesthetic to dodge by
# Too much jitter.width?
p + aes(colour = drv) # Throws error
p + aes(group = drv)
#> Error in `geom_point()`:
#> ! Problem while computing position.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `setup_params()`:
#> ! `position_jitterdodge()` requires at least one aesthetic to dodge by Created on 2024-05-31 with reprex v2.1.0 |
Also, if you use a string, e.g. library(ggplot2)
# What it should do
ggplot(mpg, aes(factor(cyl), displ, colour = factor(year))) +
geom_point(position = position_jitterdodge()) ggplot(mpg, aes(factor(cyl), displ, colour = factor(year))) +
geom_point(position = "jitterdodge")
#> Error in `geom_point()`:
#> ! Problem while computing position.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `if (params$jitter.height > 0) ...`:
#> ! argument is of length zero Created on 2024-05-31 with reprex v2.1.0 |
Hello,
I was trying to produce a scatter plot with points labelled by the variable
text
and dodged by the color variablegroup
. Theposition_dodge()
function worked fine, as shown in the 1st figure below. But when I tried to jitter the points usingposition_jitterdodge()
, this function would fail unless I added thefill
attribute in theaes()
. 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.
#> Error:
position_jitterdodge()
requires at least one aesthetic to dodge byCreated on 2019-12-04 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: