Skip to content

Commit b649078

Browse files
committed
Added new function geom_curve
1 parent 2b6b986 commit b649078

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Collate:
9696
'geom-blank.r'
9797
'geom-boxplot.r'
9898
'geom-crossbar.r'
99+
'geom-curve.r'
99100
'geom-defaults.r'
100101
'geom-dotplot.r'
101102
'geom-error.r'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export(geom_boxplot)
224224
export(geom_contour)
225225
export(geom_count)
226226
export(geom_crossbar)
227+
export(geom_curve)
227228
export(geom_density)
228229
export(geom_density2d)
229230
export(geom_dotplot)

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ ggplot2 1.0.1.9000
5050
* Improved documentation for `aes()` and many geoms and scales. I've tried
5151
to reduce the use of `...` so that you can see all the documentation in one
5252
place rather than having to navigate through multiple pages.
53+
* Added new function geom_curve to add curved lines to plot (similar to
54+
geom_segment to add straight lines). (@veraanadi)
5355

5456
ggplot2 1.0.1
5557
----------------------------------------------------------------

R/geom-curve.r

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#' Single curved line segments.
2+
#'
3+
#' @section Aesthetics:
4+
#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")}
5+
#'
6+
#' @inheritParams geom_point
7+
#' @param curvature see curveGrob
8+
#' @param angle see curveGrob
9+
#' @param ncp see curveGrob
10+
#' @param arrow specification for arrow heads, as created by arrow()
11+
#' @param lineend Line end style (round, butt, square)
12+
#' @seealso \code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
13+
#' segment lines and paths.
14+
#' @export
15+
#' @examples
16+
#' # Adding curve segments
17+
#' library(grid) # needed for arrow function
18+
#' b <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
19+
#' b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25, curvature = 0.2))
20+
#' b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15, ncp = 2))
21+
#' b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
22+
23+
24+
geom_curve <- function (mapping = NULL, data = NULL, stat = "identity",
25+
position = "identity", arrow = NULL, lineend = "butt", na.rm = FALSE, ...) {
26+
27+
GeomCurve$new(mapping = mapping, data = data, stat = stat,
28+
position = position, arrow = arrow, lineend = lineend, na.rm = na.rm, ...)
29+
}
30+
31+
GeomCurve <- proto(Geom, {
32+
objname <- "curve"
33+
34+
draw <- function(., data, scales, coordinates, arrow = NULL,
35+
lineend = "butt", na.rm = FALSE, ...) {
36+
37+
data <- remove_missing(data, na.rm = na.rm,
38+
c("x", "y", "xend", "yend", "linetype", "size", "shape", "curvature", "angle", "ncp"),
39+
name = "geom_curve")
40+
41+
if (empty(data)) return(zeroGrob())
42+
43+
if (is.linear(coordinates)) {
44+
return(with(coord_transform(coordinates, data, scales),
45+
curveGrob(x, y, xend, yend, default.units="native",
46+
curvature=curvature[1], angle=angle[1], ncp=ncp[1],
47+
square = FALSE, squareShape = 1,
48+
inflect = FALSE, open = TRUE,
49+
gp = gpar(col=alpha(colour, alpha), fill = alpha(colour, alpha),
50+
lwd=size * .pt, lty=linetype, lineend = lineend),
51+
arrow = arrow)
52+
))
53+
}
54+
print("geom_curve is not implemented for non-linear coordinates")
55+
return(zeroGrob())
56+
}
57+
58+
59+
default_stat <- function(.) StatIdentity
60+
required_aes <- c("x", "y", "xend", "yend")
61+
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = NA, curvature = 1, angle = 90, ncp = 1)
62+
guide_geom <- function(.) "path"
63+
64+
})

man/geom_curve.Rd

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
% Generated by roxygen2 (4.1.1): do not edit by hand
2+
% Please edit documentation in R/geom-curve.r
3+
\name{geom_curve}
4+
\alias{geom_curve}
5+
\title{Single curved line segments.}
6+
\usage{
7+
geom_curve(mapping = NULL, data = NULL, stat = "identity",
8+
position = "identity", arrow = NULL, lineend = "butt", na.rm = FALSE,
9+
...)
10+
}
11+
\arguments{
12+
\item{mapping}{The aesthetic mapping, usually constructed with
13+
\code{\link{aes}} or \code{\link{aes_string}}. Only needs to be set
14+
at the layer level if you are overriding the plot defaults.}
15+
16+
\item{data}{A layer specific dataset - only needed if you want to override
17+
the plot defaults.}
18+
19+
\item{stat}{The statistical transformation to use on the data for this
20+
layer.}
21+
22+
\item{position}{The position adjustment to use for overlapping points
23+
on this layer}
24+
25+
\item{arrow}{specification for arrow heads, as created by arrow()}
26+
27+
\item{lineend}{Line end style (round, butt, square)}
28+
29+
\item{na.rm}{If \code{FALSE} (the default), removes missing values with
30+
a warning. If \code{TRUE} silently removes missing values.}
31+
32+
\item{...}{other arguments passed on to \code{\link{layer}}. This can
33+
include aesthetics whose values you want to set, not map. See
34+
\code{\link{layer}} for more details.}
35+
36+
\item{curvature}{see curveGrob}
37+
38+
\item{angle}{see curveGrob}
39+
40+
\item{ncp}{see curveGrob}
41+
}
42+
\description{
43+
Single curved line segments.
44+
}
45+
\section{Aesthetics}{
46+
47+
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")}
48+
}
49+
\examples{
50+
# Adding curve segments
51+
library(grid) # needed for arrow function
52+
b <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
53+
b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25, curvature = 0.2))
54+
b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15, ncp = 2))
55+
b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
56+
}
57+
\seealso{
58+
\code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
59+
segment lines and paths.
60+
}
61+

0 commit comments

Comments
 (0)