diff --git a/DESCRIPTION b/DESCRIPTION index 5487a8e..98f6df3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggalt Title: Extra Geoms, Stats and Coords for 'ggplot2' -Version: 0.0.1.9001 +Version: 0.0.2.9000 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/NAMESPACE b/NAMESPACE index 79412d9..ddc3831 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,12 +1,16 @@ # Generated by roxygen2 (4.1.1): do not edit by hand export(GeomBkde) +export(GeomBkde2d) export(GeomXspline) export(StatBkde) +export(StatBkde2d) export(StatXspline) export(geom_bkde) +export(geom_bkde2d) export(geom_xspline) export(stat_bkde) +export(stat_bkde2d) export(stat_xspline) import(KernSmooth) import(dplyr) diff --git a/R/geom_bkde2d.r b/R/geom_bkde2d.r new file mode 100644 index 0000000..082c53e --- /dev/null +++ b/R/geom_bkde2d.r @@ -0,0 +1,105 @@ +#' Contours from a 2d density estimate. +#' +#' @inheritParams ggplot2::geom_point +#' @inheritParams ggplot2::geom_path +#' @export +geom_bkde2d <- function(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, ...) { + layer( + data = data, + mapping = mapping, + stat = stat, + geom = GeomBkde2d, + position = position, + show.legend = show.legend, + inherit.aes = inherit.aes, + params = list( + lineend = lineend, + linejoin = linejoin, + linemitre = linemitre, + bandwidth = bandwidth, + range.x = range.x, + ... + ) + ) +} + + +#' @rdname ggalt-ggproto +#' @format NULL +#' @usage NULL +#' @export +GeomBkde2d <- ggproto("GeomBkde2d", GeomPath, + default_aes = aes(colour = "#3366FF", size = 0.5, linetype = 1, alpha = NA) +) + + +#' 2D density +#' +#' @export +#' @param contour If \code{TRUE}, contour the results of the 2d density +#' estimation +#' @section Computed variables: +#' Same as \code{\link{stat_contour}} +stat_bkde2d <- function(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, ...) { + layer( + data = data, + mapping = mapping, + stat = StatBkde2d, + geom = geom, + position = position, + show.legend = show.legend, + inherit.aes = inherit.aes, + params = list( + bandwidth = bandwidth, + grid_size = grid_size, + range.x = range.x, + truncate = truncate, + contour = contour, + ... + ) + ) +} + +#' @rdname ggalt-ggproto +#' @format NULL +#' @usage NULL +#' @export +StatBkde2d <- ggproto("StatBkde2d", Stat, + default_aes = aes(colour = "#3366FF", size = 0.5), + + required_aes = c("x", "y"), + + compute_group = function(data, scales, contour=TRUE, bandwidth, grid_size=c(51, 51), range.x=NULL, + truncate=TRUE) { + + if (is.null(range.x)) range.x <- list(range(data$x), range(data$y)) + + dens <- KernSmooth::bkde2D( + as.matrix(data.frame(x=data$x, y=data$y)), + bandwidth, + grid_size, + range.x, + truncate + ) + df <- data.frame(expand.grid(x = dens$x1, y = dens$x2), z = as.vector(dens$fhat)) + df$group <- data$group[1] + + if (contour) { + StatContour$compute_panel(df, scales) + } else { + names(df) <- c("x", "y", "density", "group") + df$level <- 1 + df$piece <- 1 + df + } + } +) diff --git a/README.Rmd b/README.Rmd index 4b7558f..fbda585 100644 --- a/README.Rmd +++ b/README.Rmd @@ -21,7 +21,8 @@ A package containing additional geoms, coords and stats for the revamped (late 2 of ggplot2. The first two forays into this brave, new `ggplot2` world are _splines_! and being able to -use the (much better) `KernSmooth::bkde` for density plots. +use the (much better) `KernSmooth::bkde` for density plots. Support for `KernSmooth::bkde2D` is +a WIP. *NOTE* @@ -33,10 +34,12 @@ The following functions are implemented: - `stat_xspline` : Connect control points/observations with an X-spline - `geom_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`) - `stat_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`) +- `geom_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`) **WIP** +- `stat_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`) **WIP** ### News -- Version 0.0.1.9000 released +- Version 0.0.2.9000 released ### Installation @@ -113,6 +116,20 @@ ggplot(geyser, aes(x=duration)) + ggplot(geyser, aes(x=duration)) + geom_bkde(bandwidth=0.25) +# 2D KernSmooth::bkde2D plots are a WIP + +geyser_dat <- data.frame(x=geyser$duration, y=geyser$waiting) + +ggplot(geyser_dat, aes(x, y)) + + geom_point() + + geom_bkde2d(bandwidth=c(0.7, 7)) + + xlim(0, 6) + ylim(35, 120) + +ggplot(geyser_dat, aes(x, y)) + + geom_point() + + stat_bkde2d(bandwidth=c(0.7, 7)) + + xlim(0, 6) + ylim(35, 120) + ``` ### Test Results diff --git a/README.md b/README.md index 3744ba7..5079fca 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A package containing additional geoms, coords and stats for the revamped (late 2015) version of ggplot2. -The first two forays into this brave, new `ggplot2` world are *splines*! and being able to use the (much better) `KernSmooth::bkde` for density plots. +The first two forays into this brave, new `ggplot2` world are *splines*! and being able to use the (much better) `KernSmooth::bkde` for density plots. Support for `KernSmooth::bkde2D` is a WIP. *NOTE* @@ -15,10 +15,12 @@ The following functions are implemented: - `stat_xspline` : Connect control points/observations with an X-spline - `geom_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`) - `stat_bkde` : Display a smooth density estimate (uses `KernSmooth::bkde`) +- `geom_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`) **WIP** +- `stat_bkde2d` : Contours from a 2d density estimate. (uses `KernSmooth::bkde2D`) **WIP** ### News -- Version 0.0.1.9000 released +- Version 0.0.2.9000 released ### Installation @@ -36,7 +38,7 @@ library(ggalt) # current verison packageVersion("ggalt") -#> [1] '0.0.1.9001' +#> [1] '0.0.2.9000' set.seed(1492) dat <- data.frame(x=c(1:10, 1:10, 1:10), @@ -141,6 +143,30 @@ ggplot(geyser, aes(x=duration)) + +``` r + +# 2D KernSmooth::bkde2D plots are a WIP + +geyser_dat <- data.frame(x=geyser$duration, y=geyser$waiting) + +ggplot(geyser_dat, aes(x, y)) + + geom_point() + + geom_bkde2d(bandwidth=c(0.7, 7)) + + xlim(0, 6) + ylim(35, 120) +``` + + + +``` r + +ggplot(geyser_dat, aes(x, y)) + + geom_point() + + stat_bkde2d(bandwidth=c(0.7, 7)) + + xlim(0, 6) + ylim(35, 120) +``` + + + ### Test Results ``` r @@ -148,7 +174,7 @@ library(ggalt) library(testthat) date() -#> [1] "Tue Sep 8 16:16:44 2015" +#> [1] "Tue Sep 8 17:20:02 2015" test_dir("tests/") #> testthat results ======================================================================================================== diff --git a/README_figs/README-unnamed-chunk-4-11.png b/README_figs/README-unnamed-chunk-4-11.png new file mode 100644 index 0000000..3b2b4df Binary files /dev/null and b/README_figs/README-unnamed-chunk-4-11.png differ diff --git a/README_figs/README-unnamed-chunk-4-12.png b/README_figs/README-unnamed-chunk-4-12.png new file mode 100644 index 0000000..3b2b4df Binary files /dev/null and b/README_figs/README-unnamed-chunk-4-12.png differ diff --git a/man/geom_bkde2d.Rd b/man/geom_bkde2d.Rd new file mode 100644 index 0000000..6fe7626 --- /dev/null +++ b/man/geom_bkde2d.Rd @@ -0,0 +1,57 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/geom_bkde2d.r +\name{geom_bkde2d} +\alias{geom_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, ...) +} +\arguments{ +\item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or +\code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the +default), is combined with the default mapping at the top level of the +plot. You only need to supply \code{mapping} if there isn't a mapping +defined for the plot.} + +\item{data}{A data frame. If specified, overrides the default data frame +defined at the top level of the plot.} + +\item{stat}{The statistical transformation to use on the data for this +layer, as a string.} + +\item{position}{Position adjustment, either as a string, or the result of +a call to a position adjustment function.} + +\item{lineend}{Line end style (round, butt, square)} + +\item{linejoin}{Line join style (round, mitre, bevel)} + +\item{linemitre}{Line mitre limit (number greater than 1)} + +\item{show.legend}{logical. Should this layer be included in the legends? +\code{NA}, the default, includes if any aesthetics are mapped. +\code{FALSE} never includes, and \code{TRUE} always includes.} + +\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, +rather than combining with them. This is most useful for helper functions +that define both data and aesthetics and shouldn't inherit behaviour from +the default plot specification, e.g. \code{\link{borders}}.} + +\item{...}{other arguments passed on to \code{\link{layer}}. There are + three types of arguments you can use here: + + \itemize{ + \item Aesthetics: to set an aesthetic to a fixed value, like + \code{color = "red"} or \code{size = 3}. + \item Other arguments to the layer, for example you override the + default \code{stat} associated with the layer. + \item Other arguments passed on to the stat. + }} +} +\description{ +Contours from a 2d density estimate. +} + diff --git a/man/ggalt-ggproto.Rd b/man/ggalt-ggproto.Rd index 63c6424..1a58ab8 100644 --- a/man/ggalt-ggproto.Rd +++ b/man/ggalt-ggproto.Rd @@ -1,10 +1,12 @@ % Generated by roxygen2 (4.1.1): do not edit by hand -% Please edit documentation in R/geom_bkde.r, R/geom_xspline.r +% Please edit documentation in R/geom_bkde.r, R/geom_bkde2d.r, R/geom_xspline.r \docType{data} \name{GeomBkde} \alias{GeomBkde} +\alias{GeomBkde2d} \alias{GeomXspline} \alias{StatBkde} +\alias{StatBkde2d} \alias{StatXspline} \title{GeomXspline} \description{ diff --git a/man/stat_bkde2d.Rd b/man/stat_bkde2d.Rd new file mode 100644 index 0000000..cda1159 --- /dev/null +++ b/man/stat_bkde2d.Rd @@ -0,0 +1,23 @@ +% 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}} +} +