diff --git a/DESCRIPTION b/DESCRIPTION index ff7d010..21e4339 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ggalt Title: Extra Coordinate Systems, 'Geoms', Statistical Transformations, Scales and Fonts for 'ggplot2' -Version: 0.4.0 +Version: 0.5.0 Maintainer: Bob Rudis Authors@R: c( person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre")), @@ -73,6 +73,7 @@ Collate: 'geom_twoway_bar.r' 'geom_xspline.r' 'geom_xspline2.r' + 'geom_ubar.r' 'stat-stepribbon.r' 'ggalt-package.r' 'grob_absolute.r' diff --git a/NAMESPACE b/NAMESPACE index 7243cf7..2c2820f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(GeomEncircle) export(GeomHorizon) export(GeomLollipop) export(GeomStateface) +export(GeomUbar) export(GeomXSpline2) export(GeomXspline) export(Kb) @@ -38,6 +39,7 @@ export(geom_encircle) export(geom_horizon) export(geom_lollipop) export(geom_stateface) +export(geom_ubar) export(geom_xspline) export(load_stateface) export(show_stateface) diff --git a/NEWS.md b/NEWS.md index 8dd4e6b..febeb67 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.5.0 +===================== +* `geom_ubar()` : unform bar charts based on `geom_segment()` + 0.4.0 ===================== * Fixed `coord_proj()` diff --git a/R/geom_ubar.r b/R/geom_ubar.r new file mode 100644 index 0000000..1e92841 --- /dev/null +++ b/R/geom_ubar.r @@ -0,0 +1,80 @@ +#' Uniform "bar" charts +#' +#' I've been using `geom_segment` more to make "bar" charts, setting +#' `xend` to whatever `x` is and `yend` to `0`. The bar widths remain +#' constant without any tricks and you have granular control over the +#' segment width. I decided it was time to make a `geom`. +#' +#' Dumbbell dot plots — dot plots with two or more series of data — are an +#' alternative to the clustered bar chart or slope graph. +#' +#' @md +#' @section Aesthetics: +#' `geom_ubar`` understands the following aesthetics (required aesthetics are in bold): +#' +#' - **`x`** +#' - **`y`** +#' - `alpha` +#' - `colour` +#' - `group` +#' - `linetype` +#' - `size` +#' +#' @inheritParams ggplot2::layer +#' @param na.rm If `FALSE` (the default), removes missing values with +#' a warning. If `TRUE` silently removes missing values. +#' @param ... other arguments passed on to `layer`. These are +#' often aesthetics, used to set an aesthetic to a fixed value, like +#' `color = "red"` or `size = 3`. They may also be parameters +#' to the paired geom/stat. +#' @inheritParams ggplot2::layer +#' @export +#' @examples +#' library(ggplot2) +#' +#' data(economics) +#' ggplot(economics, aes(date, uempmed)) + +#' geom_ubar() +geom_ubar <- function(mapping = NULL, data = NULL, stat = "identity", + position = "identity", ..., + na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { + + layer( + data = data, + mapping = mapping, + stat = "identity", + geom = GeomUbar, + position = "identity", + show.legend = show.legend, + inherit.aes = inherit.aes, + params = list( + na.rm = na.rm, + ... + ) + ) +} + +#' @rdname ggalt-ggproto +#' @format NULL +#' @usage NULL +#' @export +GeomUbar <- ggproto("GeomUbar", Geom, + required_aes = c("x", "y"), + non_missing_aes = c("size"), + default_aes = aes( + size = 0.25, colour = "black", alpha = NA + ), + + setup_data = function(data, params) { + transform(data, xend = x, yend = 0) + }, + + draw_group = function(data, panel_scales, coord) { + + ggplot2::GeomSegment$draw_panel(data, panel_scales, coord) + + } + +) + + diff --git a/README.Rmd b/README.Rmd index 006b667..5f4feef 100644 --- a/README.Rmd +++ b/README.Rmd @@ -19,6 +19,7 @@ A compendium of 'geoms', 'coords', 'stats', scales and fonts for 'ggplot2', incl The following functions are implemented: +- `geom_ubar` : Uniform width bar charts - `geom_horizon` : Horizon charts (modified from ) - `coord_proj` : Like `coord_map`, only better (prbly shld use this with `geom_cartogram` as `geom_map`'s new defaults are ugh) - `geom_xspline` : Connect control points/observations with an X-spline diff --git a/README.md b/README.md index 4100cfb..f1a3b77 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A compendium of 'geoms', 'coords', 'stats', scales and fonts for 'ggplot2', incl The following functions are implemented: +- `geom_ubar` : Uniform width bar charts - `geom_horizon` : Horizon charts (modified from ) - `coord_proj` : Like `coord_map`, only better (prbly shld use this with `geom_cartogram` as `geom_map`'s new defaults are ugh) - `geom_xspline` : Connect control points/observations with an X-spline @@ -42,7 +43,7 @@ library(ggalt) # current verison packageVersion("ggalt") -## [1] '0.4.0' +## [1] '0.5.0' set.seed(1492) dat <- data.frame(x=c(1:10, 1:10, 1:10), diff --git a/README_figs/README-dumbbell2-1.png b/README_figs/README-dumbbell2-1.png index 9383505..e109741 100644 Binary files a/README_figs/README-dumbbell2-1.png and b/README_figs/README-dumbbell2-1.png differ diff --git a/README_figs/README-horizon-1.png b/README_figs/README-horizon-1.png index faad243..1a0b81a 100644 Binary files a/README_figs/README-horizon-1.png and b/README_figs/README-horizon-1.png differ diff --git a/man/geom_ubar.Rd b/man/geom_ubar.Rd new file mode 100644 index 0000000..874f313 --- /dev/null +++ b/man/geom_ubar.Rd @@ -0,0 +1,84 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geom_ubar.r +\name{geom_ubar} +\alias{geom_ubar} +\title{Uniform "bar" charts} +\usage{ +geom_ubar(mapping = NULL, data = NULL, stat = "identity", + position = "identity", ..., na.rm = FALSE, show.legend = NA, + inherit.aes = TRUE) +} +\arguments{ +\item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or +\code{\link{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}}. + + 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}} 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{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{na.rm}{If \code{FALSE} (the default), removes missing values with +a warning. If \code{TRUE} silently removes missing values.} + +\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.} + +\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}}.} +} +\description{ +I've been using \code{geom_segment} more to make "bar" charts, setting +\code{xend} to whatever \code{x} is and \code{yend} to \code{0}. The bar widths remain +constant without any tricks and you have granular control over the +segment width. I decided it was time to make a \code{geom}. +} +\details{ +Dumbbell dot plots — dot plots with two or more series of data — are an +alternative to the clustered bar chart or slope graph. +} +\section{Aesthetics}{ + +`geom_ubar`` understands the following aesthetics (required aesthetics are in bold): +\itemize{ +\item \strong{\code{x}} +\item \strong{\code{y}} +\item \code{alpha} +\item \code{colour} +\item \code{group} +\item \code{linetype} +\item \code{size} +} +} + +\examples{ +library(ggplot2) + +data(economics) +ggplot(economics, aes(date, uempmed)) + + geom_ubar() +} diff --git a/man/ggalt-ggproto.Rd b/man/ggalt-ggproto.Rd index b0e51ba..2962ed1 100644 --- a/man/ggalt-ggproto.Rd +++ b/man/ggalt-ggproto.Rd @@ -1,7 +1,8 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/coord_proj.r, R/geom_ash.r, R/geom_bkde.r, % R/geom_bkde2d.r, R/geom_dumbbell.R, R/geom_encircle.r, R/geom_lollipop.r, -% R/geom_xspline.r, R/geom_xspline2.r, R/stat-stepribbon.r, R/stateface.r +% R/geom_xspline.r, R/geom_xspline2.r, R/geom_ubar.r, R/stat-stepribbon.r, +% R/stateface.r \docType{data} \name{CoordProj} \alias{CoordProj} @@ -16,6 +17,7 @@ \alias{GeomXspline} \alias{StatXspline} \alias{GeomXSpline2} +\alias{GeomUbar} \alias{StatStepribbon} \alias{GeomStateface} \title{Geom Proto}