I am interested in plotting the distribution of some continuous variables by groups using the geom_quasirandom()
function.
I would like to know how do plot only one side, for example only points on the right side rather than on both sides.
ggplot(iris,aes(Species, Sepal.Length)) geom_quasirandom()
I am interested in a plot like this. I am not interested in solutions based on geom_beeswarm
because I have a lot of datapoints and geom_beeswarm
just creates a crowded mess for my dataset. So any solution specific to geom_quasirandom()
will be helpful. Thanks.
CodePudding user response:
Here's one approach, pulling the guts out of a beeswarm and using dplyr to adjust these manually, and adding the Species labels back manually.
ggplot_build(ggplot(iris,aes(Species, Sepal.Length))
geom_quasirandom()) -> a
a$data[[1]] %>%
group_by(group) %>%
mutate(x_adj = group abs(x - median(x))) %>%
ungroup() %>%
ggplot(aes(x_adj, y))
geom_point()
scale_x_continuous(breaks = 1:3, labels = unique(iris$Species))
...or a hack to keep the automatic labeling by using an invisible layer:
...
ggplot(aes(x, y))
geom_point(alpha = 0)
geom_point(aes(x = x_adj))
CodePudding user response:
It's funny, the first example below is actually in the README for {ggbeeswarm} but it doesn't seem to work with my versions of R and the package.
library(tidyverse)
library(ggbeeswarm)
# doesn't look asymetric as shown in the vingette
ggplot(iris,aes(Species, Sepal.Length)) geom_beeswarm(side = 1L)
#> Warning: Ignoring unknown parameters: side
However using the {see} package you can get pretty close to what I think you're asking for.
library(see)
# this works if you turn off the violin half
ggplot(iris,aes(Species, Sepal.Length))
geom_violindot(fill = NA, color = NA, color_dots = "black", size_dots = 1, fill_dots = "black")
Created on 2022-02-08 by the reprex package (v2.0.1)