10 changed files with 242 additions and 8 deletions
@ -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 |
|||
} |
|||
} |
|||
) |
After Width: | Height: | Size: 221 KiB |
After Width: | Height: | Size: 221 KiB |
@ -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. |
|||
} |
|||
|
@ -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}} |
|||
} |
|||
|
Loading…
Reference in new issue