-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Open
Labels
Description
geom_boxplot()
and geom_violin()
show inconsistent in behaviour; the former needs only one of x
or y
, while the latter insists on being supplied both. Whether or not this was intentional, it is annoying that two such similar geoms do not work consistently.
library("ggplot2")
df <- data.frame(x = rnorm(100))
# this works
df |>
ggplot(
aes(x = x)
) +
geom_boxplot()
# this doesn't, errors complaining about missing `y`
df |>
ggplot(
aes(x = x)
) +
geom_violin()
# can make geom_violin work
df |>
ggplot(
aes(x = x, y = "")
) +
geom_violin()
# geom_density works without a second variable
df |>
ggplot(
aes(x = x)
) +
geom_density()
The error with the first geom_violin()
example is:
Error in `geom_violin()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `compute_layer()`:
! `stat_ydensity()` requires the following missing aesthetics: y.
Run `rlang::last_trace()` to see where the error occurred.
I expected that geom_violin()
would work similarly to geom_boxplot()
and not require both x
and y
to be specified.
This is with:
> packageVersion("ggplot2")
[1] ‘3.5.2.9002’
Full reprex:
library("ggplot2")
df <- data.frame(x = rnorm(100))
# this works
df |>
ggplot(
aes(x = x)
) +
geom_boxplot()
# this doesn't, errors complaining about missing `y`
df |>
ggplot(
aes(x = x)
) +
geom_violin()
#> Error in `geom_violin()`:
#> ! Problem while computing stat.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_layer()`:
#> ! `stat_ydensity()` requires the following missing aesthetics: y.
# can make geom_violin work
df |>
ggplot(
aes(x = x, y = "")
) +
geom_violin()
# geom_density works without a second variable
df |>
ggplot(
aes(x = x)
) +
geom_density()
Created on 2025-09-10 with reprex v2.1.1