Closed gael-millot closed 4 years ago
Of note, group
works fine when replacing the am
category by vs
(a single qualitative variable plotted instead of two):
ggplot2::ggplot()+ggplot2::geom_dotplot(
data = mtcars,
mapping = ggplot2::aes(x = vs, y = mpg, group = vs),
position = ggplot2::position_dodge(),
binaxis = "y",
stackdir = "center",
show.legend = TRUE
)
As if, with two categories, group
takes the "average" position of A
and B
and then separates correctly between G
and H
...
Is this behavior expected?
Yes, this is an expected behaviour. My previous comment might be helpful to understand how the grouping works.
https://github.com/tidyverse/ggplot2/issues/3486#issuecomment-522210017
Many thanks for the answer ! It indeed indicates that the best is to create a new categorical colum:
# nothing changed in the code, compared to the 1st example, except the group aesthetic
ggplot2::ggplot()+ggplot2::geom_dotplot(
data = mtcars,
mapping = ggplot2::aes(x = vs, y = mpg, group = paste(vs, am, sep = ".")),
position = ggplot2::position_dodge(),
binaxis = "y",
stackdir = "center",
show.legend = TRUE
)
It is also interesting to mention that group
presents different behaviors, depending on the function used:
# test of geom_point using vs, mpg and am columns of mtcars
ggplot2::ggplot()+ggplot2::geom_point(
data = mtcars,
mapping = ggplot2::aes(x = vs, y = mpg, group = am),
)
# test of geom_dotplot in the same situation
ggplot2::ggplot()+ggplot2::geom_dotplot(
data = mtcars,
mapping = ggplot2::aes(x = vs, y = mpg, group = am),
binaxis = "y"
)
Thus, it could be worth to make a special section mentioning some particularities of group
in the ggplot2 documentation... here for instance ?
https://ggplot2.tidyverse.org/reference/aes_group_order.html
Thanks !
Ah, it might be a bit clearer if aes_group_order
doc shows an example case when both group
and other discrete variables are mapped. Feel free to contribute :)
Is this behavior expected?
Because other aesthetics gives the appropriate result, for instance when
group
is replaced byfill
:The legend does not appear with
group
aesthetic, as expected, but the dot positions seem totally incoherent. It would be great if I could usegroup
with the correct dot positioning.Thanks for your help !
Best.