diff --git a/DESCRIPTION b/DESCRIPTION index 0305f5d..2ef5ebe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggalt Title: Extra Geoms, Stats and Coords for 'ggplot2' -Version: 0.0.2.9002 +Version: 0.0.2.9003 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_bkde2d.r b/R/geom_bkde2d.r index e745d8c..6ffad0f 100644 --- a/R/geom_bkde2d.r +++ b/R/geom_bkde2d.r @@ -3,6 +3,31 @@ #' @inheritParams ggplot2::geom_point #' @inheritParams ggplot2::geom_path #' @export +#' @examples +#' m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + +#' geom_point() + +#' xlim(0.5, 6) + +#' ylim(40, 110) +#' +#' m + geom_bkde2d(bandwidth=c(0.5, 5)) +#' +#' m + stat_bkde2d(bandwidth=c(0.5, 5), aes(fill = ..level..), geom = "polygon") +#' +#' # If you map an aesthetic to a categorical variable, you will get a +#' # set of contours for each value of that variable +#' set.seed(4393) +#' dsmall <- diamonds[sample(nrow(diamonds), 1000), ] +#' d <- ggplot(dsmall, aes(x, y)) + +#' geom_bkde2d(bandwidth=c(0.5, 0.5), aes(colour = cut)) +#' d +#' +#' # If we turn contouring off, we can use use geoms like tiles: +#' d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "raster", +#' aes(fill = ..density..), contour = FALSE) +#' +#' # Or points: +#' 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, lineend = "butt", contour=TRUE, @@ -74,11 +99,13 @@ stat_bkde2d <- function(mapping = NULL, data = NULL, geom = "density2d", #' @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, + compute_group = function(data, scales, contour=TRUE, bandwidth, + grid_size=c(51, 51), range.x=NULL, truncate=TRUE) { if (is.null(range.x)) { diff --git a/README.md b/README.md index 2905942..64db304 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ library(ggalt) # current verison packageVersion("ggalt") -#> [1] '0.0.2.9002' +#> [1] '0.0.2.9003' 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:13:04 2015" +#> [1] "Tue Sep 8 21:26:18 2015" test_dir("tests/") #> testthat results ======================================================================================================== diff --git a/man/geom_bkde2d.Rd b/man/geom_bkde2d.Rd index 6fe7626..941748d 100644 --- a/man/geom_bkde2d.Rd +++ b/man/geom_bkde2d.Rd @@ -54,4 +54,30 @@ the default plot specification, e.g. \code{\link{borders}}.} \description{ Contours from a 2d density estimate. } +\examples{ +m <- ggplot(faithful, aes(x = eruptions, y = waiting)) + + geom_point() + + xlim(0.5, 6) + + ylim(40, 110) + +m + geom_bkde2d(bandwidth=c(0.5, 5)) + +m + stat_bkde2d(bandwidth=c(0.5, 5), aes(fill = ..level..), geom = "polygon") + +# If you map an aesthetic to a categorical variable, you will get a +# set of contours for each value of that variable +set.seed(4393) +dsmall <- diamonds[sample(nrow(diamonds), 1000), ] +d <- ggplot(dsmall, aes(x, y)) + + geom_bkde2d(bandwidth=c(0.5, 0.5), aes(colour = cut)) +d + +# If we turn contouring off, we can use use geoms like tiles: +d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "raster", + aes(fill = ..density..), contour = FALSE) + +# Or points: +d + stat_bkde2d(bandwidth=c(0.5, 0.5), geom = "point", + aes(size = ..density..), contour = FALSE) +}