From 144ad187816dc64da456eebb1b7e9b3173212fc1 Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Sat, 9 Apr 2016 08:55:14 -0400 Subject: [PATCH] horizontal param for lollipops --- R/geom_lollipop.r | 20 +++++++++++++++++--- man/geom_lollipop.Rd | 12 +++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/R/geom_lollipop.r b/R/geom_lollipop.r index d4f5784..931dd58 100644 --- a/R/geom_lollipop.r +++ b/R/geom_lollipop.r @@ -19,11 +19,17 @@ #' 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. +#' @param If \code{horizontal} is \code{FALSE} (the default), the function +#' will draw the lollipops up from the X axis (i.e. it will set \code{xend} +#' to \code{x} & \code{yend} to \code{0}). If \code{TRUE}, it wiill set +#' \code{yend} to \code{y} & \code{xend} to \code{0}). Make sure you map the +#' \code{x} & \code{y} aesthetics accordingly. #' @param point.size the size of the point #' @param point.colour the colour of the point #' @inheritParams ggplot2::layer #' @export geom_lollipop <- function(mapping = NULL, data = NULL, ..., + horizontal = FALSE, point.colour = NULL, point.size = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { @@ -37,6 +43,7 @@ geom_lollipop <- function(mapping = NULL, data = NULL, ..., inherit.aes = inherit.aes, params = list( na.rm = na.rm, + horizontal = horizontal, point.colour = point.colour, point.size = point.size, ... @@ -50,18 +57,23 @@ geom_lollipop <- function(mapping = NULL, data = NULL, ..., #' @export GeomLollipop <- ggproto("GeomLollipop", Geom, required_aes = c("x", "y"), - non_missing_aes = c("size", "shape", "point.colour", "point.size"), + non_missing_aes = c("size", "shape", "point.colour", "point.size", "horizontal"), default_aes = aes( shape = 19, colour = "black", size = 0.5, fill = NA, alpha = NA, stroke = 0.5 ), setup_data = function(data, params) { - transform(data, xend = x, yend = 0) + if (params$horizontal) { + transform(data, yend = y, xend = 0) + } else { + transform(data, xend = x, yend = 0) + } }, draw_group = function(data, panel_scales, coord, - point.colour = NULL, point.size = NULL) { + point.colour = NULL, point.size = NULL, + horizontal = FALSE) { points <- data points$colour <- point.colour %||% data$colour @@ -76,3 +88,5 @@ GeomLollipop <- ggproto("GeomLollipop", Geom, draw_key = draw_key_point ) + + diff --git a/man/geom_lollipop.Rd b/man/geom_lollipop.Rd index 6d7482c..7782e07 100644 --- a/man/geom_lollipop.Rd +++ b/man/geom_lollipop.Rd @@ -4,9 +4,9 @@ \alias{geom_lollipop} \title{Lollipop charts} \usage{ -geom_lollipop(mapping = NULL, data = NULL, ..., point.colour = NULL, - point.size = NULL, na.rm = FALSE, show.legend = NA, - inherit.aes = TRUE) +geom_lollipop(mapping = NULL, data = NULL, ..., horizontal = FALSE, + point.colour = NULL, point.size = NULL, na.rm = FALSE, + show.legend = NA, inherit.aes = TRUE) } \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or @@ -48,6 +48,12 @@ a warning. If \code{TRUE} silently removes missing values.} 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}}.} + +\item{If}{\code{horizontal} is \code{FALSE} (the default), the function +will draw the lollipops up from the X axis (i.e. it will set \code{xend} +to \code{x} & \code{yend} to \code{0}). If \code{TRUE}, it wiill set +\code{yend} to \code{y} & \code{xend} to \code{0}). Make sure you map the +\code{x} & \code{y} aesthetics accordingly.} } \description{ The lollipop geom is used to create lollipop charts.