Skip to content

Commit c582922

Browse files
committed
Merge pull request #1088 from veraanadi/master
Added new function geom_curve
2 parents db7738d + 039cd01 commit c582922

File tree

7 files changed

+137
-2
lines changed

7 files changed

+137
-2
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
@@ -1,5 +1,7 @@
11
* New `theme_void()`, which is completely empty. Useful for plots with non
22
standard coordinates or for producing numerical drawings with R. (@jiho, #976)
3+
* Added new function geom_curve to add curved lines to plot (similar to
4+
geom_segment to add straight lines). (@veraanadi, #1088)
35

46
* Create correct legend for continuous color even if there is only one color
57
(@krlmlr, #943)

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 grid::curveGrob
7+
#' @inheritParams geom_point
8+
#' @inheritParams geom_segment
9+
#'
10+
#' @seealso \code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
11+
#' segment lines and paths.
12+
#' @export
13+
#' @examples
14+
#' # Adding curve segments
15+
#' library(grid) # needed for arrow function
16+
#' b <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
17+
#' b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25), curvature = 0.2)
18+
#' b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15), ncp = 2)
19+
#' b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
20+
21+
22+
geom_curve <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
23+
curvature = 1, angle = 90, ncp = 1, arrow = NULL, lineend = "butt",
24+
na.rm = FALSE, ...) {
25+
26+
GeomCurve$new(mapping = mapping, data = data, stat = stat,
27+
position = position, arrow = arrow, curvature = curvature, angle = angle,
28+
ncp = ncp, lineend = lineend, na.rm = na.rm, ...)
29+
}
30+
31+
GeomCurve <- proto(Geom, {
32+
objname <- "curve"
33+
34+
draw <- function(., data, scales, coordinates, curvature, angle, ncp,
35+
arrow, lineend, na.rm, ...) {
36+
37+
data <- remove_missing(data, na.rm = na.rm,
38+
c("x", "y", "xend", "yend", "linetype", "size", "shape"),
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, angle=angle, ncp=ncp,
47+
square = FALSE, squareShape = 1,
48+
inflect = FALSE, open = TRUE,
49+
gp = gpar(col=alpha(colour, alpha), lwd=size * .pt,
50+
lty=linetype, lineend = lineend),
51+
arrow = arrow)
52+
))
53+
}
54+
warning("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)
62+
guide_geom <- function(.) "path"
63+
64+
})

man/geom_bar.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ m + geom_bar()
145145
}
146146
}
147147
\seealso{
148-
\code{\link{stat_bin}} for more details of the binning alogirithm,
148+
\code{\link{stat_bin}} for more details of the binning algorithm,
149149
\code{\link{position_dodge}} for creating side-by-side barcharts,
150150
\code{\link{position_stack}} for more info on stacking,
151151
}

man/geom_curve.Rd

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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", curvature = 1, angle = 90, ncp = 1,
9+
arrow = NULL, lineend = "butt", na.rm = FALSE, ...)
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{curvature}{A numeric value giving the amount of curvature.
26+
Negative values produce left-hand curves, positive values
27+
produce right-hand curves, and zero produces a straight line.}
28+
29+
\item{angle}{A numeric value between 0 and 180,
30+
giving an amount to skew the control
31+
points of the curve. Values less than 90 skew the curve towards
32+
the start point and values greater than 90 skew the curve
33+
towards the end point.}
34+
35+
\item{ncp}{The number of control points used to draw the curve.
36+
More control points creates a smoother curve.}
37+
38+
\item{arrow}{A list describing arrow heads to place at either end
39+
of the curve, as produced by the \code{arrow} function.}
40+
41+
\item{lineend}{Line end style (round, butt, square)}
42+
43+
\item{na.rm}{If \code{FALSE} (the default), removes missing values with
44+
a warning. If \code{TRUE} silently removes missing values.}
45+
46+
\item{...}{Arguments to be passed to \code{curveGrob}.}
47+
}
48+
\description{
49+
Single curved line segments.
50+
}
51+
\section{Aesthetics}{
52+
53+
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "curve")}
54+
}
55+
\examples{
56+
# Adding curve segments
57+
library(grid) # needed for arrow function
58+
b <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
59+
b + geom_curve(aes(x = 2, y = 15, xend = 2, yend = 25), curvature = 0.2)
60+
b + geom_curve(aes(x = 2, y = 15, xend = 3, yend = 15), ncp = 2)
61+
b + geom_curve(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
62+
}
63+
\seealso{
64+
\code{\link{geom_segment}}, \code{\link{geom_path}} and \code{\link{geom_line}} for multi-
65+
segment lines and paths.
66+
}
67+

man/geom_density2d.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ include aesthetics whose values you want to set, not map. See
3636
\code{\link{layer}} for more details.}
3737
}
3838
\description{
39-
Perform a 2D kernel density estimatation using kde2d and display the
39+
Perform a 2D kernel density estimation using kde2d and display the
4040
results with contours.
4141
}
4242
\details{

0 commit comments

Comments
 (0)