diff --git a/DESCRIPTION b/DESCRIPTION index 07bac17630..6dc69077c9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -96,6 +96,7 @@ Collate: 'geom-blank.r' 'geom-boxplot.r' 'geom-crossbar.r' + 'geom-curve.r' 'geom-defaults.r' 'geom-dotplot.r' 'geom-error.r' diff --git a/NAMESPACE b/NAMESPACE index 488256708d..869d3ba1df 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -224,6 +224,7 @@ export(geom_boxplot) export(geom_contour) export(geom_count) export(geom_crossbar) +export(geom_curve) export(geom_density) export(geom_density2d) export(geom_dotplot) diff --git a/NEWS b/NEWS index 3aeea10213..3f52bdc9a5 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ * New `theme_void()`, which is completely empty. Useful for plots with non standard coordinates or for producing numerical drawings with R. (@jiho, #976) +* Added new function geom_curve to add curved lines to plot (similar to + geom_segment to add straight lines). (@veraanadi, #1088) * Create correct legend for continuous color even if there is only one color (@krlmlr, #943) diff --git a/R/geom-curve.r b/R/geom-curve.r new file mode 100644 index 0000000000..55aed30daf --- /dev/null +++ b/R/geom-curve.r @@ -0,0 +1,64 @@ +#' Single curved line segments. +#' +#' @section Aesthetics: +#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")} +#' +#' @inheritParams grid::curveGrob +#' @inheritParams geom_point +#' @inheritParams geom_segment +#' +#' @seealso \code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi- +#' segment lines and paths. +#' @export +#' @examples +#' # Adding curve segments +#' library(grid) # needed for arrow function +#' b <- ggplot(mtcars, aes(wt, mpg)) + geom_point() +#' b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25), curvature = 0.2) +#' b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15), ncp = 2) +#' b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm"))) + + +geom_curve <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", + curvature = 1, angle = 90, ncp = 1, arrow = NULL, lineend = "butt", + na.rm = FALSE, ...) { + + GeomCurve$new(mapping = mapping, data = data, stat = stat, + position = position, arrow = arrow, curvature = curvature, angle = angle, + ncp = ncp, lineend = lineend, na.rm = na.rm, ...) +} + +GeomCurve <- proto(Geom, { + objname <- "curve" + + draw <- function(., data, scales, coordinates, curvature, angle, ncp, + arrow, lineend, na.rm, ...) { + + data <- remove_missing(data, na.rm = na.rm, + c("x", "y", "xend", "yend", "linetype", "size", "shape"), + name = "geom_curve") + + if (empty(data)) return(zeroGrob()) + + if (is.linear(coordinates)) { + return(with(coord_transform(coordinates, data, scales), + curveGrob(x, y, xend, yend, default.units="native", + curvature=curvature, angle=angle, ncp=ncp, + square = FALSE, squareShape = 1, + inflect = FALSE, open = TRUE, + gp = gpar(col=alpha(colour, alpha), lwd=size * .pt, + lty=linetype, lineend = lineend), + arrow = arrow) + )) + } + warning("geom_curve is not implemented for non-linear coordinates") + return(zeroGrob()) + } + + + default_stat <- function(.) StatIdentity + required_aes <- c("x", "y", "xend", "yend") + default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = NA) + guide_geom <- function(.) "path" + +}) diff --git a/man/geom_bar.Rd b/man/geom_bar.Rd index 9b3894cfd5..d2f2cfbad8 100644 --- a/man/geom_bar.Rd +++ b/man/geom_bar.Rd @@ -145,7 +145,7 @@ m + geom_bar() } } \seealso{ -\code{\link{stat_bin}} for more details of the binning alogirithm, +\code{\link{stat_bin}} for more details of the binning algorithm, \code{\link{position_dodge}} for creating side-by-side barcharts, \code{\link{position_stack}} for more info on stacking, } diff --git a/man/geom_curve.Rd b/man/geom_curve.Rd new file mode 100644 index 0000000000..ca38c94ce7 --- /dev/null +++ b/man/geom_curve.Rd @@ -0,0 +1,67 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/geom-curve.r +\name{geom_curve} +\alias{geom_curve} +\title{Single curved line segments.} +\usage{ +geom_curve(mapping = NULL, data = NULL, stat = "identity", + position = "identity", curvature = 1, angle = 90, ncp = 1, + arrow = NULL, lineend = "butt", na.rm = FALSE, ...) +} +\arguments{ +\item{mapping}{The aesthetic mapping, usually constructed with +\code{\link{aes}} or \code{\link{aes_string}}. Only needs to be set +at the layer level if you are overriding the plot defaults.} + +\item{data}{A layer specific dataset - only needed if you want to override +the plot defaults.} + +\item{stat}{The statistical transformation to use on the data for this +layer.} + +\item{position}{The position adjustment to use for overlapping points +on this layer} + +\item{curvature}{A numeric value giving the amount of curvature. + Negative values produce left-hand curves, positive values + produce right-hand curves, and zero produces a straight line.} + +\item{angle}{A numeric value between 0 and 180, + giving an amount to skew the control + points of the curve. Values less than 90 skew the curve towards + the start point and values greater than 90 skew the curve + towards the end point.} + +\item{ncp}{The number of control points used to draw the curve. + More control points creates a smoother curve.} + +\item{arrow}{A list describing arrow heads to place at either end + of the curve, as produced by the \code{arrow} function.} + +\item{lineend}{Line end style (round, butt, square)} + +\item{na.rm}{If \code{FALSE} (the default), removes missing values with +a warning. If \code{TRUE} silently removes missing values.} + +\item{...}{Arguments to be passed to \code{curveGrob}.} +} +\description{ +Single curved line segments. +} +\section{Aesthetics}{ + +\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")} +} +\examples{ +# Adding curve segments +library(grid) # needed for arrow function +b <- ggplot(mtcars, aes(wt, mpg)) + geom_point() +b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25), curvature = 0.2) +b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15), ncp = 2) +b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm"))) +} +\seealso{ +\code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi- +segment lines and paths. +} + diff --git a/man/geom_density2d.Rd b/man/geom_density2d.Rd index 52c5adeeb1..7be0797c1f 100644 --- a/man/geom_density2d.Rd +++ b/man/geom_density2d.Rd @@ -36,7 +36,7 @@ include aesthetics whose values you want to set, not map. See \code{\link{layer}} for more details.} } \description{ -Perform a 2D kernel density estimatation using kde2d and display the +Perform a 2D kernel density estimation using kde2d and display the results with contours. } \details{