Skip to content

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

Merged
merged 3 commits into from
Jun 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
64 changes: 64 additions & 0 deletions R/geom-curve.r
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
Copy link
Member

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

#' @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")
Copy link
Member

Choose a reason for hiding this comment

The 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"

})
2 changes: 1 addition & 1 deletion man/geom_bar.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
67 changes: 67 additions & 0 deletions man/geom_curve.Rd
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.
}

2 changes: 1 addition & 1 deletion man/geom_density2d.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down