erocoar / gghalves

✂️ Easy half-half geoms in ggplot2
https://erocoar.github.io/gghalves/
Other
241 stars 13 forks source link

Is it possible to draw all the points together but color them separately? #4

Closed tungttnguyen closed 4 years ago

tungttnguyen commented 4 years ago

Happy Friday,

A follow-up from this issue. Can we somehow get the combination of the 1st & 2nd plots?

Plot all the points together


library(ggplot2)
library(ggbeeswarm)
library(ggpol)
library(gghalves)

h1 <- ggplot(iris, aes(y = Petal.Width)) +
  geom_boxjitter(
    errorbar.draw = TRUE,
    jitter.position = ggbeeswarm:::PositionQuasirandom,
    jitter.params = formals(position_quasirandom)) +
  theme_classic()
h1

With geom_half_point(), points are now colored differently but they are also not together anymore


h2 <- ggplot(iris, aes(y = Sepal.Width)) +
  geom_half_boxplot() +
  geom_half_point(aes(x = 0.5, color = Species),
                  transformation = ggbeeswarm:::PositionQuasirandom,
                  transformation_params = formals(ggbeeswarm::position_quasirandom)) +
  theme_classic()
h2


h3 <- ggplot(iris, aes(y = Sepal.Width)) +
  geom_half_boxplot() +
  geom_half_point(aes(color = Species),
                  transformation = ggbeeswarm:::PositionQuasirandom,
                  transformation_params = formals(ggbeeswarm::position_quasirandom)) +
  theme_classic()
h3

Created on 2019-10-11 by the reprex package (v0.3.0)

erocoar commented 4 years ago

I have now implemented this by adding a geom_half_point_panel, where instead of per grouping, the entire panel is computed at once, meaning that you can use all points for computing the position (quasirandom, jitter, beeswarm) but then differentiate them via color, shape etc. - see the example:

library(ggplot2)
library(gghalves)
ggplot(iris, aes(y = Sepal.Width)) +
  geom_half_boxplot() +
  geom_half_point_panel(aes(x = 0.5, color = Species),
                  transformation = ggbeeswarm:::PositionQuasirandom,
                  transformation_params = formals(ggbeeswarm::position_quasirandom)) +
  theme_classic()

Created on 2020-02-26 by the reprex package (v0.3.0)

tungttnguyen commented 4 years ago

Thank you @erocoar !