@ -0,0 +1,119 @@ |
|||
#' @title Draw spikelines on a plot |
|||
#' @description Segment reference lines that originate at an point |
|||
#' @inheritParams ggplot2::geom_line |
|||
#' @examples |
|||
#' |
|||
#' mtcars$name <- rownames(mtcars) |
|||
#' |
|||
#' p <- ggplot(data = mtcars, aes(x=mpg,y=disp)) + geom_point() |
|||
#' |
|||
#' p + geom_spikelines(data = mtcars[mtcars$carb==4,],linetype = 2) |
|||
#' |
|||
#' p + geom_spikelines(data = mtcars[mtcars$carb==4,],aes(colour = factor(gear)), linetype = 2) |
|||
#' |
|||
#' \dontrun{ |
|||
#' require(ggrepel) |
|||
#' p + geom_spikelines(data = mtcars[mtcars$carb==4,],aes(colour = factor(gear)), linetype = 2) + |
|||
#' ggrepel::geom_label_repel(data = mtcars[mtcars$carb==4,],aes(label = name)) |
|||
#' } |
|||
#' |
|||
#' @rdname geom_spikelines |
|||
#' @author Jonathan Sidi |
|||
#' @export |
|||
|
|||
geom_spikelines <- function(mapping = NULL, data = NULL, |
|||
stat = "identity", position = "identity", |
|||
..., |
|||
arrow = NULL, |
|||
lineend = "butt", |
|||
linejoin = "round", |
|||
na.rm = FALSE, |
|||
show.legend = NA, |
|||
inherit.aes = TRUE) { |
|||
layer( |
|||
data = data, |
|||
mapping = mapping, |
|||
stat = stat, |
|||
geom = GeomSpikelines, |
|||
position = position, |
|||
show.legend = show.legend, |
|||
inherit.aes = inherit.aes, |
|||
params = list( |
|||
arrow = arrow, |
|||
lineend = lineend, |
|||
linejoin = linejoin, |
|||
na.rm = na.rm, |
|||
... |
|||
) |
|||
) |
|||
} |
|||
|
|||
#' @rdname ggalt-ggproto |
|||
#' @format NULL |
|||
#' @usage NULL |
|||
#' @export |
|||
GeomSpikelines <- ggproto("GeomSpikelines", Geom, |
|||
required_aes = c("x", "y"), |
|||
non_missing_aes = c("linetype", "size", "shape"), |
|||
default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA), |
|||
|
|||
draw_panel = function(data, panel_params, coord, arrow = NULL, |
|||
lineend = "butt", linejoin = "round", na.rm = FALSE) { |
|||
|
|||
data1 <- data |
|||
data2 <- data |
|||
|
|||
data1$xend <- data$x |
|||
data1$yend <- data$y |
|||
data1$y <- 0 |
|||
|
|||
|
|||
data2$xend <- data$x |
|||
data2$yend <- data$y |
|||
data2$x <- 0 |
|||
|
|||
data <- rbind(data1,data2) |
|||
|
|||
data <- remove_missing(data, na.rm = na.rm, |
|||
c("x", "y", "linetype", "size", "shape"), |
|||
name = "geom_spikelines") |
|||
if (empty(data)) return(zeroGrob()) |
|||
|
|||
if (coord$is_linear()) { |
|||
coord <- coord$transform(data, panel_params) |
|||
|
|||
ret <- grid::segmentsGrob(coord$x, coord$y, coord$xend, coord$yend, |
|||
default.units = "native", |
|||
gp = grid::gpar( |
|||
col = alpha(coord$colour, coord$alpha), |
|||
fill = alpha(coord$colour, coord$alpha), |
|||
lwd = coord$size * .pt, |
|||
lty = coord$linetype, |
|||
lineend = lineend, |
|||
linejoin = linejoin |
|||
), |
|||
arrow = arrow |
|||
) |
|||
|
|||
return(ret) |
|||
} |
|||
|
|||
data$group <- 1:nrow(data) |
|||
starts <- subset(data, select = c(-xend, -yend)) |
|||
ends <- plyr::rename(subset(data, select = c(-x, -y)), c("xend" = "x", "yend" = "y"), |
|||
warn_missing = FALSE) |
|||
|
|||
pieces <- rbind(starts, ends) |
|||
pieces <- pieces[order(pieces$group),] |
|||
|
|||
GeomPath$draw_panel(pieces, panel_params, coord, arrow = arrow, |
|||
lineend = lineend) |
|||
}, |
|||
|
|||
draw_key = draw_key_path |
|||
) |
|||
|
|||
empty <- function (df) |
|||
{ |
|||
is.null(df) || nrow(df) == 0 || ncol(df) == 0 |
|||
} |
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 40 KiB |
Before Width: | Height: | Size: 235 KiB After Width: | Height: | Size: 235 KiB |
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
Before Width: | Height: | Size: 101 KiB After Width: | Height: | Size: 102 KiB |
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 184 KiB After Width: | Height: | Size: 184 KiB |
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 120 KiB |
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 49 KiB After Width: | Height: | Size: 49 KiB |
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 54 KiB |
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 791 KiB After Width: | Height: | Size: 795 KiB |
Before Width: | Height: | Size: 145 KiB After Width: | Height: | Size: 147 KiB |
After Width: | Height: | Size: 135 KiB |
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 108 KiB |
Before Width: | Height: | Size: 132 KiB After Width: | Height: | Size: 134 KiB |
Before Width: | Height: | Size: 138 KiB After Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 138 KiB After Width: | Height: | Size: 137 KiB |
Before Width: | Height: | Size: 134 KiB After Width: | Height: | Size: 134 KiB |
Before Width: | Height: | Size: 131 KiB After Width: | Height: | Size: 131 KiB |
Before Width: | Height: | Size: 136 KiB After Width: | Height: | Size: 137 KiB |
Before Width: | Height: | Size: 138 KiB After Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 112 KiB After Width: | Height: | Size: 112 KiB |
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
Before Width: | Height: | Size: 39 KiB After Width: | Height: | Size: 39 KiB |
@ -0,0 +1,85 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/geom_spikelines.R |
|||
\name{geom_spikelines} |
|||
\alias{geom_spikelines} |
|||
\title{Draw spikelines on a plot} |
|||
\usage{ |
|||
geom_spikelines(mapping = NULL, data = NULL, stat = "identity", |
|||
position = "identity", ..., arrow = NULL, lineend = "butt", |
|||
linejoin = "round", na.rm = FALSE, show.legend = NA, |
|||
inherit.aes = TRUE) |
|||
} |
|||
\arguments{ |
|||
\item{mapping}{Set of aesthetic mappings created by \code{\link[=aes]{aes()}} or |
|||
\code{\link[=aes_]{aes_()}}. If specified and \code{inherit.aes = TRUE} (the |
|||
default), it is combined with the default mapping at the top level of the |
|||
plot. You must supply \code{mapping} if there is no plot mapping.} |
|||
|
|||
\item{data}{The data to be displayed in this layer. There are three |
|||
options: |
|||
|
|||
If \code{NULL}, the default, the data is inherited from the plot |
|||
data as specified in the call to \code{\link[=ggplot]{ggplot()}}. |
|||
|
|||
A \code{data.frame}, or other object, will override the plot |
|||
data. All objects will be fortified to produce a data frame. See |
|||
\code{\link[=fortify]{fortify()}} for which variables will be created. |
|||
|
|||
A \code{function} will be called with a single argument, |
|||
the plot data. The return value must be a \code{data.frame.}, and |
|||
will be used as the layer data.} |
|||
|
|||
\item{stat}{The statistical transformation to use on the data for this |
|||
layer, as a string.} |
|||
|
|||
\item{position}{Position adjustment, either as a string, or the result of |
|||
a call to a position adjustment function.} |
|||
|
|||
\item{...}{Other arguments passed on to \code{\link[=layer]{layer()}}. These are |
|||
often aesthetics, used to set an aesthetic to a fixed value, like |
|||
\code{color = "red"} or \code{size = 3}. They may also be parameters |
|||
to the paired geom/stat.} |
|||
|
|||
\item{arrow}{Arrow specification, as created by \code{\link[grid:arrow]{grid::arrow()}}.} |
|||
|
|||
\item{lineend}{Line end style (round, butt, square).} |
|||
|
|||
\item{linejoin}{Line join style (round, mitre, bevel).} |
|||
|
|||
\item{na.rm}{If \code{FALSE}, the default, missing values are removed with |
|||
a warning. If \code{TRUE}, missing values are silently removed.} |
|||
|
|||
\item{show.legend}{logical. Should this layer be included in the legends? |
|||
\code{NA}, the default, includes if any aesthetics are mapped. |
|||
\code{FALSE} never includes, and \code{TRUE} always includes. |
|||
It can also be a named logical vector to finely select the aesthetics to |
|||
display.} |
|||
|
|||
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, |
|||
rather than combining with them. This is most useful for helper functions |
|||
that define both data and aesthetics and shouldn't inherit behaviour from |
|||
the default plot specification, e.g. \code{\link[=borders]{borders()}}.} |
|||
} |
|||
\description{ |
|||
Segment reference lines that originate at an point |
|||
} |
|||
\examples{ |
|||
|
|||
mtcars$name <- rownames(mtcars) |
|||
|
|||
p <- ggplot(data = mtcars, aes(x=mpg,y=disp)) + geom_point() |
|||
|
|||
p + geom_spikelines(data = mtcars[mtcars$carb==4,],linetype = 2) |
|||
|
|||
p + geom_spikelines(data = mtcars[mtcars$carb==4,],aes(colour = factor(gear)), linetype = 2) |
|||
|
|||
\dontrun{ |
|||
require(ggrepel) |
|||
p + geom_spikelines(data = mtcars[mtcars$carb==4,],aes(colour = factor(gear)), linetype = 2) + |
|||
ggrepel::geom_label_repel(data = mtcars[mtcars$carb==4,],aes(label = name)) |
|||
} |
|||
|
|||
} |
|||
\author{ |
|||
Jonathan Sidi |
|||
} |