Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created March 5, 2017 03:57
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 jrnold/0632722477f3b61fc48a7903fb613a05 to your computer and use it in GitHub Desktop.
Save jrnold/0632722477f3b61fc48a7903fb613a05 to your computer and use it in GitHub Desktop.
misc
```{r}
#' A line with shaded area around it
#'
#' Plot a line with a shaded area around it. This is often used
#' to plot fitted regression function with confidence intervals.
#' This is a helper function for \code{\link[ggplot2]{geom_smooth}}
#' but defaulting to `stat = "identity"`, so it expects, `y`, `ymin` and `ymax` from the data instead of generating them with a model.
#'
#' @inheritParams geom_pointrange
#' @seeAlso
#' @export
#' @return A ggplot object
geom_linearea <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...) {
geom_smooth(mapping = mapping, data = data, stat = stat,
position = position, ...)
}
nbin_sqrt <- function(x) {
sqrt(length(x))
}
nbin_sturges <- function(x) {
ceiling(log2(length(x))) + 1
}
nbin_rice <- function(x) {
ceiling(2 * length(x) ^ (1 / 3))
}
nbin_binwidth <- function(x, h) {
ceiling(range(x) / h)
}
breaks_pretty <- function(x, n) {
pretty(range(x), n, min.n = n)
}
bindwith_scott <- function(x) {
3.5 * sd(x) / (length(x) ^ (1 / 3))
}
binwidth_fd <- function(x) {
2 * IQR(x) / (length(x) ^ (1 / 3))
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment