-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Added new function geom_curve #1088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooooh, I see that you're passing them in an aesthetics. I think parameters would make more sense to me - I don't think mapping (e.g.) curvature to a variable is likely to yield a useful plot. (Plus if they're aesthetics you really need to think about default scales and legends etc) |
||
|
||
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" | ||
|
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to keep the previous
@inheritParams
and this@inheritParams grid::curveGrob
. Then you don't need to document the individual arguments below because they'll be automatically pulled from curveGrob's documentation