Skip to content

Commit 9b5bb8b

Browse files
committed
Updates to position_jitterdodge()
* Motivate the change in NEWS * Fix the style * Allow jitter.width, jitter.height, dodge.width as arguments * Improve the documentation
1 parent 5e640a2 commit 9b5bb8b

9 files changed

+77
-42
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ export(plotmatrix)
286286
export(position_dodge)
287287
export(position_fill)
288288
export(position_identity)
289-
export(position_jd)
290289
export(position_jitter)
291290
export(position_jitterdodge)
292291
export(position_stack)

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ggplot2 0.9.3.1.99
22
----------------------------------------------------------------
33

4+
* `position_jitterdodge()` combines `position_jitter()` and `position_dodge()`,
5+
allowing the user to plot and align points generated by e.g. `geom_point()`
6+
with those generated by a dodged `geom_boxplot()`. See
7+
`example(position_jitterdodge)` for a potential usage. (@kevinushey, #932)
8+
49
* Allow specifying only one of the limits in a scale and use the automatic
510
calculation of the other limit by passing NA to to the limit function,
611
`xlim()` or `ylim()` (@jimhester, #557).

R/position-jitterdodge.R

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,47 @@
1-
#' Jitter-dodge points to align them with a boxplot including fill aesthetic
1+
#' Adjust position by simultaneously dodging and jittering
2+
#'
3+
#' This is primarily used for aligning points generated through
4+
#' \code{geom_point()} with dodged boxplots (e.g., a \code{geom_boxplot()} with
5+
#' a fill aesthetic supplied).
26
#'
37
#' @family position adjustments
4-
#' @param width degree of jitter in x direction. Defaults to 40\% of the
8+
#' @param jitter.width degree of jitter in x direction. Defaults to 40\% of the
59
#' resolution of the data.
6-
#' @param height degree of jitter in y direction. Defaults to 40\% of the
7-
#' resolution of the data
10+
#' @param jitter.height degree of jitter in y direction. Defaults to 0.
11+
#' @param dodge.width the amount to dodge in the x direction. Defaults to 0.75,
12+
#' the default \code{position_dodge()} width.
813
#' @export
914
#' @examples
10-
#' dsub <- diamonds[ sample(1:nrow(diamonds), 1000), ]
11-
#' ggplot(dsub, aes(x=cut, y=carat, fill=clarity)) +
12-
#' geom_boxplot(outlier.size=0) +
13-
#' geom_point( pch=21, position=position_jitterdodge() )
14-
position_jitterdodge <- function (width = NULL, height = NULL) {
15-
PositionJitterDodge$new(width = width, height = height)
16-
}
15+
#' dsub <- diamonds[ sample(nrow(diamonds), 1000), ]
16+
#' ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
17+
#' geom_boxplot(outlier.size = 0) +
18+
#' geom_point(pch = 21, position = position_jitterdodge())
19+
position_jitterdodge <- function (jitter.width = NULL,
20+
jitter.height = NULL,
21+
dodge.width = NULL) {
1722

18-
#' @rdname position_jitterdodge
19-
#' @export
20-
position_jd <- position_jitterdodge
23+
PositionJitterDodge$new(jitter.width = jitter.width,
24+
jitter.height = jitter.height,
25+
dodge.width = dodge.width)
26+
}
2127

2228
PositionJitterDodge <- proto(Position, {
29+
30+
jitter.width <- NULL
31+
jitter.height <- NULL
32+
dodge.width <- NULL
33+
34+
new <- function(.,
35+
jitter.width = NULL,
36+
jitter.height = NULL,
37+
dodge.width = NULL) {
38+
39+
.$proto(jitter.width=jitter.width,
40+
jitter.height=jitter.height,
41+
dodge.width=dodge.width)
42+
43+
}
44+
2345
objname <- "jitterdodge"
2446

2547
adjust <- function(., data) {
@@ -34,22 +56,31 @@ PositionJitterDodge <- proto(Position, {
3456
}
3557

3658
## Adjust the x transformation based on the number of 'fill' variables
37-
nfill <- length( levels(data$fill) )
59+
nfill <- length(levels(data$fill))
3860

39-
if (is.null(.$width)) .$width <- resolution(data$x, zero = FALSE) * 0.4
40-
if (is.null(.$height)) .$height <- resolution(data$y, zero = FALSE) * 0.4
61+
if (is.null(.$jitter.width)) {
62+
.$jitter.width <- resolution(data$x, zero = FALSE) * 0.4
63+
}
64+
65+
if (is.null(.$jitter.height)) {
66+
.$jitter.height <- 0
67+
}
4168

4269
trans_x <- NULL
4370
trans_y <- NULL
44-
if(.$width > 0) {
45-
trans_x <- function(x) jitter(x, amount = .$width / (nfill + 2))
71+
if (.$jitter.width > 0) {
72+
trans_x <- function(x) jitter(x, amount = .$jitter.width / (nfill + 2))
73+
}
74+
if (.$jitter.height > 0) {
75+
trans_y <- function(x) jitter(x, amount = .$jitter.height)
4676
}
47-
if(.$height > 0) {
48-
trans_y <- function(x) jitter(x, amount = .$height)
77+
78+
if (is.null(.$dodge.width)) {
79+
.$dodge.width <- 0.75
4980
}
5081

5182
## dodge, then jitter
52-
data <- collide(data, 0.75, .$my_name(), pos_dodge, check.width = FALSE)
83+
data <- collide(data, .$dodge.width, .$my_name(), pos_dodge, check.width = FALSE)
5384
transform_position(data, trans_x, trans_y)
5485
}
5586

man/position_dodge.Rd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ p + geom_errorbar(aes(ymin = y-1, ymax = y+1, width = 0.2),
4242
\seealso{
4343
Other position adjustments: \code{\link{position_fill}};
4444
\code{\link{position_identity}};
45-
\code{\link{position_jd}},
4645
\code{\link{position_jitterdodge}};
4746
\code{\link{position_jitter}};
4847
\code{\link{position_stack}}

man/position_fill.Rd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ See \code{\link{geom_bar}} and \code{\link{geom_area}} for
3737

3838
Other position adjustments: \code{\link{position_dodge}};
3939
\code{\link{position_identity}};
40-
\code{\link{position_jd}},
4140
\code{\link{position_jitterdodge}};
4241
\code{\link{position_jitter}};
4342
\code{\link{position_stack}}

man/position_identity.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Don't adjust position
1717
}
1818
\seealso{
1919
Other position adjustments: \code{\link{position_dodge}};
20-
\code{\link{position_fill}}; \code{\link{position_jd}},
20+
\code{\link{position_fill}};
2121
\code{\link{position_jitterdodge}};
2222
\code{\link{position_jitter}};
2323
\code{\link{position_stack}}

man/position_jitter.Rd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ qplot(class, hwy, data = mpg, geom = c("boxplot", "jitter"))
3636
Other position adjustments: \code{\link{position_dodge}};
3737
\code{\link{position_fill}};
3838
\code{\link{position_identity}};
39-
\code{\link{position_jd}},
4039
\code{\link{position_jitterdodge}};
4140
\code{\link{position_stack}}
4241
}

man/position_jitterdodge.Rd

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
% Generated by roxygen2 (4.0.0): do not edit by hand
22
\name{position_jitterdodge}
3-
\alias{position_jd}
43
\alias{position_jitterdodge}
5-
\title{Jitter-dodge points to align them with a boxplot including fill aesthetic}
4+
\title{Adjust position by simultaneously dodging and jittering}
65
\usage{
7-
position_jitterdodge(width = NULL, height = NULL)
8-
9-
position_jd(width = NULL, height = NULL)
6+
position_jitterdodge(jitter.width = NULL, jitter.height = NULL,
7+
dodge.width = NULL)
108
}
119
\arguments{
12-
\item{width}{degree of jitter in x direction. Defaults to
13-
40\% of the resolution of the data.}
10+
\item{jitter.width}{degree of jitter in x direction.
11+
Defaults to 40\% of the resolution of the data.}
12+
13+
\item{jitter.height}{degree of jitter in y direction.
14+
Defaults to 0.}
1415

15-
\item{height}{degree of jitter in y direction. Defaults
16-
to 40\% of the resolution of the data}
16+
\item{dodge.width}{the amount to dodge in the x
17+
direction. Defaults to 0.75, the default
18+
\code{position_dodge()} width.}
1719
}
1820
\description{
19-
Jitter-dodge points to align them with a boxplot including fill aesthetic
21+
This is primarily used for aligning points generated through
22+
\code{geom_point()} with dodged boxplots (e.g., a \code{geom_boxplot()} with
23+
a fill aesthetic supplied).
2024
}
2125
\examples{
22-
dsub <- diamonds[ sample(1:nrow(diamonds), 1000), ]
23-
ggplot(dsub, aes(x=cut, y=carat, fill=clarity)) +
24-
geom_boxplot(outlier.size=0) +
25-
geom_point( pch=21, position=position_jitterdodge() )
26+
dsub <- diamonds[ sample(nrow(diamonds), 1000), ]
27+
ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
28+
geom_boxplot(outlier.size = 0) +
29+
geom_point(pch = 21, position = position_jitterdodge())
2630
}
2731
\seealso{
2832
Other position adjustments: \code{\link{position_dodge}};

man/position_stack.Rd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ qplot(Time, Value, data = data.set, colour = Type, geom = "line",
4545
Other position adjustments: \code{\link{position_dodge}};
4646
\code{\link{position_fill}};
4747
\code{\link{position_identity}};
48-
\code{\link{position_jd}},
4948
\code{\link{position_jitterdodge}};
5049
\code{\link{position_jitter}}
5150
}

0 commit comments

Comments
 (0)