Problem
We would like to restrain the scattered jitter points within a violin plot using ggplot2.
library(tidyverse)
p <- ggplot(mpg, aes(class, hwy))
p + geom_violin() + geom_jitter()
Solution
- Option 1 Not a completely satisfactory option, because by restricting the horizontal jitter we defeat the purpose of handling overplotting. But we can enlarge the width of the violin plots (width = 1.3), and play with alpha for transparency and limit the horizontal jitter (width = .02).
p + geom_violin(width = 1.3) + geom_jitter(alpha = 0.2, width = .02)
The quasirandom geom is a convenient means to offset points within categories to reduce overplotting. Uses the vipor package
library(ggbeeswarm)
p + geom_violin(width = 1.3) + geom_quasirandom(alpha = 0.2, width = 0.2)
Related posts
- Spanish version: Cómo controlar la dispersión de puntos dentro de un diagrama de violín con ggplot2
- English posts
References
No hay comentarios:
Publicar un comentario