diff --git a/DESCRIPTION b/DESCRIPTION index 7579be2..1606a1d 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.3.0.9000 +Version: 0.3.1.9000 Maintainer: Bob Rudis Authors@R: c( person("Bob", "Rudis", email = "bob@rudis.net", role = c("aut", "cre")), @@ -48,21 +48,24 @@ RoxygenNote: 5.0.1 VignetteBuilder: knitr Collate: 'a-pokemon-colors.r' + 'annotate_textp.r' 'coord_proj.r' 'formatters.r' + 'fortify.r' 'geom2plotly.r' 'geom_ash.r' 'geom_bkde.r' 'geom_bkde2d.r' + 'geom_dumbbell.R' 'geom_encircle.r' + 'geom_lollipop.r' 'geom_table.r' + 'geom_twoway_bar.r' 'geom_xspline.r' - 'annotate_textp.r' 'geom_xspline2.r' + 'stat-stepribbon.r' 'ggalt-package.r' 'grob_absolute.r' - 'geom_lollipop.r' - 'geom_dumbbell.R' 'guide_axis.r' 'pokemon.r' 'stateface.r' diff --git a/NAMESPACE b/NAMESPACE index acd7326..661421b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(fortify,table) S3method(grid.draw,absoluteGrob) S3method(grobHeight,absoluteGrob) S3method(grobWidth,absoluteGrob) @@ -13,6 +14,7 @@ export(GeomDumbbell) export(GeomEncircle) export(GeomLollipop) export(GeomStateface) +export(GeomTwowayBar) export(GeomXSpline2) export(GeomXspline) export(Kb) @@ -20,6 +22,7 @@ export(Mb) export(StatAsh) export(StatBkde) export(StatBkde2d) +export(StatStepribbon) export(StatXspline) export(annotate_textp) export(byte_format) @@ -31,6 +34,7 @@ export(geom_dumbbell) export(geom_encircle) export(geom_lollipop) export(geom_stateface) +export(geom_twoway_bar) export(geom_xspline) export(list_avatars) export(load_stateface) @@ -42,6 +46,7 @@ export(show_stateface) export(stat_ash) export(stat_bkde) export(stat_bkde2d) +export(stat_stepribbon) export(stat_xspline) export(to_basic.GeomBkde2d) export(to_basic.GeomStateface) diff --git a/R/fortify.r b/R/fortify.r new file mode 100644 index 0000000..520ee00 --- /dev/null +++ b/R/fortify.r @@ -0,0 +1,9 @@ +#' Fortify contingency tables +#' +#' @param model the contingency table +#' @param data data (unused) +#' @param ... (unused) +#' @export +fortify.table <- function(model, data, ...) { + as_tibble(as.data.frame(model, stringsAsFactors=FALSE)) +} diff --git a/R/geom_twoway_bar.r b/R/geom_twoway_bar.r new file mode 100644 index 0000000..9bc9bad --- /dev/null +++ b/R/geom_twoway_bar.r @@ -0,0 +1,73 @@ +#' @export +geom_twoway_bar <- function(mapping = NULL, data = NULL, + stat = "identity", + width = NULL, + ..., + na.rm = FALSE, + show.legend = NA, + inherit.aes = TRUE) { + + layer( + data = data, + mapping = mapping, + stat = stat, + geom = GeomTwowayBar, + position = "stack", + show.legend = show.legend, + inherit.aes = inherit.aes, + params = list( + width = width, + na.rm = na.rm, + ... + ) + ) +} + +#' @rdname ggalt-ggproto +#' @format NULL +#' @usage NULL +#' @export +GeomTwowayBar <- ggproto("GeomTwowayBar", GeomRect, + required_aes = c("x", "y"), + + do_setup_data = function(data, params) { + data$width <- data$width %||% + params$width %||% (resolution(data$x, FALSE) * 0.9) + + d_plus <- subset(data, y>=0, drop=FALSE) + d_minus <- subset(data, y<0, drop=FALSE) + + d_plus <- transform(d_plus, + ymin = pmin(y, 0), ymax = pmax(y, 0), + xmin = x - width / 2, xmax = x + width / 2, width = NULL, + is_plus = TRUE + ) + + d_minus <- transform(d_minus, + ymin = pmin(y, 0), ymax = pmax(y, 0), + xmin = x - width / 2, xmax = x + width / 2, width = NULL, + y = abs(y), + is_plus = FALSE + ) + + cat("setup_data() after _________\n") + print(rbind(d_plus, d_minus)) + + rbind(d_plus, d_minus) + + }, + + draw_panel = function(self, data, panel_scales, coord, width=NULL) { + + cat("draw_panel() _________\n") + print(data) +# +# d_plus <- subset(data, is_plus) +# d_minus <- subset(data, !is_plus) +# d_minus$y <- -d_minus$y + + gList( + ggplot2::ggproto_parent(GeomBar, self)$draw_panel(data, panel_scales, coord) + ) + } +) diff --git a/R/stat-stepribbon.r b/R/stat-stepribbon.r new file mode 100644 index 0000000..dd4d485 --- /dev/null +++ b/R/stat-stepribbon.r @@ -0,0 +1,87 @@ +#' Step ribbon statistic +#' +#' Provides stairstep values for ribbon plots +#' +#' @inheritParams ggplot2::geom_ribbon +#' @param direction \code{hv} for horizontal-veritcal steps, \code{vh} for +#' vertical-horizontal steps +#' @references \url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs} +#' @export +#' @examples +#' x <- 1:10 +#' df <- data.frame(x=x, y=x+10, ymin=x+7, ymax=x+12) +#' +#' gg <- ggplot(df, aes(x, y)) +#' gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), +#' stat="stepribbon", fill="#b2b2b2") +#' gg <- gg + geom_step(color="#2b2b2b") +#' gg +#' +#' gg <- ggplot(df, aes(x, y)) +#' gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), +#' stat="stepribbon", fill="#b2b2b2", +#' direction="hv") +#' gg <- gg + geom_step(color="#2b2b2b") +#' gg +stat_stepribbon <- function(mapping=NULL, data=NULL, geom="ribbon", + position="identity", + na.rm=FALSE, show.legend=NA, inherit.aes=TRUE, + direction="hv", ...) { + + ggplot2::layer( + data = data, + mapping = mapping, + stat = Stepribbon, + geom = geom, + position = position, + show.legend = show.legend, + inherit.aes = inherit.aes, + params = list( + na.rm = na.rm, + direction = direction, + ... + ) + ) +} + +#' @rdname ggalt-ggproto +#' @format NULL +#' @usage NULL +#' @references \url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs} +#' @export +StatStepribbon <- + ggproto( + "StepRibbon", Stat, + + required_aes = c("x", "ymin", "ymax"), + + compute_group = function(data, scales, direction="hv", + yvars=c("ymin", "ymax"), ...) { + stairstepn(data=data, direction=direction, yvars=yvars) + } + + ) + +stairstepn <- function(data, direction="hv", yvars="y") { + + direction <- match.arg(direction, c("hv", "vh")) + + data <- as.data.frame(data)[order(data$x),] + + n <- nrow(data) + + if (direction == "vh") { + xs <- rep(1:n, each=2)[-2*n] + ys <- c(1, rep( 2:n, each=2)) + } else { + ys <- rep(1:n, each=2)[-2*n] + xs <- c(1, rep(2:n, each=2)) + } + + data.frame( + x=data$x[xs], + data[ys, yvars, drop=FALSE], + data[xs, setdiff(names(data), c("x", yvars)), drop=FALSE] + ) + +} diff --git a/README.Rmd b/README.Rmd index 921b456..0bf2463 100644 --- a/README.Rmd +++ b/README.Rmd @@ -36,7 +36,8 @@ The following functions are implemented: - `scale_fill_pokemon` : discrete pokemon scales (data taken from the hard work by the ) - `byte_format`: + helpers. e.g. turn `10000` into `10 Kb` - `geom_lollipop()`: Dead easy lollipops (horizontal or vertical) -- `geom_dumbberll()` : Dead easy dumbbell plots +- `geom_dumbbell()` : Dead easy dumbbell plots +- `geom_stepribbon()` : Step ribbons ### Installation @@ -237,6 +238,26 @@ gg + geom_encircle(data=ss, colour="blue", s_shape=0.9, expand=0.07) + geom_point() + geom_point(data=ss, colour="blue") ``` +### Step ribbons + +```{r stepribbon} +x <- 1:10 +df <- data.frame(x=x, y=x+10, ymin=x+7, ymax=x+12) + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2") +gg <- gg + geom_step(color="#2b2b2b") +gg + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2", + direction="vh") +gg <- gg + geom_step(color="#2b2b2b") +gg +``` + ### Lollipop charts ```{r lollipop} diff --git a/README.md b/README.md index 91652de..ecc800c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ The following functions are implemented: - `scale_fill_pokemon` : discrete pokemon scales (data taken from the hard work by the ) - `byte_format`: + helpers. e.g. turn `10000` into `10 Kb` - `geom_lollipop()`: Dead easy lollipops (horizontal or vertical) -- `geom_dumbberll()` : Dead easy dumbbell plots +- `geom_dumbbell()` : Dead easy dumbbell plots +- `geom_stepribbon()` : Step ribbons ### Installation @@ -248,9 +249,8 @@ m + stat_bkde2d(bandwidth=c(0.5, 4), aes(fill = ..level..), geom = "polygon") ``` r world <- map_data("world") #> -#> # ATTENTION: maps v3.0 has an updated 'world' map. # -#> # Many country borders and names have changed since 1990. # -#> # Type '?world' or 'news(package="maps")'. See README_v3. # +#> # maps v3.1: updated 'world': all lakes moved to separate new # +#> # 'lakes' database. Type '?world' or 'news(package="maps")'. # world <- world[world$region != "Antarctica",] gg <- ggplot() @@ -346,6 +346,33 @@ gg + geom_encircle(data=ss, colour="blue", s_shape=0.9, expand=0.07) + +### Step ribbons + +``` r +x <- 1:10 +df <- data.frame(x=x, y=x+10, ymin=x+7, ymax=x+12) + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2") +gg <- gg + geom_step(color="#2b2b2b") +gg +``` + + + +``` r + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2", + direction="vh") +gg <- gg + geom_step(color="#2b2b2b") +gg +``` + + + ### Lollipop charts ``` r diff --git a/README_figs/README-coord_proj-1.png b/README_figs/README-coord_proj-1.png index f16b8b4..5e665f1 100644 Binary files a/README_figs/README-coord_proj-1.png and b/README_figs/README-coord_proj-1.png differ diff --git a/README_figs/README-dumbbell-1.png b/README_figs/README-dumbbell-1.png index 488a003..a29345b 100644 Binary files a/README_figs/README-dumbbell-1.png and b/README_figs/README-dumbbell-1.png differ diff --git a/README_figs/README-encircle-1.png b/README_figs/README-encircle-1.png index e6ceab1..8a4e481 100644 Binary files a/README_figs/README-encircle-1.png and b/README_figs/README-encircle-1.png differ diff --git a/README_figs/README-encircle-2.png b/README_figs/README-encircle-2.png index bf21d23..9fc491c 100644 Binary files a/README_figs/README-encircle-2.png and b/README_figs/README-encircle-2.png differ diff --git a/README_figs/README-encircle-3.png b/README_figs/README-encircle-3.png index e51c1d2..7a87b9a 100644 Binary files a/README_figs/README-encircle-3.png and b/README_figs/README-encircle-3.png differ diff --git a/README_figs/README-encircle-4.png b/README_figs/README-encircle-4.png index ba10969..78770b7 100644 Binary files a/README_figs/README-encircle-4.png and b/README_figs/README-encircle-4.png differ diff --git a/README_figs/README-encircle-5.png b/README_figs/README-encircle-5.png index fae3554..e877927 100644 Binary files a/README_figs/README-encircle-5.png and b/README_figs/README-encircle-5.png differ diff --git a/README_figs/README-encircle-6.png b/README_figs/README-encircle-6.png index 02d7aeb..e4d2ce7 100644 Binary files a/README_figs/README-encircle-6.png and b/README_figs/README-encircle-6.png differ diff --git a/README_figs/README-encircle-7.png b/README_figs/README-encircle-7.png index 3d13221..9479cbd 100644 Binary files a/README_figs/README-encircle-7.png and b/README_figs/README-encircle-7.png differ diff --git a/README_figs/README-stepribbon-1.png b/README_figs/README-stepribbon-1.png new file mode 100644 index 0000000..e3f8449 Binary files /dev/null and b/README_figs/README-stepribbon-1.png differ diff --git a/README_figs/README-stepribbon-2.png b/README_figs/README-stepribbon-2.png new file mode 100644 index 0000000..3a4308f Binary files /dev/null and b/README_figs/README-stepribbon-2.png differ diff --git a/man/fortify.table.Rd b/man/fortify.table.Rd new file mode 100644 index 0000000..264f7de --- /dev/null +++ b/man/fortify.table.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fortify.r +\name{fortify.table} +\alias{fortify.table} +\title{Fortify contingency tables} +\usage{ +\method{fortify}{table}(model, data, ...) +} +\arguments{ +\item{model}{the contingency table} + +\item{data}{data (unused)} + +\item{...}{(unused)} +} +\description{ +Fortify contingency tables +} + diff --git a/man/geom_encircle.Rd b/man/geom_encircle.Rd index 31a3553..311e72a 100644 --- a/man/geom_encircle.Rd +++ b/man/geom_encircle.Rd @@ -65,6 +65,11 @@ gg +geom_encircle(data=subset(d, x==2), colour="cyan", spread=0.04) + gg <- ggplot(mpg, aes(displ, hwy)) gg + geom_encircle(data=subset(mpg, hwy>40)) + geom_point() +gg + geom_encircle(aes(group=manufacturer)) + geom_point() +gg + geom_encircle(aes(group=manufacturer,fill=manufacturer),alpha=0.4)+ + geom_point() +gg + geom_encircle(aes(group=manufacturer,colour=manufacturer))+ + geom_point() ss <- subset(mpg,hwy>31 & displ<2) diff --git a/man/geom_stateface.Rd b/man/geom_stateface.Rd index 541dd97..d1c4432 100644 --- a/man/geom_stateface.Rd +++ b/man/geom_stateface.Rd @@ -48,7 +48,7 @@ abels by. Useful for offsetting text from points, particularly on discrete scales.} \item{check_overlap}{If \code{TRUE}, text that overlaps previous text in the -same layer will not be plotted. A quick and dirty way} +same layer will not be plotted.} \item{na.rm}{If \code{FALSE} (the default), removes missing values with a warning. If \code{TRUE} silently removes missing values.} diff --git a/man/ggalt-ggproto.Rd b/man/ggalt-ggproto.Rd index ec4bdf1..c70fa7d 100644 --- a/man/ggalt-ggproto.Rd +++ b/man/ggalt-ggproto.Rd @@ -1,5 +1,5 @@ % 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_encircle.r, R/geom_xspline.r, R/geom_xspline2.r, R/geom_lollipop.r, R/geom_dumbbell.R, R/stateface.r +% 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_twoway_bar.r, R/geom_xspline.r, R/geom_xspline2.r, R/stat-stepribbon.r, R/stateface.r \docType{data} \name{CoordProj} \alias{CoordProj} @@ -9,11 +9,13 @@ \alias{GeomEncircle} \alias{GeomLollipop} \alias{GeomStateface} +\alias{GeomTwowayBar} \alias{GeomXSpline2} \alias{GeomXspline} \alias{StatAsh} \alias{StatBkde} \alias{StatBkde2d} +\alias{StatStepribbon} \alias{StatXspline} \title{Geom Proto} \description{ @@ -27,6 +29,9 @@ Geom Proto Geom Proto } +\references{ +\url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs} +} \keyword{datasets} \keyword{internal} diff --git a/man/stat_stepribbon.Rd b/man/stat_stepribbon.Rd new file mode 100644 index 0000000..a66457d --- /dev/null +++ b/man/stat_stepribbon.Rd @@ -0,0 +1,76 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/stat-stepribbon.r +\name{stat_stepribbon} +\alias{stat_stepribbon} +\title{Step ribbon statistic} +\usage{ +stat_stepribbon(mapping = NULL, data = NULL, geom = "ribbon", + position = "identity", na.rm = FALSE, show.legend = NA, + inherit.aes = TRUE, direction = "hv", ...) +} +\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{position}{Position adjustment, either as a string, or the result of +a call to a position adjustment function.} + +\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}}.} + +\item{direction}{\code{hv} for horizontal-veritcal steps, \code{vh} for +vertical-horizontal steps} + +\item{...}{other arguments passed on to \code{\link{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.} +} +\description{ +Provides stairstep values for ribbon plots +} +\examples{ +df <- data.frame(x=1:10, y=x+10, ymin=x+7, ymax=x+12) + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2") +gg <- gg + geom_step(color="#2b2b2b") +gg + +gg <- ggplot(df, aes(x, y)) +gg <- gg + geom_ribbon(aes(ymin=ymin, ymax=ymax), + stat="stepribbon", fill="#b2b2b2", + direction="hv") +gg <- gg + geom_step(color="#2b2b2b") +gg +} +\references{ +\url{https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/9cFWHaH1CPs} +} +