Skip to content

Commit ce75c32

Browse files
committed
Merge branch 'master' of github.com:kevinushey/ggplot2 into kevinushey-master
2 parents 687ba6e + 9b5bb8b commit ce75c32

10 files changed

+136
-0
lines changed

DESCRIPTION

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

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export(position_dodge)
288288
export(position_fill)
289289
export(position_identity)
290290
export(position_jitter)
291+
export(position_jitterdodge)
291292
export(position_stack)
292293
export(qplot)
293294
export(quickplot)

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ ggplot2 0.9.3.1.99
1717

1818
* Allow to use brewer palettes for continuous scales, through the new
1919
`scale_fill/colour_distiller()` functions (@jiho, #925).
20+
* `position_jitterdodge()` combines `position_jitter()` and `position_dodge()`,
21+
allowing the user to plot and align points generated by e.g. `geom_point()`
22+
with those generated by a dodged `geom_boxplot()`. See
23+
`example(position_jitterdodge)` for a potential usage. (@kevinushey, #932)
2024

2125
* Allow specifying only one of the limits in a scale and use the automatic
2226
calculation of the other limit by passing NA to to the limit function,

R/position-jitterdodge.R

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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).
6+
#'
7+
#' @family position adjustments
8+
#' @param jitter.width degree of jitter in x direction. Defaults to 40\% of the
9+
#' 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.
13+
#' @export
14+
#' @examples
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) {
22+
23+
PositionJitterDodge$new(jitter.width = jitter.width,
24+
jitter.height = jitter.height,
25+
dodge.width = dodge.width)
26+
}
27+
28+
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+
45+
objname <- "jitterdodge"
46+
47+
adjust <- function(., data) {
48+
49+
if (empty(data)) return(data.frame())
50+
check_required_aesthetics(c("x", "y", "fill"), names(data), "position_jitterdodge")
51+
52+
## Workaround to avoid this warning:
53+
## ymax not defined: adjusting position using y instead
54+
if (!("ymax" %in% names(data))) {
55+
data$ymax <- data$y
56+
}
57+
58+
## Adjust the x transformation based on the number of 'fill' variables
59+
nfill <- length(levels(data$fill))
60+
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+
}
68+
69+
trans_x <- NULL
70+
trans_y <- NULL
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)
76+
}
77+
78+
if (is.null(.$dodge.width)) {
79+
.$dodge.width <- 0.75
80+
}
81+
82+
## dodge, then jitter
83+
data <- collide(data, .$dodge.width, .$my_name(), pos_dodge, check.width = FALSE)
84+
transform_position(data, trans_x, trans_y)
85+
}
86+
87+
})

man/position_dodge.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ 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_jitterdodge}};
4546
\code{\link{position_jitter}};
4647
\code{\link{position_stack}}
4748
}

man/position_fill.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ 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_jitterdodge}};
4041
\code{\link{position_jitter}};
4142
\code{\link{position_stack}}
4243
}

man/position_identity.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Don't adjust position
1818
\seealso{
1919
Other position adjustments: \code{\link{position_dodge}};
2020
\code{\link{position_fill}};
21+
\code{\link{position_jitterdodge}};
2122
\code{\link{position_jitter}};
2223
\code{\link{position_stack}}
2324
}

man/position_jitter.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ 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_jitterdodge}};
3940
\code{\link{position_stack}}
4041
}
4142

man/position_jitterdodge.Rd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
% Generated by roxygen2 (4.0.0): do not edit by hand
2+
\name{position_jitterdodge}
3+
\alias{position_jitterdodge}
4+
\title{Adjust position by simultaneously dodging and jittering}
5+
\usage{
6+
position_jitterdodge(jitter.width = NULL, jitter.height = NULL,
7+
dodge.width = NULL)
8+
}
9+
\arguments{
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.}
15+
16+
\item{dodge.width}{the amount to dodge in the x
17+
direction. Defaults to 0.75, the default
18+
\code{position_dodge()} width.}
19+
}
20+
\description{
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).
24+
}
25+
\examples{
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())
30+
}
31+
\seealso{
32+
Other position adjustments: \code{\link{position_dodge}};
33+
\code{\link{position_fill}};
34+
\code{\link{position_identity}};
35+
\code{\link{position_jitter}};
36+
\code{\link{position_stack}}
37+
}
38+

man/position_stack.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ 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_jitterdodge}};
4849
\code{\link{position_jitter}}
4950
}
5051

0 commit comments

Comments
 (0)