Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created June 1, 2017 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/e5ab58b29b2d5166b8df716a2608b8e6 to your computer and use it in GitHub Desktop.
Save abikoushi/e5ab58b29b2d5166b8df716a2608b8e6 to your computer and use it in GitHub Desktop.
StatBinomCI <- ggproto("StatBinomCi", Stat,
required_aes = c("x", "numerator", "denominator"),
compute_group = function(data, scales, conf.level) {
CI <-t(mapply(function(x,n)binom.test(x=x, n=n, conf.level = conf.level)$conf.int,
data$numerator,
data$denominator))
grid <-as.data.frame(CI)
colnames(grid) <- c("ymin","ymax")
grid$x <- data$x
grid
}
)
stat_binomCI <- function(mapping = NULL, data = NULL, geom = "linerange",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, conf.level=0.95,...) {
layer(
stat = StatBinomCI, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(conf.level = conf.level, na.rm = na.rm, ...)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment