Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Created February 27, 2017 14:42
Show Gist options
  • Save timcdlucas/4e371eda4269910f2542d9e93b778f97 to your computer and use it in GitHub Desktop.
Save timcdlucas/4e371eda4269910f2542d9e93b778f97 to your computer and use it in GitHub Desktop.
Violins vs bars with proportion data.
library(dplyr)
library(ggplot2)
d <- data.frame(x = rep(c('a', 'b'), 50), y = rbinom(100, 1, 0.4))
summary <- d %>%
group_by(x) %>%
summarise(prop = mean(y), CI = 0.95 * sqrt((sum(y)/50 * (50 - sum(y))/50) / 50))
ggplot(summary, aes(x, prop)) +
geom_bar(stat = 'identity') +
geom_errorbar(aes(ymax = prop + CI, ymin = prop - CI), width = 0.5)
ggsave('E://tim/stuff/bars.png')
ggplot(d, aes(x, y)) +
geom_violin() +
geom_jitter(width = 0.2, height = 0.1)
ggsave('E://tim/stuff/violins.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment