boB Rudis
7 years ago
10 changed files with 178 additions and 3 deletions
@ -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) |
||||
|
|
||||
|
} |
||||
|
|
||||
|
) |
||||
|
|
||||
|
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 791 KiB After Width: | Height: | Size: 791 KiB |
@ -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() |
||||
|
} |
Loading…
Reference in new issue