2019-01-31

How to restrain scattered jitter points within a violin plot using ggplot2

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)
    
  • Option 2
  • Using the function geom_quasirandom from package geom_beeswarm:

    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

References

No hay comentarios:

Publicar un comentario

Nube de datos