Skip to content

Commit faec5fb

Browse files
committed
Merge branch 'baobao-geom_jitter' of https://github.com/ropensci/plotly into baobao-geom_jitter
2 parents ef440d2 + 037a027 commit faec5fb

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
@@ -401,6 +401,14 @@ toBasic <- list(
401401
g$params$yend <- max(g$prestats.data$globymax)
402402
g
403403
},
404+
jitter=function(g) {
405+
if ("size" %in% names(g$data)) {
406+
g$params$sizemin <- min(g$prestats.data$globsizemin)
407+
g$params$sizemax <- max(g$prestats.data$globsizemax)
408+
}
409+
g$geom <- "point"
410+
g
411+
},
404412
point=function(g) {
405413
if ("size" %in% names(g$data)) {
406414
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
@@ -71,3 +71,11 @@ g <- ggplot(dat, aes(x=xrnd, y=yrnd)) +
7171
geom_point(shape=1, # Use hollow circles
7272
position=position_jitter(width=1,height=.5))
7373
save_outputs(g, "scatterplots-jitter")
74+
75+
# Jitter the points using geom_jitter
76+
# Jitter range is 1 on the x-axis, .5 on the y-axis
77+
g <- ggplot(dat, aes(x = xrnd, y = yrnd)) +
78+
geom_jitter(shape = 1, # Use hollow circles
79+
width = 1, height = 0.5)
80+
save_outputs(g, "scatterplots-geom_jitter")
81+

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)