Skip to content

Instantly share code, notes, and snippets.

@BrunoGrandePhD
Last active October 17, 2018 22:17
Show Gist options
  • Save BrunoGrandePhD/3f1a3ddba8aefcace87f116e4474f5bc to your computer and use it in GitHub Desktop.
Save BrunoGrandePhD/3f1a3ddba8aefcace87f116e4474f5bc to your computer and use it in GitHub Desktop.
Comparison of various ggplot2 geoms for visualizing distributions
library(ggplot2)
library(cowplot) # For better ggplot2 theme
library(ggbeeswarm) # For geom_quasirandom()
set.seed(1)
data <- data.frame(value = c(rnorm(100, -1.8), rnorm(100, 1.8)))
extra <- list(scale_x_discrete(labels = NULL, name = NULL),
scale_y_continuous(name = NULL))
base <- ggplot(data, aes("example", value))
p1 <- base + geom_boxplot(fill = "grey90") + extra + ggtitle("geom_boxplot")
p2 <- base + geom_jitter(size = 1) + extra + ggtitle("geom_jitter")
p3 <- base + geom_quasirandom(size = 1) + extra + ggtitle("geom_quasirandom")
p4 <- base + geom_violin(fill = "grey90") + extra + ggtitle("geom_violin")
p5 <- p4 + geom_boxplot(width = 0.1) + ggtitle("geom_violin +\ngeom_boxplot")
p6 <- p4 + geom_quasirandom(size = 1) + ggtitle("geom_violin +\ngeom_quasirandom")
plot_grid(p1, p2, p3, p4, p5, p6, nrow = 1, align = "h")
ggsave("~/Desktop/plot_dist.png", width = 13, height = 7, dpi = 150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment