Skip to content

Commit ddf8d41

Browse files
committed
Merge 037a027 into fb40491
2 parents fb40491 + 037a027 commit ddf8d41

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

R/trace_generation.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ toBasic <- list(
384384
g$params$yend <- max(g$prestats.data$globymax)
385385
g
386386
},
387+
jitter=function(g) {
388+
if ("size" %in% names(g$data)) {
389+
g$params$sizemin <- min(g$prestats.data$globsizemin)
390+
g$params$sizemax <- max(g$prestats.data$globsizemax)
391+
}
392+
g$geom <- "point"
393+
g
394+
},
387395
point=function(g) {
388396
if ("size" %in% names(g$data)) {
389397
g$params$sizemin <- min(g$prestats.data$globsizemin)

tests/testthat/test-cookbook-scatterplots.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ g <- ggplot(dat, aes(x=xrnd, y=yrnd)) +
7272
geom_point(shape=1, # Use hollow circles
7373
position=position_jitter(width=1,height=.5))
7474
save_outputs(g, "scatterplots-jitter")
75+
76+
# Jitter the points using geom_jitter
77+
# Jitter range is 1 on the x-axis, .5 on the y-axis
78+
g <- ggplot(dat, aes(x = xrnd, y = yrnd)) +
79+
geom_jitter(shape = 1, # Use hollow circles
80+
width = 1, height = 0.5)
81+
save_outputs(g, "scatterplots-geom_jitter")
82+

tests/testthat/test-ggplot-jitter.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
context("geom_jitter")
2+
3+
# Expect trace function
4+
expect_traces <- function(gg, n_traces, name) {
5+
stopifnot(is.ggplot(gg))
6+
stopifnot(is.numeric(n_traces))
7+
save_outputs(gg, paste0("jitter-", name))
8+
L <- gg2list(gg)
9+
all_traces <- L$data
10+
no_data <- sapply(all_traces, function(tr) {
11+
is.null(tr[["x"]]) && is.null(tr[["y"]])
12+
})
13+
has_data <- all_traces[!no_data]
14+
expect_equal(length(has_data), n_traces)
15+
list(traces = has_data, layout = L$layout)
16+
}
17+
18+
set.seed(1001)
19+
p <- ggplot(mpg, aes(cyl, hwy)) + geom_jitter()
20+
21+
test_that("geom_jitter is working", {
22+
info <- expect_traces(p, 1, "basic")
23+
tr <- info$traces[[1]]
24+
expect_identical(tr$type, "scatter")
25+
# default jitter is 40% of the resolution of the data.
26+
diffs <- abs(mpg$cyl - tr$x)
27+
expect_true(all(0 < diffs & diffs < 0.4))
28+
})

0 commit comments

Comments
 (0)