Skip to content

Commit

Permalink
Merge pull request #32 from eclarke/fixGroupOnX
Browse files Browse the repository at this point in the history
Fix #31 with groupOnX defaults
  • Loading branch information
eclarke authored Aug 3, 2017
2 parents 2c50b23 + 18c2c4f commit eba17b0
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 41 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: ggbeeswarm
Type: Package
Title: Categorical Scatter (Violin Point) Plots
Version: 0.5.4
Date: 2017-07-31
Version: 0.6.0
Date: 2017-08-03
Authors@R: c(
person(given="Erik", family="Clarke", role=c("aut", "cre"), email="[email protected]"),
person(given="Scott", family="Sherrill-Mix", role=c("aut"), email="[email protected]"))
Expand Down
8 changes: 5 additions & 3 deletions R/position-beeswarm.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @family position adjustments
#' @param priority Method used to perform point layout (see \code{\link{swarmx}})
#' @param cex Scaling for adjusting point spacing (see \code{\link{swarmx}})
#' @param groupOnX should jitter be added to the x axis if TRUE or y axis if FALSE (the default NULL causes the function to guess which axis is the categorical one based on the number of unique entries in each)
#' @param groupOnX if TRUE then jitter is added to the x axis and if FALSE jitter is added to the y axis. Prior to v0.6.0, the default NULL causes the function to guess which axis is the categorical one based on the number of unique entries in each. This could result in unexpected results when the x variable has few unique values and so in v0.6.0 the default was changed to always jitter on the x axis unless groupOnX=FALSE. Also consider \code{\link[ggplot2]{coord_flip}}.
#' @param dodge.width Amount by which points from different aesthetic groups will be dodged. This requires that one of the aesthetics is a factor.
#' @export
#' @importFrom beeswarm swarmx
Expand All @@ -28,8 +28,10 @@ PositionBeeswarm <- ggplot2::ggproto("PositionBeeswarm",ggplot2:::Position, requ
data <- remove_missing(data, vars = c("x","y"), name = "position_beeswarm")
if (nrow(data)==0) return(data.frame())

# more unique entries in x than y suggests y (not x) is categorical
if(is.null(params$groupOnX)) params$groupOnX <- length(unique(data$y)) > length(unique(data$x))
if(is.null(params$groupOnX)){
params$groupOnX<-TRUE
if(length(unique(data$y)) <= length(unique(data$x))) warning('The default behavior of beeswarm has changed in version 0.6.0. In versions <0.6.0, this plot would have been dodged on the y-axis. In versions >=0.6.0, grouponX=FALSE must be explicitly set to group on y-axis. Please set grouponX=TRUE/FALSE to avoid this warning and ensure proper axis choice.')
}

# dodge
if(!params$groupOnX){
Expand Down
7 changes: 5 additions & 2 deletions R/position-quasirandom.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' Smaller numbers (< 1) produce a tighter "fit". (default: 0.5)
#' @param nbins the number of bins used when calculating density (has little effect with quasirandom/random distribution)
#' @param method the method used for distributing points (quasirandom, pseudorandom, smiley or frowney)
#' @param groupOnX should jitter be added to the x axis if TRUE or y axis if FALSE (the default NULL causes the function to guess which axis is the categorical one based on the number of unique entries in each)
#' @param groupOnX if TRUE then jitter is added to the x axis and if FALSE jitter is added to the y axis. Prior to v0.6.0, the default NULL causes the function to guess which axis is the categorical one based on the number of unique entries in each. This could result in unexpected results when the x variable has few unique values and so in v0.6.0 the default was changed to always jitter on the x axis unless groupOnX=FALSE. Also consider \code{\link[ggplot2]{coord_flip}}.
#' @param dodge.width Amount by which points from different aesthetic groups will be dodged. This requires that one of the aesthetics is a factor.
#' @export
#' @importFrom vipor offsetX
Expand All @@ -28,7 +28,10 @@ PositionQuasirandom <- ggplot2::ggproto("PositionQuasirandom",ggplot2:::Position
data <- remove_missing(data, vars = c("x","y"), name = "position_quasirandom")
if (nrow(data)==0) return(data.frame())

if(is.null(params$groupOnX)) params$groupOnX <- length(unique(data$y)) > length(unique(data$x))
if(is.null(params$groupOnX)){
params$groupOnX<-TRUE
if(length(unique(data$y)) <= length(unique(data$x))) warning('The default behavior of beeswarm has changed in version 0.6.0. In versions <0.6.0, this plot would have been dodged on the y-axis. In versions >=0.6.0, grouponX=FALSE must be explicitly set to group on y-axis. Please set grouponX=TRUE/FALSE to avoid this warning and ensure proper axis choice.')
}

# dodge
if(!params$groupOnX){
Expand Down
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Beeswarm plots (aka column scatter plots or violin scatter plots) are a way of p

Features:

- Can handle categorical variables on the y-axis (thanks @smsaladi)
- Can handle categorical variables on the y-axis (thanks @smsaladi, @koncina)
- Automatically dodges if a grouping variable is categorical and `dodge.width` is specified (thanks @josesho)

See the examples below.
Expand Down Expand Up @@ -64,7 +64,7 @@ Using `geom_quasirandom`:
ggplot(mpg,aes(class, hwy)) + geom_quasirandom()
# With categorical y-axis
ggplot(mpg,aes(hwy, class)) + geom_quasirandom()
ggplot(mpg,aes(hwy, class)) + geom_quasirandom(groupOnX=FALSE)
# Some groups may have only a few points. Use `varwidth=TRUE` to adjust width dynamically.
ggplot(mpg,aes(class, hwy)) + geom_quasirandom(varwidth = TRUE)
Expand Down Expand Up @@ -92,7 +92,7 @@ Using `geom_beeswarm`:
ggplot(iris,aes(Species, Sepal.Length)) + geom_beeswarm()
ggplot(mpg,aes(class, hwy)) + geom_beeswarm()
# With categorical y-axis
ggplot(mpg,aes(hwy, class)) + geom_beeswarm(cex=1.2)
ggplot(mpg,aes(hwy, class)) + geom_beeswarm(cex=1.2,groupOnX=FALSE)
# ggplot doesn't pass any information about the actual device size of the points
# to the underlying layout code, so it's important to manually adjust the `cex`
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Beeswarm plots (aka column scatter plots or violin scatter plots) are a way of p

Features:

- Can handle categorical variables on the y-axis (thanks @smsaladi)
- Can handle categorical variables on the y-axis (thanks @smsaladi, @koncina)
- Automatically dodges if a grouping variable is categorical and `dodge.width` is specified (thanks @josesho)

See the examples below.
Expand Down Expand Up @@ -66,7 +66,7 @@ ggplot(mpg,aes(class, hwy)) + geom_quasirandom()

```r
# With categorical y-axis
ggplot(mpg,aes(hwy, class)) + geom_quasirandom()
ggplot(mpg,aes(hwy, class)) + geom_quasirandom(groupOnX=FALSE)
```

<img src="README_files/figure-html/ggplot2-examples-2.png" title="plot of chunk ggplot2-examples" alt="plot of chunk ggplot2-examples" width="432" />
Expand Down Expand Up @@ -142,7 +142,7 @@ ggplot(mpg,aes(class, hwy)) + geom_beeswarm()

```r
# With categorical y-axis
ggplot(mpg,aes(hwy, class)) + geom_beeswarm(cex=1.2)
ggplot(mpg,aes(hwy, class)) + geom_beeswarm(cex=1.2,groupOnX=FALSE)
```

<img src="README_files/figure-html/ggplot2-beeswarm-3.png" title="plot of chunk ggplot2-beeswarm" alt="plot of chunk ggplot2-beeswarm" width="432" />
Expand Down
12 changes: 6 additions & 6 deletions inst/doc/usageExamples.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ packageKeywords<-"visualization, display, one dimensional, grouped, groups, viol
###################################################
### code chunk number 8: yaxis (eval = FALSE)
###################################################
## ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs))
## ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs),groupOnX=FALSE)


###################################################
### code chunk number 9: showYaxis
###################################################
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs))
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs),groupOnX=FALSE)


###################################################
Expand All @@ -87,14 +87,14 @@ packageKeywords<-"visualization, display, one dimensional, grouped, groups, viol
### code chunk number 12: dodgey (eval = FALSE)
###################################################
## labs2<-factor(rep(1:2,each=n))
## ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8)
## ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8,groupOnX=FALSE)


###################################################
### code chunk number 13: showDodgey
###################################################
labs2<-factor(rep(1:2,each=n))
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8)
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8,groupOnX=FALSE)


###################################################
Expand All @@ -115,14 +115,14 @@ packageKeywords<-"visualization, display, one dimensional, grouped, groups, viol
### code chunk number 16: dodgeYBee (eval = FALSE)
###################################################
## ggplot(mapping=aes(dat,labs,color=labs2)) +
## geom_beeswarm(dodge.width=.8,cex=2)
## geom_beeswarm(dodge.width=.8,cex=2,groupOnX=FALSE)


###################################################
### code chunk number 17: showDodgeYBee
###################################################
ggplot(mapping=aes(dat,labs,color=labs2)) +
geom_beeswarm(dodge.width=.8,cex=2)
geom_beeswarm(dodge.width=.8,cex=2,groupOnX=FALSE)


###################################################
Expand Down
8 changes: 4 additions & 4 deletions inst/doc/usageExamples.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ Factors can be used to generate custom group orderings:
@
\end{center}

The axes can also be switched with a categorical y-axis:
The axes can also be switched with a categorical y-axis using the argument \code{groupOnX=FALSE}:
<<yaxis, echo=TRUE, eval=FALSE>>=
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs))
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs),groupOnX=FALSE)
@
\begin{center}
<<showYaxis, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand All @@ -92,7 +92,7 @@ And dodging can be used to compare within groups:
Or on the y-axis:
<<dodgey, echo=TRUE, eval=FALSE>>=
labs2<-factor(rep(1:2,each=n))
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8)
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8,groupOnX=FALSE)
@
\begin{center}
<<showDodgey, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand All @@ -112,7 +112,7 @@ And with \code{geom_beeswarm}:
\end{center}
<<dodgeYBee, echo=TRUE, eval=FALSE>>=
ggplot(mapping=aes(dat,labs,color=labs2)) +
geom_beeswarm(dodge.width=.8,cex=2)
geom_beeswarm(dodge.width=.8,cex=2,groupOnX=FALSE)
@
\begin{center}
<<showDodgeYBee, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand Down
Binary file modified inst/doc/usageExamples.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions man/geom_beeswarm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/geom_quasirandom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/ggbeeswarm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/position_beeswarm.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/position_quasirandom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vignettes/usageExamples.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ Factors can be used to generate custom group orderings:
@
\end{center}

The axes can also be switched with a categorical y-axis:
The axes can also be switched with a categorical y-axis using the argument \code{groupOnX=FALSE}:
<<yaxis, echo=TRUE, eval=FALSE>>=
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs))
ggplot(mapping=aes(dat,labs)) + geom_quasirandom(aes(color=labs),groupOnX=FALSE)
@
\begin{center}
<<showYaxis, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand All @@ -92,7 +92,7 @@ And dodging can be used to compare within groups:
Or on the y-axis:
<<dodgey, echo=TRUE, eval=FALSE>>=
labs2<-factor(rep(1:2,each=n))
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8)
ggplot(mapping=aes(dat,labs,color=labs2)) + geom_quasirandom(dodge.width=.8,groupOnX=FALSE)
@
\begin{center}
<<showDodgey, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand All @@ -112,7 +112,7 @@ And with \code{geom_beeswarm}:
\end{center}
<<dodgeYBee, echo=TRUE, eval=FALSE>>=
ggplot(mapping=aes(dat,labs,color=labs2)) +
geom_beeswarm(dodge.width=.8,cex=2)
geom_beeswarm(dodge.width=.8,cex=2,groupOnX=FALSE)
@
\begin{center}
<<showDodgeYBee, fig=TRUE, height=3.5, width=5, echo=FALSE>>=
Expand Down

0 comments on commit eba17b0

Please sign in to comment.