Browse Source

implements #20

pull/27/head
Bob Rudis 8 years ago
parent
commit
93a7dd9ed7
  1. 11
      DESCRIPTION
  2. 5
      NAMESPACE
  3. 9
      R/fortify.r
  4. 73
      R/geom_twoway_bar.r
  5. 87
      R/stat-stepribbon.r
  6. 23
      README.Rmd
  7. 35
      README.md
  8. BIN
      README_figs/README-coord_proj-1.png
  9. BIN
      README_figs/README-dumbbell-1.png
  10. BIN
      README_figs/README-encircle-1.png
  11. BIN
      README_figs/README-encircle-2.png
  12. BIN
      README_figs/README-encircle-3.png
  13. BIN
      README_figs/README-encircle-4.png
  14. BIN
      README_figs/README-encircle-5.png
  15. BIN
      README_figs/README-encircle-6.png
  16. BIN
      README_figs/README-encircle-7.png
  17. BIN
      README_figs/README-stepribbon-1.png
  18. BIN
      README_figs/README-stepribbon-2.png
  19. 19
      man/fortify.table.Rd
  20. 5
      man/geom_encircle.Rd
  21. 2
      man/geom_stateface.Rd
  22. 7
      man/ggalt-ggproto.Rd
  23. 76
      man/stat_stepribbon.Rd

11
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 <bob@rudis.net>
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'

5
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)

9
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))
}

73
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)
)
}
)

87
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]
)
}

23
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 <http://www.pokegraphs.com/>)
- `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}

35
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 <http://www.pokegraphs.com/>)
- `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) +
<img src="README_figs/README-encircle-7.png" width="672" />
### 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
```
<img src="README_figs/README-stepribbon-1.png" width="672" />
``` 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
```
<img src="README_figs/README-stepribbon-2.png" width="672" />
### Lollipop charts
``` r

BIN
README_figs/README-coord_proj-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 183 KiB

After

Width:  |  Height:  |  Size: 182 KiB

BIN
README_figs/README-dumbbell-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 121 KiB

BIN
README_figs/README-encircle-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 49 KiB

BIN
README_figs/README-encircle-2.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

BIN
README_figs/README-encircle-3.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

BIN
README_figs/README-encircle-4.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 54 KiB

BIN
README_figs/README-encircle-5.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 47 KiB

BIN
README_figs/README-encircle-6.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

After

Width:  |  Height:  |  Size: 83 KiB

BIN
README_figs/README-encircle-7.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

After

Width:  |  Height:  |  Size: 83 KiB

BIN
README_figs/README-stepribbon-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
README_figs/README-stepribbon-2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

19
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
}

5
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)

2
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.}

7
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}

76
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}
}
Loading…
Cancel
Save