ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

misleading error message in geom_dotplot

Open luketudge opened this issue 4 years ago • 1 comments

When the y aesthetic is mapped inappropriately for geom_dotplot() the error message states that the required y aesthetic is missing. At least from the point of view of the user's original call, y is neither required nor missing.

library(ggplot2)
d <- data.frame(A = rep(1:3, times = 10))

ggplot(d, aes(x = A)) +
  geom_dotplot()
## No error here. Neither does y seem to be required.

# Now map y to something spurious and inappropriate:
ggplot(d, aes(x = A, y = A + 1)) +
  geom_dotplot()
Error: geom_dotplot requires the following missing aesthetics: y

I guess this is a very minor issue. But I have really come to appreciate the tidyverse's informative error messages, so I thought I'd report this in case there is an easy fix.

luketudge avatar Jul 27 '21 21:07 luketudge

During attempting to fix this in #4914, I found I don't understand the semantics of dotplot. Shouldn't the example above work even when both x and y is specified? I don't see why it works only when binaxis = "y".

library(ggplot2)
d <- data.frame(A = rep(1:3, times = 10))

# fail (default)
ggplot(d, aes(x = A, y = A)) +
  geom_dotplot(binaxis = "x")
#> Bin width defaults to 1/30 of the range of the data. Pick better value with
#> `binwidth`.
#> Warning: The following aesthetics were dropped during statistical transformation: y
#> ℹ This can happen when ggplot fails to infer the correct grouping structure in
#>   the data.
#> ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
#>   variable into a factor?
#> Error in `geom_dotplot(binaxis = "x")`:
#> ! Problem while setting up geom.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_geom_1()` at ggplot2/R/ggproto.r:182:16:
#> ! `geom_dotplot()` requires the following missing aesthetics: y

# success
ggplot(d, aes(x = A, y = A)) +
  geom_dotplot(binaxis = "y")
#> Bin width defaults to 1/30 of the range of the data. Pick better value with
#> `binwidth`.

Created on 2022-07-23 by the reprex package (v2.0.1)

yutannihilation avatar Jul 23 '22 12:07 yutannihilation