Skip to content

Commit 99bc4c5

Browse files
committed
included suggestions
1 parent b649078 commit 99bc4c5

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Added new function geom_curve to add curved lines to plot (similar to
2+
geom_segment to add straight lines). (@veraanadi, #1088)
3+
14
* Create correct legend for continuous color even if there is only one color
25
(@krlmlr, #943)
36

R/geom-curve.r

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#' @section Aesthetics:
44
#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")}
55
#'
6-
#' @inheritParams geom_point
7-
#' @param curvature see curveGrob
8-
#' @param angle see curveGrob
9-
#' @param ncp see curveGrob
6+
#' @inheritParams grid::curveGrob
7+
#' @param 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. (see curveGrob)
8+
#' @param 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. (see curveGrob)
9+
#' @param ncp The number of control points used to draw the curve. More control points creates a smoother curve. (see curveGrob)
1010
#' @param arrow specification for arrow heads, as created by arrow()
1111
#' @param lineend Line end style (round, butt, square)
1212
#' @seealso \code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
@@ -16,49 +16,51 @@
1616
#' # Adding curve segments
1717
#' library(grid) # needed for arrow function
1818
#' 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))
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)
2121
#' b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
2222

2323

24-
geom_curve <- function (mapping = NULL, data = NULL, stat = "identity",
25-
position = "identity", arrow = NULL, lineend = "butt", na.rm = FALSE, ...) {
24+
geom_curve <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
25+
curvature = 1, angle = 90, ncp = 1, arrow = NULL, lineend = "butt",
26+
na.rm = FALSE, ...) {
2627

2728
GeomCurve$new(mapping = mapping, data = data, stat = stat,
28-
position = position, arrow = arrow, lineend = lineend, na.rm = na.rm, ...)
29+
position = position, arrow = arrow, curvature = curvature, angle = angle,
30+
ncp = ncp, lineend = lineend, na.rm = na.rm, ...)
2931
}
3032

3133
GeomCurve <- proto(Geom, {
3234
objname <- "curve"
3335

34-
draw <- function(., data, scales, coordinates, arrow = NULL,
35-
lineend = "butt", na.rm = FALSE, ...) {
36+
draw <- function(., data, scales, coordinates, curvature, angle, ncp,
37+
arrow, lineend, na.rm, ...) {
3638

3739
data <- remove_missing(data, na.rm = na.rm,
38-
c("x", "y", "xend", "yend", "linetype", "size", "shape", "curvature", "angle", "ncp"),
40+
c("x", "y", "xend", "yend", "linetype", "size", "shape"),
3941
name = "geom_curve")
4042

4143
if (empty(data)) return(zeroGrob())
4244

4345
if (is.linear(coordinates)) {
4446
return(with(coord_transform(coordinates, data, scales),
4547
curveGrob(x, y, xend, yend, default.units="native",
46-
curvature=curvature[1], angle=angle[1], ncp=ncp[1],
48+
curvature=curvature, angle=angle, ncp=ncp,
4749
square = FALSE, squareShape = 1,
4850
inflect = FALSE, open = TRUE,
4951
gp = gpar(col=alpha(colour, alpha), fill = alpha(colour, alpha),
5052
lwd=size * .pt, lty=linetype, lineend = lineend),
5153
arrow = arrow)
5254
))
5355
}
54-
print("geom_curve is not implemented for non-linear coordinates")
56+
warning("geom_curve is not implemented for non-linear coordinates")
5557
return(zeroGrob())
5658
}
5759

5860

5961
default_stat <- function(.) StatIdentity
6062
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)
63+
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = NA)
6264
guide_geom <- function(.) "path"
6365

6466
})

man/geom_curve.Rd

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,21 @@
55
\title{Single curved line segments.}
66
\usage{
77
geom_curve(mapping = NULL, data = NULL, stat = "identity",
8-
position = "identity", arrow = NULL, lineend = "butt", na.rm = FALSE,
9-
...)
8+
position = "identity", curvature = 1, angle = 90, ncp = 1,
9+
arrow = NULL, lineend = "butt", na.rm = FALSE, ...)
1010
}
1111
\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.}
12+
\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. (see curveGrob)}
1513

16-
\item{data}{A layer specific dataset - only needed if you want to override
17-
the plot defaults.}
14+
\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. (see curveGrob)}
1815

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}
16+
\item{ncp}{The number of control points used to draw the curve. More control points creates a smoother curve. (see curveGrob)}
2417

2518
\item{arrow}{specification for arrow heads, as created by arrow()}
2619

2720
\item{lineend}{Line end style (round, butt, square)}
2821

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}
22+
\item{...}{Arguments to be passed to \code{curveGrob}.}
4123
}
4224
\description{
4325
Single curved line segments.
@@ -50,8 +32,8 @@ Single curved line segments.
5032
# Adding curve segments
5133
library(grid) # needed for arrow function
5234
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))
35+
b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25), curvature = 0.2)
36+
b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15), ncp = 2)
5537
b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
5638
}
5739
\seealso{

0 commit comments

Comments
 (0)