Browse Source

better bkd2D @examples

tags/v0.1.1
hrbrmstr 9 years ago
parent
commit
212f86cb68
  1. 2
      DESCRIPTION
  2. 29
      R/geom_bkde2d.r
  3. 4
      README.md
  4. 26
      man/geom_bkde2d.Rd

2
DESCRIPTION

@ -1,6 +1,6 @@
Package: ggalt Package: ggalt
Title: Extra Geoms, Stats and Coords for 'ggplot2' 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"))) 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. 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) Depends: R (>= 3.0.0), ggplot2 (>= 1.0.1.9003)

29
R/geom_bkde2d.r

@ -3,6 +3,31 @@
#' @inheritParams ggplot2::geom_point #' @inheritParams ggplot2::geom_point
#' @inheritParams ggplot2::geom_path #' @inheritParams ggplot2::geom_path
#' @export #' @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", geom_bkde2d <- function(mapping = NULL, data = NULL, stat = "bkde2d",
position = "identity", bandwidth, range.x=NULL, position = "identity", bandwidth, range.x=NULL,
lineend = "butt", contour=TRUE, lineend = "butt", contour=TRUE,
@ -74,11 +99,13 @@ stat_bkde2d <- function(mapping = NULL, data = NULL, geom = "density2d",
#' @usage NULL #' @usage NULL
#' @export #' @export
StatBkde2d <- ggproto("StatBkde2d", Stat, StatBkde2d <- ggproto("StatBkde2d", Stat,
default_aes = aes(colour = "#3366FF", size = 0.5), default_aes = aes(colour = "#3366FF", size = 0.5),
required_aes = c("x", "y"), 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) { truncate=TRUE) {
if (is.null(range.x)) { if (is.null(range.x)) {

4
README.md

@ -39,7 +39,7 @@ library(ggalt)
# current verison # current verison
packageVersion("ggalt") packageVersion("ggalt")
#> [1] '0.0.2.9002' #> [1] '0.0.2.9003'
set.seed(1492) set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10), dat <- data.frame(x=c(1:10, 1:10, 1:10),
@ -173,7 +173,7 @@ library(ggalt)
library(testthat) library(testthat)
date() date()
#> [1] "Tue Sep 8 21:13:04 2015" #> [1] "Tue Sep 8 21:26:18 2015"
test_dir("tests/") test_dir("tests/")
#> testthat results ======================================================================================================== #> testthat results ========================================================================================================

26
man/geom_bkde2d.Rd

@ -54,4 +54,30 @@ the default plot specification, e.g. \code{\link{borders}}.}
\description{ \description{
Contours from a 2d density estimate. 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)
}

Loading…
Cancel
Save