Skip to content

Instantly share code, notes, and snippets.

@derekpowell
Created June 30, 2017 01:04
Show Gist options
  • Save derekpowell/4a8dea27d27990973898f05972ebd7a1 to your computer and use it in GitHub Desktop.
Save derekpowell/4a8dea27d27990973898f05972ebd7a1 to your computer and use it in GitHub Desktop.
R: standard error functions for use in ggplot2
# standard error function
stderr <- function(x) {
sqrt(var(x[!is.na(x)]) / length(x[!is.na(x)]))
}
# Then create a wrapper to *stderr* to make it compatible with *stat_summary*
my.stderr <- function(x) {
meany <- mean(x)
ymin <- mean(x) - stderr(x)
ymax <- mean(x) + stderr(x)
# assemble the named output
out <- c(y = meany, ymin = ymin, ymax = ymax)
return(out)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment