Skip to content

Commit 5e640a2

Browse files
committed
Add initial stab at position_jitterdodge
1 parent ae175ae commit 5e640a2

9 files changed

+103
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Collate:
143143
'position-fill.r'
144144
'position-identity.r'
145145
'position-jitter.r'
146+
'position-jitterdodge.R'
146147
'position-stack.r'
147148
'quick-plot.r'
148149
'save.r'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ export(plotmatrix)
286286
export(position_dodge)
287287
export(position_fill)
288288
export(position_identity)
289+
export(position_jd)
289290
export(position_jitter)
291+
export(position_jitterdodge)
290292
export(position_stack)
291293
export(qplot)
292294
export(quickplot)

R/position-jitterdodge.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' Jitter-dodge points to align them with a boxplot including fill aesthetic
2+
#'
3+
#' @family position adjustments
4+
#' @param width degree of jitter in x direction. Defaults to 40\% of the
5+
#' resolution of the data.
6+
#' @param height degree of jitter in y direction. Defaults to 40\% of the
7+
#' resolution of the data
8+
#' @export
9+
#' @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+
}
17+
18+
#' @rdname position_jitterdodge
19+
#' @export
20+
position_jd <- position_jitterdodge
21+
22+
PositionJitterDodge <- proto(Position, {
23+
objname <- "jitterdodge"
24+
25+
adjust <- function(., data) {
26+
27+
if (empty(data)) return(data.frame())
28+
check_required_aesthetics(c("x", "y", "fill"), names(data), "position_jitterdodge")
29+
30+
## Workaround to avoid this warning:
31+
## ymax not defined: adjusting position using y instead
32+
if (!("ymax" %in% names(data))) {
33+
data$ymax <- data$y
34+
}
35+
36+
## Adjust the x transformation based on the number of 'fill' variables
37+
nfill <- length( levels(data$fill) )
38+
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
41+
42+
trans_x <- NULL
43+
trans_y <- NULL
44+
if(.$width > 0) {
45+
trans_x <- function(x) jitter(x, amount = .$width / (nfill + 2))
46+
}
47+
if(.$height > 0) {
48+
trans_y <- function(x) jitter(x, amount = .$height)
49+
}
50+
51+
## dodge, then jitter
52+
data <- collide(data, 0.75, .$my_name(), pos_dodge, check.width = FALSE)
53+
transform_position(data, trans_x, trans_y)
54+
}
55+
56+
})

man/position_dodge.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ 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}},
46+
\code{\link{position_jitterdodge}};
4547
\code{\link{position_jitter}};
4648
\code{\link{position_stack}}
4749
}

man/position_fill.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ 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}},
41+
\code{\link{position_jitterdodge}};
4042
\code{\link{position_jitter}};
4143
\code{\link{position_stack}}
4244
}

man/position_identity.Rd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Don't adjust position
1717
}
1818
\seealso{
1919
Other position adjustments: \code{\link{position_dodge}};
20-
\code{\link{position_fill}};
20+
\code{\link{position_fill}}; \code{\link{position_jd}},
21+
\code{\link{position_jitterdodge}};
2122
\code{\link{position_jitter}};
2223
\code{\link{position_stack}}
2324
}

man/position_jitter.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ 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}},
40+
\code{\link{position_jitterdodge}};
3941
\code{\link{position_stack}}
4042
}
4143

man/position_jitterdodge.Rd

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
% Generated by roxygen2 (4.0.0): do not edit by hand
2+
\name{position_jitterdodge}
3+
\alias{position_jd}
4+
\alias{position_jitterdodge}
5+
\title{Jitter-dodge points to align them with a boxplot including fill aesthetic}
6+
\usage{
7+
position_jitterdodge(width = NULL, height = NULL)
8+
9+
position_jd(width = NULL, height = NULL)
10+
}
11+
\arguments{
12+
\item{width}{degree of jitter in x direction. Defaults to
13+
40\% of the resolution of the data.}
14+
15+
\item{height}{degree of jitter in y direction. Defaults
16+
to 40\% of the resolution of the data}
17+
}
18+
\description{
19+
Jitter-dodge points to align them with a boxplot including fill aesthetic
20+
}
21+
\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+
}
27+
\seealso{
28+
Other position adjustments: \code{\link{position_dodge}};
29+
\code{\link{position_fill}};
30+
\code{\link{position_identity}};
31+
\code{\link{position_jitter}};
32+
\code{\link{position_stack}}
33+
}
34+

man/position_stack.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ 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}},
49+
\code{\link{position_jitterdodge}};
4850
\code{\link{position_jitter}}
4951
}
5052

0 commit comments

Comments
 (0)