diff --git a/DESCRIPTION b/DESCRIPTION index 2ef5ebe..c8e5687 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggalt Title: Extra Geoms, Stats and Coords for 'ggplot2' -Version: 0.0.2.9003 +Version: 0.0.2.9004 Authors@R: c(person("Bob", "Rudis", email = "bob@rudis.net", role = c("aut", "cre"))) Description: A package contains additional geoms, coords and stats for the revamped (late 2015) version of ggplot2. Depends: R (>= 3.0.0), ggplot2 (>= 1.0.1.9003) diff --git a/R/geom_bkde.r b/R/geom_bkde.r index 5e73ad4..2915ed3 100644 --- a/R/geom_bkde.r +++ b/R/geom_bkde.r @@ -24,8 +24,9 @@ #' @inheritParams ggplot2::geom_point #' @export geom_bkde <- function(mapping = NULL, data = NULL, stat = "bkde", - position = "identity", bandwidth, range.x=NULL, show.legend = NA, inherit.aes = TRUE, - ...) { + position = "identity", bandwidth=NULL, range.x=NULL, + show.legend = NA, inherit.aes = TRUE, + ...) { layer( data = data, @@ -45,18 +46,21 @@ geom_bkde <- function(mapping = NULL, data = NULL, stat = "bkde", #' @usage NULL #' @export GeomBkde <- ggproto("GeomBkde", GeomArea, - default_aes = aes(colour = NA, fill = "grey20", size = 0.5, linetype = 1, alpha = NA) + default_aes = aes(colour = NA, fill = "grey20", size = 0.5, + linetype = 1, alpha = NA) ) #' @param bandwidth the kernel bandwidth smoothing parameter. see -#' \code{\link[KernSmooth]{bkde}} for details +#' \code{\link[KernSmooth]{bkde}} for details. If \code{NULL}, +#' it will be computed for you but will most likely not yield optimal +#' results. #' @param kernel character string which determines the smoothing kernel. see #' \code{\link[KernSmooth]{bkde}} for details #' @param canonical logical flag: if TRUE, canonically scaled kernels are used. #' see \code{\link[KernSmooth]{bkde}} for details #' @param gridsize the number of equally spaced points at which to estimate the -#' density. see \code{\link[KernSmooth]{bkde}} for details +#' density. see \code{\link[KernSmooth]{bkde}} for details. #' @param range.x vector containing the minimum and maximum values of x at which #' to compute the estimate. see \code{\link[KernSmooth]{bkde}} for details #' @param truncate logical flag: if TRUE, data with x values outside the range @@ -80,8 +84,10 @@ GeomBkde <- ggproto("GeomBkde", GeomArea, #' ggplot(geyser, aes(x=duration)) + #' geom_bkde(bandwidth=0.25) stat_bkde <- function(mapping = NULL, data = NULL, geom = "area", - position = "stack", kernel="normal", canonical=FALSE, bandwidth, gridsize=410, - range.x=NULL, truncate=TRUE, show.legend = NA, inherit.aes = TRUE, ...) { + position = "stack", kernel="normal", canonical=FALSE, + bandwidth = NULL, gridsize=410, range.x=NULL, + truncate=TRUE, show.legend = NA, inherit.aes = TRUE, + ...) { layer( data = data, @@ -114,9 +120,12 @@ StatBkde <- ggproto("StatBkde", Stat, default_aes = aes(y = ..density.., fill = NA), compute_group = function(data, scales, kernel="normal", canonical=FALSE, - bandwidth, gridsize=410, range.x, truncate=TRUE) { + bandwidth=NULL, gridsize=410, range.x=NULL, + truncate=TRUE) { + + if (is.null(bandwidth)) bandwidth <- KernSmooth::dpik(data$x) - if (missing(range.x) | is.null(range.x)) range.x <- range(data$x) + if (is.null(range.x)) range.x <- range(data$x) compute_bkde(data$x, kernel=kernel, canonical=canonical, bandwidth=bandwidth, gridsize=gridsize, range.x=range.x, diff --git a/R/geom_bkde2d.r b/R/geom_bkde2d.r index 6ffad0f..d73b61a 100644 --- a/R/geom_bkde2d.r +++ b/R/geom_bkde2d.r @@ -29,7 +29,7 @@ #' d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "point", #' aes(size = ..density..), contour = FALSE) geom_bkde2d <- function(mapping = NULL, data = NULL, stat = "bkde2d", - position = "identity", bandwidth, range.x=NULL, + position = "identity", bandwidth=NULL, range.x=NULL, lineend = "butt", contour=TRUE, linejoin = "round", linemitre = 1, show.legend = NA, @@ -63,16 +63,35 @@ GeomBkde2d <- ggproto("GeomBkde2d", GeomPath, ) -#' 2D density +#' Contours from a 2d density estimate. #' -#' @export +#' Perform a 2D kernel density estimation using \code{bkde2D} and display the +#' results with contours. This can be useful for dealing with overplotting +#' +#' @param bandwidth the kernel bandwidth smoothing parameter. see +#' \code{\link[KernSmooth]{bkde2D}} for details. If \code{NULL}, +#' it will be computed for you but will most likely not yield optimal +#' results. see \code{\link[KernSmooth]{bkde2D}} for details +#' @param gridsize vector containing the number of equally spaced points in each +#' direction over which the density is to be estimated. see +#' \code{\link[KernSmooth]{bkde2D}} for details +#' @param range.x a list containing two vectors, where each vector contains the +#' minimum and maximum values of x at which to compute the estimate for +#' each direction. see \code{\link[KernSmooth]{bkde2D}} for details +#' @param truncate logical flag: if TRUE, data with x values outside the range +#' specified by range.x are ignored. see \code{\link[KernSmooth]{bkde2D}} +#' for details #' @param contour If \code{TRUE}, contour the results of the 2d density #' estimation #' @section Computed variables: #' Same as \code{\link{stat_contour}} +#' @seealso \code{\link{geom_contour}} for contour drawing geom, +#' \code{\link{stat_sum}} for another way of dealing with overplotting +#' @rdname geom_bkde2d +#' @export stat_bkde2d <- function(mapping = NULL, data = NULL, geom = "density2d", position = "identity", contour = TRUE, - bandwidth, grid_size=c(51, 51), range.x=NULL, + bandwidth=NULL, grid_size=c(51, 51), range.x=NULL, truncate=TRUE, show.legend = NA, inherit.aes = TRUE, ...) { layer( @@ -104,10 +123,15 @@ StatBkde2d <- ggproto("StatBkde2d", Stat, required_aes = c("x", "y"), - compute_group = function(data, scales, contour=TRUE, bandwidth, + compute_group = function(data, scales, contour=TRUE, bandwidth=NULL, grid_size=c(51, 51), range.x=NULL, truncate=TRUE) { + if (is.null(bandwidth)) { + bandwidth <- c(KernSmooth::dpik(data$x), + KernSmooth::dpik(data$y)) + } + if (is.null(range.x)) { x_range <- range(data$x) y_range <- range(data$y) diff --git a/README.md b/README.md index 64db304..2f04a89 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ library(ggalt) # current verison packageVersion("ggalt") -#> [1] '0.0.2.9003' +#> [1] '0.0.2.9004' set.seed(1492) dat <- data.frame(x=c(1:10, 1:10, 1:10), @@ -173,7 +173,7 @@ library(ggalt) library(testthat) date() -#> [1] "Tue Sep 8 21:26:18 2015" +#> [1] "Tue Sep 8 21:44:34 2015" test_dir("tests/") #> testthat results ======================================================================================================== diff --git a/README_figs/README-unnamed-chunk-4-10.png b/README_figs/README-unnamed-chunk-4-10.png index c852f72..4ec9f2d 100644 Binary files a/README_figs/README-unnamed-chunk-4-10.png and b/README_figs/README-unnamed-chunk-4-10.png differ diff --git a/man/geom_bkde.Rd b/man/geom_bkde.Rd index b421c52..945a39f 100644 --- a/man/geom_bkde.Rd +++ b/man/geom_bkde.Rd @@ -6,13 +6,13 @@ \title{Display a smooth density estimate.} \usage{ geom_bkde(mapping = NULL, data = NULL, stat = "bkde", - position = "identity", bandwidth, range.x = NULL, show.legend = NA, - inherit.aes = TRUE, ...) + position = "identity", bandwidth = NULL, range.x = NULL, + show.legend = NA, inherit.aes = TRUE, ...) stat_bkde(mapping = NULL, data = NULL, geom = "area", - position = "stack", kernel = "normal", canonical = FALSE, bandwidth, - gridsize = 410, range.x = NULL, truncate = TRUE, show.legend = NA, - inherit.aes = TRUE, ...) + position = "stack", kernel = "normal", canonical = FALSE, + bandwidth = NULL, gridsize = 410, range.x = NULL, truncate = TRUE, + show.legend = NA, inherit.aes = TRUE, ...) } \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or @@ -28,7 +28,9 @@ defined at the top level of the plot.} a call to a position adjustment function.} \item{bandwidth}{the kernel bandwidth smoothing parameter. see -\code{\link[KernSmooth]{bkde}} for details} +\code{\link[KernSmooth]{bkde}} for details. If \code{NULL}, +it will be computed for you but will most likely not yield optimal +results.} \item{range.x}{vector containing the minimum and maximum values of x at which to compute the estimate. see \code{\link[KernSmooth]{bkde}} for details} @@ -63,7 +65,7 @@ the default plot specification, e.g. \code{\link{borders}}.} see \code{\link[KernSmooth]{bkde}} for details} \item{gridsize}{the number of equally spaced points at which to estimate the -density. see \code{\link[KernSmooth]{bkde}} for details} +density. see \code{\link[KernSmooth]{bkde}} for details.} \item{truncate}{logical flag: if TRUE, data with x values outside the range specified by range.x are ignored. see \code{\link[KernSmooth]{bkde}} diff --git a/man/geom_bkde2d.Rd b/man/geom_bkde2d.Rd index 941748d..73865f8 100644 --- a/man/geom_bkde2d.Rd +++ b/man/geom_bkde2d.Rd @@ -2,12 +2,18 @@ % Please edit documentation in R/geom_bkde2d.r \name{geom_bkde2d} \alias{geom_bkde2d} +\alias{stat_bkde2d} \title{Contours from a 2d density estimate.} \usage{ geom_bkde2d(mapping = NULL, data = NULL, stat = "bkde2d", - position = "identity", bandwidth, range.x = NULL, lineend = "butt", - contour = TRUE, linejoin = "round", linemitre = 1, show.legend = NA, - inherit.aes = TRUE, ...) + position = "identity", bandwidth = NULL, range.x = NULL, + lineend = "butt", contour = TRUE, linejoin = "round", linemitre = 1, + show.legend = NA, inherit.aes = TRUE, ...) + +stat_bkde2d(mapping = NULL, data = NULL, geom = "density2d", + position = "identity", contour = TRUE, bandwidth = NULL, + grid_size = c(51, 51), range.x = NULL, truncate = TRUE, + show.legend = NA, inherit.aes = TRUE, ...) } \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or @@ -25,8 +31,20 @@ layer, as a string.} \item{position}{Position adjustment, either as a string, or the result of a call to a position adjustment function.} +\item{bandwidth}{the kernel bandwidth smoothing parameter. see +\code{\link[KernSmooth]{bkde2D}} for details. If \code{NULL}, +it will be computed for you but will most likely not yield optimal +results. see \code{\link[KernSmooth]{bkde2D}} for details} + +\item{range.x}{a list containing two vectors, where each vector contains the +minimum and maximum values of x at which to compute the estimate for +each direction. see \code{\link[KernSmooth]{bkde2D}} for details} + \item{lineend}{Line end style (round, butt, square)} +\item{contour}{If \code{TRUE}, contour the results of the 2d density +estimation} + \item{linejoin}{Line join style (round, mitre, bevel)} \item{linemitre}{Line mitre limit (number greater than 1)} @@ -50,9 +68,24 @@ the default plot specification, e.g. \code{\link{borders}}.} default \code{stat} associated with the layer. \item Other arguments passed on to the stat. }} + +\item{truncate}{logical flag: if TRUE, data with x values outside the range +specified by range.x are ignored. see \code{\link[KernSmooth]{bkde2D}} +for details} + +\item{gridsize}{vector containing the number of equally spaced points in each +direction over which the density is to be estimated. see +\code{\link[KernSmooth]{bkde2D}} for details} } \description{ Contours from a 2d density estimate. + +Perform a 2D kernel density estimation using \code{bkde2D} and display the +results with contours. This can be useful for dealing with overplotting +} +\section{Computed variables}{ + +Same as \code{\link{stat_contour}} } \examples{ m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + @@ -80,4 +113,8 @@ d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "raster", d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "point", aes(size = ..density..), contour = FALSE) } +\seealso{ +\code{\link{geom_contour}} for contour drawing geom, + \code{\link{stat_sum}} for another way of dealing with overplotting +} diff --git a/man/stat_bkde2d.Rd b/man/stat_bkde2d.Rd deleted file mode 100644 index cda1159..0000000 --- a/man/stat_bkde2d.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand -% Please edit documentation in R/geom_bkde2d.r -\name{stat_bkde2d} -\alias{stat_bkde2d} -\title{2D density} -\usage{ -stat_bkde2d(mapping = NULL, data = NULL, geom = "density2d", - position = "identity", contour = TRUE, bandwidth, grid_size = c(51, 51), - range.x = NULL, truncate = TRUE, show.legend = NA, inherit.aes = TRUE, - ...) -} -\arguments{ -\item{contour}{If \code{TRUE}, contour the results of the 2d density -estimation} -} -\description{ -2D density -} -\section{Computed variables}{ - -Same as \code{\link{stat_contour}} -} -