beeswarm/0000755000175100001440000000000012707430622012100 5ustar hornikusersbeeswarm/NAMESPACE0000644000175100001440000000064112567330463013326 0ustar hornikusers export(beeswarm, bxplot, swarmx, swarmy) S3method(beeswarm, default) S3method(beeswarm, formula) S3method(bxplot, default) S3method(bxplot, formula) importFrom("grDevices", "extendrange", "xy.coords") importFrom("graphics", "axis", "box", "boxplot", "par", "plot", "points", "segments", "xinch", "yinch") importFrom("stats", "approx", "density", "na.omit", "quantile", "runif") importFrom("utils", "head", "tail") beeswarm/NEWS0000644000175100001440000000535712707417101012606 0ustar hornikusersbeeswarm NEWS Changes in version 0.2.3 (2016-04-25) - Fixed a bug in which specifying non-default "corral" and "side" arguments would result in unexpected results. (Issue #2) Changes in version 0.2.2 (2015-11-25) - Check that glim, dlim, xlim, ylim have length 2, thus avoiding confusing errors. - New argument "axes" (previously, trying to specify "axes" in ... would cause an error). Changes in version 0.2.1 (2015-08-26) - Corrected NAMESPACE and DESCRIPTION to properly indicate Imports and to remove unnecessary Depends. Changes in version 0.2.0 (2015-05-04) - New arguments "side" and "priority" for beeswarm, swarmx, and swarmy. "side" allows swarms to grow on only one side rather than both sides. "priority" controls the order in which swarms are built, thus affecting the appearance of the plot. Thanks to Jon Lake and Brad Stiritz for suggestions. Changes in version 0.1.8 (2015-04-30) - Added "Depends: stats" to DESCRIPTION and removed "require(stats)" calls from code in order to achieve a clean package check Changes in version 0.1.7 (2014-08-05) - ".calculateSwarm" now performs the swarm point layout more efficiently Changes in version 0.1.6 (2013-09-18) - The argument "labels" now gets recycled. - "labels = NULL" is now the same as missing "labels"; i.e. labels are inferred from data. - There is now a "corralWidth" argument to control the size of corrals, if corrals are used. Changes in version 0.1.5 (2012-05-07) - Minor adjustments. Changes in version 0.1.4 (2012-05-03) - Minor adjustments. Changes in version 0.1.3 (2012-03-22) - New function "bxplot" as a minimal version of "boxplot", intended mainly as a way to add quantile lines to a beeswarm plot. - "beeswarm" has a new argument "corral" to control the spread of swarms into adjacent groups. Changes in version 0.1.2 (2012-03-14) - Documentation fixes - Added a NAMESPACE file - The formula interface now splits as expected for formulas such as: x ~ y * z Changes in version 0.1.1 (2011-08-04) - Documentation fixes. - Adjusted "beeswarm.default" to work when x is a simple numeric vector. - Removed "beeswarm.numeric" . Changes in version 0.1.0 (2011-08-03) - In "beeswarm", defaults for "col", "pch" are now taken from "par", and "bg" defaults to NA, and "pwbg" defaults to NULL. - Renamed "smile" method; now it is called "swarm" - Changed default method to "swarm" - Removed function "smile" - New functions "swarmx" and "swarmy" for easily adding swarm-ed points to a plot. - Fixed bug affecting swarming when par('cex') was not 1. - Fixed bug reversing xlim and ylim on horizontal plots. - Fixed bug causing an error if xlim or ylim were set to NULL - Defaults for xlim and ylim are now NULL instead of missing - New argument 'dlim' - Log scales are now supported. beeswarm/data/0000755000175100001440000000000011453102250012777 5ustar hornikusersbeeswarm/data/breast.RData0000644000175100001440000001411311453074720015206 0ustar hornikusersZ \LiO*M)%IM9gLjRn%&ʥtQDE-!ʥĆBn%벡tAZ*W3kۜy~˙cD[MAAAQAQbOt@ ̈􋜯S)w 1B=@cg^=߲[~+w?ɶ6'<_:sw{>_ͷ['RS-DWQs#ѕCN8exX$k\ȏ[uL?a AS^jτ7edhZ".|4۳Vl֘'ATs+^ts@-ꌻ+x*)@! 9c cAe.}NΆf㇟AY&< ]8 Ú^! ,e#?Jj'2c0Yв':Z.U[bGO rP (8;J>e@lcc=pL:3*@da^5 } )2FD9 zq@VzGY;m_>iM8IpyN`Ʒe@m|wnn.]2Sל^D^cTРcZAxmd?E'xpw=Tw1 Ƚ?v(>A؋nBOϐBo:2S5=DǍt yj'ViY2ʘ!״uJ}f[,jܐ"#.S9 K1FOU,dLj8?<$]g붬E``gxˠwJSYYs:&-7ŷ¸ Lt)g@-~S&J&A/R@U~6 =ao@o3Wp8[9-oJtQyY rkzk?qnpI@5ȟ'+wo#]?ƍ}>[;iN{ ds): L5DIˁj5Q Դ#'[@G} t:ܠb7No~-\9q7o-Uӝâ@?Dg7(i*7@q# 7H1״?rV|wdEĮQ0g =F榀 "#rmFkZdGA/b4PKWa<5RV3(rʵځ_W;a: Q4-s]wx ]Fi:`Β@8 @NwioU dUEy@v]4B#g5 m @y/\%P繍's)~==y0DϗȮ A?Oil}X# (jUhdH)As Aq*.qU J E [5Ձ]5*0ԛ^:Ju0lWf-CI7߶=-\RaIm`Ns npnqDdV JN2\qP=Z<@d>Nڵej PR|ޓ  "rUC{o?۫u^w1"wAdbq5[lz<>]N>sij=Tk@>\ xW.ď"h%r2п`+ur5~-^_kn<`zc .vN&8(sU{\ēg/ײ :Zzr끠y#ꮳ`2[R`_*MA( 5浿,k~Ð ;e)+@<`d>e(簊[@s?q;7*5p=Q-׬ܷ"2ĝ7 l* d:N7 D!r (ӵ UZ< 8i_-OBcԯrڴ @^J|}gEUh 乘ȟ?9VRZ:n+ӣ~{-@r =Yt=]xfUd\_jj{׀^k b j}\ Tu͋(n<7[L.Qہ.@ۙ -aե q| <sݪ~qE-k\uq^gEPΙU𥷤Zܺ=# !״=`E௞}5pw婠k 7vY7*v+?TܴJ@tP rC CG>>&'G_ޮg7)`f..`#E`0!ӋK/<#)_C1*tGD3zyk2aY2<8 ݈YӗQ~#Pc<& Aϗ Q }$c!4c_cnό3}r?c|; a B*[[1zAc$_;7sE~xF36Zas/Gn}c_F-u B/iWfl(Lm ׏Y̻X8)#e|ys16븈<02JCAHDe6eda|as/Ǡ#'2`:F6?3?ә}?111ya|,c";y,c؅## Nl*/a}D(#*+KANήf/`["(2BcS8VV%VY#(VVV%N`'K0XczYc2[c'[c'['c boedl&Kl$XHJ,^`A*TRL 8,p$XH&`zcQ" cE57,n$XH`q#aG ;(nx`l1^ m1m1|bܥw)G#bH1kHy#0e2L@& GKslJc+cEch,;vv,UYvfq%lņM`vRqصbvqaW ;8ذN-:;8Xjbe-~.e4UvH!zԒRllbcIb#RH+:+VaXbmMR|HIJk>RH#Śk>R,+*)BKiXb.)L^vaXkTه`\52 eX8ʰpa(Â2l@a% 2lԐaeQɇ_V.jlc&Ú KD/2˰.G&Ŭ7֑e쎌(1%4(11E>.úƘcza`hD4Øc'o%h쉂{ȥBnAcOh[X'IQƒhL`XR삍(X{Xf"Y[4V.h\XϣB;`J 54 @cEhXaj4V@hvఫ4V.h8v졖-v6Ja֗Ƥc0La;[!qulW4beeswarm/R/0000755000175100001440000000000011751707462012310 5ustar hornikusersbeeswarm/R/beeswarm.R0000644000175100001440000003471412707421242014240 0ustar hornikusers# beeswarm.R # # Aron Charles Eklund # # A part of the "beeswarm" R package # beeswarm <- function (x, ...) UseMethod("beeswarm") ## here x should be a list or data.frame or numeric beeswarm.default <- function(x, method = c("swarm", "center", "hex", "square"), vertical = TRUE, horizontal = !vertical, cex = 1, spacing = 1, breaks = NULL, labels, at = NULL, corral = c("none", "gutter", "wrap", "random", "omit"), corralWidth, side = 0L, priority = c("ascending", "descending", "density", "random", "none"), pch = par("pch"), col = par("col"), bg = NA, pwpch = NULL, pwcol = NULL, pwbg = NULL, do.plot = TRUE, add = FALSE, axes = TRUE, log = FALSE, xlim = NULL, ylim = NULL, dlim = NULL, glim = NULL, xlab = NULL, ylab = NULL, dlab = "", glab = "", ...) { method <- match.arg(method) corral <- match.arg(corral) priority <- match.arg(priority) if(length(cex) > 1) { stop('the parameter "cex" must have length 1') } stopifnot(side %in% -1:1) if(is.numeric(x)) { x <- list(x) } n.groups <- length(x) #### Resolve group labels if(missing(labels) || is.null(labels)) { if(is.null(names(x))) { if(n.groups == 1) { labels <- NA } else { labels <- 1:n.groups } } else { labels <- names(x) } } else { labels <- rep(labels, length.out = n.groups) } if (is.null(at)) at <- 1:n.groups else if (length(at) != n.groups) stop(gettextf("'at' must have length equal to %d, the number of groups", n.groups), domain = NA) if (is.null(dlab)) dlab <- deparse(substitute(x)) ## this function returns a "group" vector, to complement "unlist" unlistGroup <- function(x, nms = names(x)) rep(nms, sapply(x, length)) x.val <- unlist(x) x.gp <- unlistGroup(x, nms = labels) if((range(x.val, finite = TRUE)[1] <= 0) && log) warning('values <= 0 omitted from logarithmic plot') n.obs <- length(x.val) n.obs.per.group <- sapply(x, length) #### Resolve xlim, ylim, dlim, xlab, ylab if(is.null(dlim)) { if(log) { dlim <- 10 ^ (extendrange(log10(x.val[x.val > 0]))) } else { dlim <- extendrange(x.val, f = 0.01) } } else if (length(dlim) != 2) { stop ("'dlim' must have length 2") } if(is.null(glim)) { glim <- c(min(at) - 0.5, max(at) + 0.5) } else if (length(glim) != 2) { stop ("'glim' must have length 2") } if(horizontal) { ## plot is horizontal if(is.null(ylim)) ylim <- glim if(is.null(xlim)) { xlim <- dlim } else { dlim <- xlim } if (is.null(xlab)) xlab <- dlab if (is.null(ylab)) ylab <- glab } else { ## plot is vertical if(is.null(xlim)) xlim <- glim if(is.null(ylim)) { ylim <- dlim } else { dlim <- ylim } if (is.null(ylab)) ylab <- dlab if (is.null(xlab)) xlab <- glab } if(length(xlim) != 2) stop ("'xlim' must have length 2") if(length(ylim) != 2) stop ("'ylim' must have length 2") #### Resolve plotting characters and colors if(is.null(pwpch)) { pch.out <- unlistGroup(x, nms = rep(pch, length.out = n.groups)) } else { if(is.list(pwpch)) { names(pwpch) <- names(x) stopifnot(all(sapply(pwpch, length) == n.obs.per.group)) pch.out <- unlist(pwpch) } else { pch.out <- pwpch } } stopifnot(length(pch.out) == n.obs) if(is.null(pwcol)) { col.out <- unlistGroup(x, nms = rep(col, length.out = n.groups)) } else { if(is.list(pwcol)) { names(pwcol) <- names(x) stopifnot(all(sapply(pwcol, length) == n.obs.per.group)) col.out <- unlist(pwcol) } else { col.out <- pwcol } } stopifnot(length(col.out) == n.obs) if(is.null(pwbg)) { bg.out <- unlistGroup(x, nms = rep(bg, length.out = n.groups)) } else { if(is.list(pwbg)) { names(pwbg) <- names(x) stopifnot(all(sapply(pwbg, length) == n.obs.per.group)) bg.out <- unlist(pwbg) } else { bg.out <- pwbg } } stopifnot(length(bg.out) == n.obs) #### Set up the plot if(do.plot & !add) { plot(xlim, ylim, type = 'n', axes = FALSE, log = ifelse(log, ifelse(horizontal, 'x', 'y'), ''), xlab = xlab, ylab = ylab, ...) } #### Calculate the size of a plotting character along group- or data-axis sizeMultiplier <- par('cex') * cex * spacing if(horizontal) { size.g <- yinch(0.08, warn.log = FALSE) * sizeMultiplier size.d <- xinch(0.08, warn.log = FALSE) * sizeMultiplier } else { # vertical size.g <- xinch(0.08, warn.log = FALSE) * sizeMultiplier size.d <- yinch(0.08, warn.log = FALSE) * sizeMultiplier } ##### Calculate point positions g.pos, d.pos if(method == 'swarm') { if(horizontal) { g.offset <- lapply(x, function(a) swarmy(x = a, y = rep(0, length(a)), cex = sizeMultiplier, side = side, priority = priority)$y) } else { g.offset <- lapply(x, function(a) swarmx(x = rep(0, length(a)), y = a, cex = sizeMultiplier, side = side, priority = priority)$x) } d.pos <- x } else { #### non-swarm methods ##### first determine positions along the data axis if(method == 'hex') size.d <- size.d * sqrt(3) / 2 if(log) { ## if data axis IS on a log scale if(is.null(breaks)) breaks <- 10 ^ seq(log10(dlim[1]), log10(dlim[2]) + size.d, by = size.d) if(length(breaks) == 1 && is.na(breaks[1])) { d.index <- x d.pos <- x } else { mids <- 10 ^ ((log10(head(breaks, -1)) + log10(tail(breaks, -1))) / 2) d.index <- lapply(x, cut, breaks = breaks, labels = FALSE) d.pos <- lapply(d.index, function(a) mids[a]) } } else { ## if data axis is NOT on a log scale if(is.null(breaks)) breaks <- seq(dlim[1], dlim[2] + size.d, by = size.d) if(length(breaks) == 1 && is.na(breaks[1])) { d.index <- x d.pos <- x } else { mids <- (head(breaks, -1) + tail(breaks, -1)) / 2 d.index <- lapply(x, cut, breaks = breaks, labels = FALSE) d.pos <- lapply(d.index, function(a) mids[a]) } } ##### now determine positions along the group axis x.index <- lapply(d.index, function(v) { if(length(na.omit(v)) == 0) return(v) v.s <- lapply(split(v, v), seq_along) if(method %in% c('center', 'square') && side == -1) v.s <- lapply(v.s, function(a) a - max(a)) else if(method %in% c('center', 'square') && side == 1) v.s <- lapply(v.s, function(a) a - 1) else if(method == 'center') v.s <- lapply(v.s, function(a) a - mean(a)) else if(method == 'square') v.s <- lapply(v.s, function(a) a - floor(mean(a))) else if(method == 'hex') { odd.row <- (as.numeric(names(v.s)) %% 2) == 1 if(side == 0) { v.s[ odd.row] <- lapply(v.s[ odd.row], function(a) a - floor(mean(a)) - 0.25) v.s[!odd.row] <- lapply(v.s[!odd.row], function(a) a - ceiling(mean(a)) + 0.25) } else if(side == -1) { v.s[ odd.row] <- lapply(v.s[ odd.row], function(a) a - max(a)) v.s[!odd.row] <- lapply(v.s[!odd.row], function(a) a - max(a) - 0.5) } else if(side == 1) { v.s[ odd.row] <- lapply(v.s[ odd.row], function(a) a - 1) v.s[!odd.row] <- lapply(v.s[!odd.row], function(a) a - 0.5) } } unsplit(v.s, v) }) g.offset <- lapply(1:n.groups, function(i) x.index[[i]] * size.g) } ###### end of non-swarm methods ##### now check for runaway points (if "corral" has been set) if(corral != 'none') { if(missing(corralWidth)) { if(n.groups > 1) { corralWidth <- min(at[-1] - at[-n.groups]) - (2 * size.g) } else { corralWidth <- 2 * (min(diff(c(par('usr')[1], at, par('usr')[2]))) - size.g) } } else { stopifnot(length(corralWidth) == 1) stopifnot(corralWidth > 0) } corralLo <- (side - 1) * corralWidth / 2 corralHi <- (side + 1) * corralWidth / 2 if(corral == 'gutter') { g.offset <- lapply(g.offset, function(zz) pmin(corralHi, pmax(corralLo, zz))) } if(corral == 'wrap') { if(side == -1) { ## special case with side=-1: reverse the corral to avoid artifacts at zero g.offset <- lapply(g.offset, function(zz) corralHi - ((corralHi - zz) %% corralWidth)) } else { g.offset <- lapply(g.offset, function(zz) ((zz - corralLo) %% corralWidth) + corralLo) } } if(corral == 'random') { g.offset <- lapply(g.offset, function(zz) ifelse(zz > corralHi | zz < corralLo, yes = runif(length(zz), corralLo, corralHi), no = zz)) } if(corral == 'omit') { g.offset <- lapply(g.offset, function(zz) ifelse(zz > corralHi | zz < corralLo, yes = NA, no = zz)) } } g.pos <- lapply(1:n.groups, function(i) at[i] + g.offset[[i]]) out <- data.frame(x = unlist(g.pos), y = unlist(d.pos), pch = pch.out, col = col.out, bg = bg.out, x.orig = x.gp, y.orig = x.val, stringsAsFactors = FALSE) if(do.plot) { if(horizontal) { ## plot is horizontal points(out$y, out$x, pch = out$pch, col = out$col, bg = out$bg, cex = cex) if(axes & !add) { axis(1, ...) axis(2, at = at, labels = labels, tick = FALSE, ...) box(...) } } else { ## plot is vertical points(out$x, out$y, pch = out$pch, col = out$col, bg = out$bg, cex = cex) if(axes & !add) { axis(2, ...) axis(1, at = at, labels = labels, tick = FALSE, ...) box(...) } } } invisible(out) } beeswarm.formula <- function (formula, data = NULL, subset, na.action = NULL, pwpch = NULL, pwcol = NULL, pwbg = NULL, dlab, glab, ...) { if (missing(formula) || (length(formula) != 3)) stop("'formula' missing or incorrect") m <- match.call(expand.dots = FALSE) if (is.matrix(eval(m$data, parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m$dlab <- NULL m$glab <- NULL m$na.action <- na.action m[[1]] <- as.name("model.frame") mf <- eval(m, parent.frame()) response <- attr(attr(mf, "terms"), "response") if (missing(dlab)) dlab <- names(mf)[response] if (missing(glab)) glab <- as.character(formula)[3] f <- mf[-response] f <- f[names(f) %in% attr(attr(mf, "terms"), "term.labels")] if(!is.null(mf$'(pwpch)')) pwpch <- split(mf$'(pwpch)', f) if(!is.null(mf$'(pwcol)')) pwcol <- split(mf$'(pwcol)', f) if(!is.null(mf$'(pwbg)')) pwbg <- split(mf$'(pwbg)',f) beeswarm(split(mf[[response]], f), pwpch = pwpch, pwcol = pwcol, pwbg = pwbg, dlab = dlab, glab = glab, ...) } #### hidden function to do swarm layout .calculateSwarm <- function(x, dsize, gsize, side = 0L, priority = "ascending") { if(length(x) == 0) return(numeric(0)) stopifnot(side %in% -1:1) out <- data.frame(x = x / dsize, y = 0, index = seq(along = x)) #### Determine the order in which points will be placed if( priority == "ascending" ) { out <- out[order( out$x), ] } ## default "smile" else if(priority == "descending") { out <- out[order(-out$x), ] } ## frown else if(priority == "none") { } ## do not reorder else if(priority == "density") { dens.x <- density(out$x, na.rm = TRUE) ## compute kernel density estimate dens.interp <- approx(dens.x$x, dens.x$y, xout = out$x, rule = 2) ## interpolated density out <- out[order(-dens.interp$y), ] ## arrange outward from densest areas } else if(priority == "random") { out <- out[sample(nrow(out)), ] } #### place the points if(nrow(out) > 1) { for (ii in 2:nrow(out)) { ## we will place one point at a time xi <- out$x[ii] ## identify previously-placed points with potential to overlap the current point isPotOverlap <- (abs(xi - out$x) < 1) & (1:nrow(out) < ii) isPotOverlap[is.na(isPotOverlap)] <- FALSE if(any(isPotOverlap)) { pre.x <- out[isPotOverlap, 'x'] pre.y <- out[isPotOverlap, 'y'] poty.off <- sqrt(1 - ((xi - pre.x) ^ 2)) ## potential y offsets poty <- switch(side + 2, c(0, pre.y - poty.off), c(0, pre.y + poty.off, pre.y - poty.off), c(0, pre.y + poty.off) ) poty.bad <- sapply(poty, function(y) { ## check for overlaps any(((xi - pre.x) ^ 2 + (y - pre.y) ^ 2) < 0.999) }) poty[poty.bad] <- Inf out$y[ii] <- poty[which.min(abs(poty))] } else { out$y[ii] <- 0 } } } out[is.na(out$x), 'y'] <- NA ## missing x values should have missing y values out$y[order(out$index)] * gsize } ### jitter points horizontally swarmx <- function(x, y, xsize = xinch(0.08, warn.log = FALSE), ysize = yinch(0.08, warn.log = FALSE), log = NULL, cex = par("cex"), side = 0L, priority = c("ascending", "descending", "density", "random", "none")) { priority <- match.arg(priority) if(is.null(log)) log <- paste(ifelse(par('xlog'), 'x', ''), ifelse(par('ylog'), 'y', ''), sep = '') xlog <- 'x' %in% strsplit(log, NULL)[[1L]] ylog <- 'y' %in% strsplit(log, NULL)[[1L]] xy <- xy.coords(x = x, y = y, recycle = TRUE, log = log) stopifnot((length(unique(xy$x)) <= 1)) if(xlog) xy$x <- log10(xy$x) if(ylog) xy$y <- log10(xy$y) x.new <- xy$x + .calculateSwarm(xy$y, dsize = ysize * cex, gsize = xsize * cex, side = side, priority = priority) out <- data.frame(x = x.new, y = y) if(xlog) out$x <- 10 ^ out$x out } ### jitter points vertically swarmy <- function(x, y, xsize = xinch(0.08, warn.log = FALSE), ysize = yinch(0.08, warn.log = FALSE), log = NULL, cex = par("cex"), side = 0L, priority = c("ascending", "descending", "density", "random", "none")) { priority <- match.arg(priority) if(is.null(log)) log <- paste(ifelse(par('xlog'), 'x', ''), ifelse(par('ylog'), 'y', ''), sep = '') xlog <- 'x' %in% strsplit(log, NULL)[[1L]] ylog <- 'y' %in% strsplit(log, NULL)[[1L]] xy <- xy.coords(x = x, y = y, recycle = TRUE, log = log) stopifnot((length(unique(xy$y)) <= 1)) if(xlog) xy$x <- log10(xy$x) if(ylog) xy$y <- log10(xy$y) y.new <- xy$y + .calculateSwarm(xy$x, dsize = xsize * cex, gsize = ysize * cex, side = side, priority = priority) out <- data.frame(x = x, y = y.new) if(ylog) out$y <- 10 ^ out$y out } beeswarm/R/bxplot.R0000644000175100001440000000327612520411462013736 0ustar hornikusers# bxplot.R # # Aron Charles Eklund ## # A part of the "beeswarm" R package # bxplot <- function (x, ...) UseMethod("bxplot") bxplot.default <- function(x, probs = c(0.25, 0.5, 0.75), vertical = TRUE, horizontal = !vertical, add = FALSE, col = par("col"), lty = par("lty"), lwd = NULL, at = NULL, width = 0.75, ...) { if(is.numeric(x)) { x <- list(x) } n <- length(x) n.probs <- length(probs) if(is.null(lwd)) { ## default is a thick line at the median lwd <- rep(par('lwd'), length.out = n.probs) if(0.5 %in% probs) lwd[probs == 0.5] <- par('lwd') * 3 } y <- lapply(x, quantile, probs = probs, na.rm = TRUE) if(is.null(at)) at <- 1:n if(!add) { boxplot(x, horizontal = horizontal, at = at, pars = list(whisklty = 0, staplelty = 0, outpch = NA, boxlty = 0, medlty = 0), ...) } hw <- width / 2 # half-width if(horizontal) { for (i in 1:n) { segments(y0 = at[i] - hw, y1 = at[i] + hw, x0 = y[[i]], col = col, lwd = lwd, lty = lty) } } else { for (i in 1:n) { segments(x0 = at[i] - hw, x1 = at[i] + hw, y0 = y[[i]], col = col, lwd = lwd, lty = lty) } } } bxplot.formula <- function (formula, data = NULL, ..., subset, na.action = NULL) { if (missing(formula) || (length(formula) != 3L)) stop("'formula' missing or incorrect") m <- match.call(expand.dots = FALSE) if (is.matrix(eval(m$data, parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m$na.action <- na.action m[[1L]] <- as.name("model.frame") mf <- eval(m, parent.frame()) response <- attr(attr(mf, "terms"), "response") bxplot(split(mf[[response]], mf[-response]), ...) } beeswarm/R/zzz.R0000644000175100001440000000002411750464500013254 0ustar hornikusers.noGenerics <- TRUE beeswarm/README.md0000644000175100001440000000057412556633734013400 0ustar hornikusersbeeswarm ======== An R package implementing bee swarm plots You can see some examples here: http://www.cbs.dtu.dk/~eklund/beeswarm/ Installation ------------ You can install the latest release on CRAN like this: install.packages("beeswarm") You can install the latest development version from GitHub like this: library(devtools) install_github("aroneklund/beeswarm") beeswarm/MD50000644000175100001440000000105112707430622012405 0ustar hornikusers72e898030870647e36a4072f2b3bf141 *DESCRIPTION eb4e245537123b9485b4e0b3d00f4f61 *NAMESPACE 0f1a8c25e6076cf4bec98ef778c7c46a *NEWS af10ea827c803e47a58f45504ee36349 *R/beeswarm.R 99b47bff686cbb9afaf34e18a74ce49d *R/bxplot.R 16cc12d2742742718c88949f18d5ac5d *R/zzz.R 1a19e283dbfe0c6d3d8db1e0d21db033 *README.md bcf102fab14afb8d779c8c4148685174 *data/breast.RData 346458956b02b410920368f5c5b7e14e *man/beeswarm.Rd 1ee38a2972fa0874a81c807e36dd22a6 *man/breast.Rd d7097bb52220995656e837936b892d12 *man/bxplot.Rd 39895501e8f57e66d39377f43cf0eae7 *man/swarmx.Rd beeswarm/DESCRIPTION0000644000175100001440000000102312707430622013602 0ustar hornikusersPackage: beeswarm Version: 0.2.3 Date: 2016-04-25 Title: The Bee Swarm Plot, an Alternative to Stripchart Author: Aron Eklund Maintainer: Aron Eklund Imports: stats, graphics, grDevices, utils Description: The bee swarm plot is a one-dimensional scatter plot like "stripchart", but with closely-packed, non-overlapping points. License: Artistic-2.0 URL: http://www.cbs.dtu.dk/~eklund/beeswarm/ NeedsCompilation: no Packaged: 2016-04-25 13:59:08 UTC; aron Repository: CRAN Date/Publication: 2016-04-25 17:02:42 beeswarm/man/0000755000175100001440000000000011751713176012661 5ustar hornikusersbeeswarm/man/swarmx.Rd0000644000175100001440000000616212521634330014464 0ustar hornikusers\name{swarmx} \alias{swarmx} \alias{swarmy} \title{ Adjust 1-d data to separate coincident points } \description{Take a series of points lying in a horizontal or vertical line, and jitter them in the other dimension such that no points are overlapping. } \usage{ swarmx(x, y, xsize = xinch(0.08, warn.log = FALSE), ysize = yinch(0.08, warn.log = FALSE), log = NULL, cex = par("cex"), side = 0L, priority = c("ascending", "descending", "density", "random", "none")) swarmy(x, y, xsize = xinch(0.08, warn.log = FALSE), ysize = yinch(0.08, warn.log = FALSE), log = NULL, cex = par("cex"), side = 0L, priority = c("ascending", "descending", "density", "random", "none")) } \arguments{ \item{x, y}{ Coordinate vectors in any format supported by \code{\link{xy.coords}}. } \item{xsize, ysize}{ Width and height of the plotting character in user coordinates. } \item{log}{ Character string indicating which axes are logarithmic, as in \code{\link{plot.default}}, or \code{NULL} to figure it out automatically.} \item{cex}{ Relative plotting character size.} \item{side}{ Direction to perform jittering: 0: both directions; 1: to the right or upwards; -1: to the left or downwards.} \item{priority}{ Method used to perform point layout (see below).} } \details{ For \code{swarmx}, the input coordinates must lie in a vertical line. For \code{swarmy}, the input coordinates must lie in a horizontal line. \code{swarmx} adjusts coordinates to the left or right; \code{swarmy} adjusts coordinates up or down. \code{priority} controls the order in which the points are placed; this has generally has a noticeable effect on the resulting appearance. \code{"ascending"} gives the "traditional" beeswarm plot in which the points are placed in an ascending order. \code{"descending"} is the opposite. \code{"density"} prioritizes points with higher local density. \code{"random"} places points in a random order. \code{"none"} places points in the order provided. Usually it makes sense to call this function after a plotting device has already been set up (e.g. when adding points to an existing plot), so that the default values for \code{xsize}, \code{ysize}, and \code{log} will be appropriate. } \value{ A data frame with columns \code{x} and \code{y} with the new coordinates. } \seealso{ \code{\link{beeswarm}}, \code{\link{jitter}} } \examples{ ## Plot points in one dimension index <- rep(0, 100) values <- rnorm(100) plot(index, values, xlim = c(-0.5, 2.5)) points(swarmx(index + 1, values), col = 2) points(swarmx(index + 2, values, cex = 1.5), col = 3, cex = 1.5) ## Try the horizontal direction, with a log scale plot(values, index, log = "x", ylim = c(-1, 2)) points(swarmy(values, index + 1), col = 2) ## Newer examples using "side" and "priority" plot(c(-0.5, 3.5), range(values), type = 'n') points(swarmx(index + 0, values), col = 1) points(swarmx(index + 0.9, values, side = -1), col = 2) points(swarmx(index + 1.1, values, side = 1, priority = "descending"), col = 3) points(swarmx(index + 2 , values, priority = 'density'), col = 4) points(swarmx(index + 3 , values, priority = 'random'), col = 5) } \keyword{ dplot } beeswarm/man/breast.Rd0000644000175100001440000000242411551570002014415 0ustar hornikusers\name{breast} \alias{breast} \docType{data} \title{ Lymph-node-negative primary breast tumors } \description{ Tumor molecular measurements and outcome from breast cancer patients. } \usage{data(breast)} \format{ A data frame with 286 observations on the following 5 variables. \describe{ \item{\code{ER}}{Estrogen receptor status (factor with levels \code{neg}, \code{pos})} \item{\code{ESR1}}{Expression of the ESR1 gene (numeric)} \item{\code{ERBB2}}{Expression of the ERBB2 gene (numeric)} \item{\code{time_survival}}{Time in months (numeric)} \item{\code{event_survival}}{Coded event: 0 = censored, 1 = metastasis (numeric)} } } \details{ ER, ESR1, and ERBB2 were measured on a tumor specimen taken at surgery (time = 0). ESR1 and ERBB2 expression values were determined by microarray probe sets 205225_at and 216836_s_at using RMA-normalized data. } \source{ Wang Y, Klijn JG, Zhang Y, Sieuwerts AM, Look MP, Yang F, Talantov D, Timmermans M, Meijer-van Gelder ME, Yu J, Jatkoe T, Berns EM, Atkins D, Foekens JA. Gene-expression profiles to predict distant metastasis of lymph-node-negative primary breast cancer. Lancet. 2005 Feb 19-25;365(9460):671-9. } \examples{ data(breast) with(breast, plot(ESR1, ERBB2, col = as.numeric(ER)) ) } \keyword{datasets} beeswarm/man/beeswarm.Rd0000644000175100001440000002426312657053561014765 0ustar hornikusers\name{beeswarm} \alias{beeswarm} \alias{beeswarm.default} \alias{beeswarm.formula} \title{Bee swarm plot} \description{ Create a bee swarm plot. A bee swarm plot is a one-dimensional scatter plot similar to \code{\link{stripchart}}, but with various methods to separate coincident points such that each point is visible. Also, \code{beeswarm} introduces additional features unavailable in \code{stripchart}, such as the ability to control the color and plotting character of each point. } \usage{ beeswarm(x, \dots) \method{beeswarm}{formula}(formula, data = NULL, subset, na.action = NULL, pwpch = NULL, pwcol = NULL, pwbg = NULL, dlab, glab, \dots) \method{beeswarm}{default}(x, method = c("swarm", "center", "hex", "square"), vertical = TRUE, horizontal = !vertical, cex = 1, spacing = 1, breaks = NULL, labels, at = NULL, corral = c("none", "gutter", "wrap", "random", "omit"), corralWidth, side = 0L, priority = c("ascending", "descending", "density", "random", "none"), pch = par("pch"), col = par("col"), bg = NA, pwpch = NULL, pwcol = NULL, pwbg = NULL, do.plot = TRUE, add = FALSE, axes = TRUE, log = FALSE, xlim = NULL, ylim = NULL, dlim = NULL, glim = NULL, xlab = NULL, ylab = NULL, dlab = "", glab = "", \dots) } \arguments{ \item{formula}{A formula, such as \code{y ~ grp}, where \code{y} is a numeric vector of data values to be split into groups according to the grouping variable \code{grp} (usually a factor).} \item{data}{A data.frame (or list) from which the variables in \code{formula} should be taken.} \item{subset}{An optional vector specifying a subset of observations to be used.} \item{na.action}{A function which indicates what should happen when the data contain \code{NA}s. The default is to quietly ignore missing values in either the response or the group.} \item{x}{ A numeric vector, or a data frame or list of numeric vectors, each of which is plotted as an individual swarm.} \item{method}{ Method for arranging points (see Details). } \item{vertical, horizontal}{ Orientation of the plot. \code{horizontal} takes precedence if both are specified. } \item{cex}{ Size of points relative to the default given by \code{par("cex")}. Unlike other plotting functions, this must be a single value.} \item{spacing}{ Relative spacing between points.} \item{breaks}{ Breakpoints (optional). If \code{NULL}, breakpoints are chosen automatically. If \code{NA}, bins are not used (similar to \code{stripchart} with \code{method = "stack"}).} \item{labels}{ Labels for each group. Recycled if necessary. By default, these are inferred from the data. } \item{at}{ Numeric vector giving the locations where the swarms should be drawn; defaults to \code{1:n} where \var{n} is the number of groups. } \item{corral}{ Method to adjust points that would be placed outside their own group region (see Details). } \item{corralWidth}{ Width of the "corral" in user coordinates. If missing, a sensible value will be chosen. } \item{side}{ Direction to perform jittering: 0: both directions; 1: to the right or upwards; -1: to the left or downwards.} \item{priority}{ Order used to perform point layout when method is \code{"swarm"}; ignored otherwise (see Details).} \item{pch, col, bg}{ Plotting characters and colors, specified by group. Recycled if necessary (see Details). } \item{pwpch, pwcol, pwbg}{ \dQuote{Point-wise} plotting characters and colors, specified for each data point (see Details). } \item{do.plot}{ Draw a plot? } \item{add}{ Add to an existing plot? } \item{axes}{ Draw axes and box? } \item{log}{ Use a logarithmic scale on the data axis? } \item{xlim, ylim}{ Limits of the plot. } \item{dlim, glim}{ An alternative way to specify limits (see Details). } \item{xlab, ylab}{ Axis labels. } \item{dlab, glab}{ An alternative way to specify axis labels (see Details). } \item{\dots}{ Further arguments passed to \code{\link{plot}}. } } \details{ Several methods for placing the points are available; each method uses a different algorithm to avoid overlapping points. The default method, \code{swarm}, places points in increasing order. If a point would overlap an existing point, it is shifted sideways (along the group axis) by a minimal amount sufficient to avoid overlap. \code{breaks} is ignored. The other three methods first discretize the values along the data axis, in order to create more efficient packing: \code{square} places the points on a square grid, whereas \code{hex} uses a hexagonal grid. \code{center} uses a square grid to produce a symmetric swarm. By default, the number of breakpoints for discretization is determined by a combination of the available plotting area and the plotting character size. The discretization of the data can be explicitly controlled using \code{breaks}. If \code{breaks} is set to \code{NA}, the data will not be grouped into intervals; this may be a sensible option if the data is already discrete. In contrast to most other plotting functions, changing the size of the graphics device will often change the position of the points. The plotting characters and colors can be controlled in two ways. First, the arguments \code{pch}, \code{col} and \code{bg} can specify plotting characters and colors in the same way as \code{\link{stripchart}} and \code{\link{boxplot}}: in short, the arguments apply to each group as a whole (and are recycled if necessary). Alternatively, the \dQuote{point-wise} characteristics of each individual data point can be controlled using \code{pwpch}, \code{pwcol}, and \code{pwbg}, which override \code{pch}, \code{col} and \code{bg} if these are also specified. These arguments can be specified as a list or vector. If supplied using the formula method, the arguments can be specified as part of the formula interface; i.e. they are affected by \code{data} and \code{subset}. The \code{dlab} and \code{glab} labels may be used instead of \code{xlab} and \code{ylab} if those are not specified. \code{dlab} applies to the continuous data axis (the Y axis unless \code{horizontal} is \code{TRUE}); \code{glab} to the group axis. Likewise, \code{dlim} and \code{glim} can be used to specify limits of the axes instead of \code{xlim} or \code{ylim}. This function is intended to be mostly compatible with calls to \code{\link{stripchart}} or \code{\link{boxplot}}. Thus, code that works with these functions should work with \code{beeswarm} with minimal modification. By default, swarms from different groups are not prevented from overlapping. Thus, large data sets, or data sets with uneven distributions, may produce somewhat unpleasing beeswarms. If this is a problem, consider reducing \code{cex}. Another approach is to control runaway points (those that would be plotted outside a region allotted to each group) with the \code{corral} argument: The default, \code{"none"}, does not control runaway points. \code{"gutter"} collects runaway points along the boundary between groups. \code{"wrap"} implements periodic boundaries. \code{"random"} places runaway points randomly in the region. \code{"omit"} omits runaway points. See Examples below. When using the \code{"swarm"} method, \code{priority} controls the order in which the points are placed; this generally has a noticeable effect on the resulting appearance. \code{"ascending"} gives the "traditional" beeswarm plot in which the points are placed in an ascending order. \code{"descending"} is the opposite. \code{"density"} prioritizes points with higher local density. \code{"random"} places points in a random order. \code{"none"} places points in the order provided. } \value{ A data frame with plotting information, invisibly. } \seealso{ \code{\link{stripchart}}, \code{\link{boxplot}} } \examples{ ## One of the examples from 'stripchart' beeswarm(decrease ~ treatment, data = OrchardSprays, log = TRUE, pch = 16, col = rainbow(8)) ## One of the examples from 'boxplot', with a beeswarm overlay boxplot(len ~ dose, data = ToothGrowth, main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "Tooth length") beeswarm(len ~ dose, data = ToothGrowth, col = 2, add = TRUE) ## Compare the 4 methods op <- par(mfrow = c(2,2)) for (m in c("swarm", "center", "hex", "square")) { beeswarm(len ~ dose, data = ToothGrowth, method = m, main = m) } par(op) ## Demonstrate the use of 'pwcol' data(breast) beeswarm(time_survival ~ ER, data = breast, pch = 16, pwcol = 1 + as.numeric(event_survival), xlab = "", ylab = "Follow-up time (months)", labels = c("ER neg", "ER pos")) legend("topright", legend = c("Yes", "No"), title = "Censored", pch = 16, col = 1:2) ## The list interface distributions <- list(runif = runif(200, min = -3, max = 3), rnorm = rnorm(200), rlnorm = rlnorm(200, sdlog = 0.5)) beeswarm(distributions, col = 2:4) ## Demonstrate 'pwcol' with the list interface myCol <- lapply(distributions, function(x) cut(x, breaks = quantile(x), labels = FALSE)) beeswarm(distributions, pch = 16, pwcol = myCol) legend("bottomright", legend = 1:4, pch = 16, col = 1:4, title = "Quartile") ## Demonstrate the 'corral' methods par(mfrow = c(2,3)) beeswarm(distributions, col = 2:4, main = 'corral = "none" (default)') beeswarm(distributions, col = 2:4, corral = "gutter", main = 'corral = "gutter"') beeswarm(distributions, col = 2:4, corral = "wrap", main = 'corral = "wrap"') beeswarm(distributions, col = 2:4, corral = "random", main = 'corral = "random"') beeswarm(distributions, col = 2:4, corral = "omit", main = 'corral = "omit"') ## Demonstrate 'side' and 'priority' par(mfrow = c(2,3)) beeswarm(distributions, col = 2:4, main = 'Default') beeswarm(distributions, col = 2:4, side = -1, main = 'side = -1') beeswarm(distributions, col = 2:4, side = 1, main = 'side = 1') beeswarm(distributions, col = 2:4, priority = "descending", main = 'priority = "descending"') beeswarm(distributions, col = 2:4, priority = "random", main = 'priority = "random"') beeswarm(distributions, col = 2:4, priority = "density", main = 'priority = "density"') } \keyword{ hplot } beeswarm/man/bxplot.Rd0000644000175100001440000000626311751707710014464 0ustar hornikusers\name{bxplot} \alias{bxplot} \alias{bxplot.default} \alias{bxplot.formula} \title{Plot quantile lines} \description{ Plot lines indicating the specified quantiles for each group. This function is intended as a simplified interpretation of \code{\link{boxplot}}, which can be combined with a \code{\link{beeswarm}} (or \code{\link{stripchart}}) plot. } \usage{ bxplot(x, \dots) \method{bxplot}{formula}(formula, data = NULL, \dots, subset, na.action = NULL) \method{bxplot}{default}(x, probs = c(0.25, 0.5, 0.75), vertical = TRUE, horizontal = !vertical, add = FALSE, col = par("col"), lty = par("lty"), lwd = NULL, at = NULL, width = 0.75, \dots) } \arguments{ \item{formula}{A formula, such as \code{y ~ grp}, where \code{y} is a numeric vector of data values to be split into groups according to the grouping variable \code{grp} (usually a factor).} \item{data}{A data.frame (or list) from which the variables in \code{formula} should be taken.} \item{subset}{An optional vector specifying a subset of observations to be used.} \item{na.action}{A function which indicates what should happen when the data contain \code{NA}s. The default is to quietly ignore missing values in either the response or the group.} \item{x}{A numeric vector, or a data frame or list of numeric vectors, each of which is considered as a group.} \item{probs}{A numeric vector of probabilities with values in [0,1]} \item{vertical, horizontal}{ Orientation of the plot. \code{horizontal} takes precedence if both are specified. } \item{add}{Add to an existing plot?} \item{col, lty}{Color and line type for each probability.} \item{lwd}{Line width for each probability (see below).} \item{at}{Numeric vector giving the locations where the swarms should be drawn; defaults to \code{1:n} where \var{n} is the number of groups.} \item{width}{Width of the lines.} \item{\dots}{Further arguments passed to \code{\link{boxplot}}.} } \details{ This function is intended as a minimalistic interpration of \code{\link{boxplot}}; however, the quantiles plotted by \code{bxplot} are not necessarily the same as the hinges plotted by a \code{boxplot}. Notice that specifying a vector of graphical parameters such as \code{lwd} or \code{col} will refer to each of \code{probs}, \emph{not} to each group in the data (as one might expect by analogy with \code{boxplot}). If \code{lwd} is \code{NULL}, and if the \code{probs} includes 0.5, \code{lwd} will be set to 3 times \code{par("lwd")} for probs=0.5, and \code{par("lwd")} for the others. (Thus something resembling the median line and hinges of a boxplot is produced by default.) } \value{ None.} \examples{ ## bxplot on bottom beeswarm(len ~ dose, data = ToothGrowth) bxplot(len ~ dose, data = ToothGrowth, add = TRUE) ## bxplot on top bxplot(decrease ~ treatment, data = OrchardSprays, probs = 0.5, col = 2) beeswarm(decrease ~ treatment, data = OrchardSprays, add = TRUE) ## Show deciles data(breast) bxplot(time_survival ~ event_survival, data = breast, probs = seq(0, 1, by = 0.1), col = rainbow(10)) beeswarm(time_survival ~ event_survival, data = breast, pch = 21, bg = "gray75", add = TRUE) } \keyword{ hplot }