|
@ -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)) { |
|
|