Created
August 17, 2011 21:27
-
-
Save briandk/1152683 to your computer and use it in GitHub Desktop.
Three plots to show that geom_jitter() calls must pass a position=position_jitter() argument to preserve jittering width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
set.seed(1001) | |
x <- rep(0, times = 1000) | |
x.jittered <- jitter(x, amount = 2) | |
y <- rnorm(1000) | |
dd <- data.frame(x, x.jittered, y) | |
p1 <- ggplot() | |
set.seed(1001) | |
p1 <- p1 + geom_jitter( | |
aes(x = x, | |
y = y | |
), data = dd, | |
width = 2 | |
) | |
p1 <- p1 + opts(title = "Plot using geom_jitter() and width=2") | |
print(p1) | |
p2 <- ggplot() | |
p2 <- p2 + geom_point( | |
aes(x = x.jittered, | |
y = y | |
), data = dd | |
) | |
p2 <- p2 + opts(title = "Plot using geom_point() and jitter(x, amount=2)") | |
print(p2) | |
# To prove that x.jittered values lie outside the plotting window defined in p1 | |
head(dd) | |
str(p1) | |
# Proof that the position_jitter call must be explicit | |
p3 <- ggplot() | |
set.seed(1001) | |
p3 <- p3 + geom_jitter( | |
aes(x = x, | |
y = y | |
), data = dd, | |
position = position_jitter(width = 2) | |
) | |
p3 <- p3 + opts(title = "Plot using geom_jitter() and explicit position_jitter call") | |
print(p3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment