Skip to content

Commit e100c07

Browse files
committed
Merge branch 'baobao-geom_jitter' of https://github.com/ropensci/plotly into baobao-geom_jitter
2 parents 4e00343 + 2060c21 commit e100c07

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

R/trace_generation.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,38 @@ geom2trace <- list(
556556
}
557557
L
558558
},
559+
jitter=function(data, params){
560+
L <- list(x = data$x,
561+
y = data$y,
562+
name = params$name,
563+
text = data$text,
564+
type = "scatter",
565+
mode = "markers",
566+
marker = paramORdefault(params, aes2marker, marker.defaults))
567+
if ("size" %in% names(data)) {
568+
L$text <- paste("size:", data$size)
569+
L$marker$sizeref <- default.marker.sizeref
570+
# Make sure sizes are passed as a list even when there is only one element.
571+
s <- data$size
572+
marker.size <- 5 * (s - params$sizemin) /
573+
(params$sizemax - params$sizemin) + 0.25
574+
marker.size <- marker.size * marker.size.mult
575+
L$marker$size <- ifelse(length(s) > 1, marker.size, list(marker.size))
576+
L$marker$line$width <- 0
577+
}
578+
if (!is.null(params$shape) && params$shape %in% c(21:25)) {
579+
L$marker$color <- ifelse(!is.null(params$fill),
580+
toRGB(params$fill), "rgba(0,0,0,0)")
581+
if (!is.null(params$colour)) {
582+
L$marker$line$color <- toRGB(params$colour)
583+
}
584+
L$marker$line$width <- 1
585+
}
586+
if (!is.null(params$shape) && params$shape %in% c(32)) {
587+
L$visible <- FALSE
588+
}
589+
return(L)
590+
},
559591
text=function(data, params){
560592
L <- list(x=data$x,
561593
y=data$y,

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
context("geom_jitter")
2+
3+
# Expect trace function
4+
expect_traces <- function(gg, n_traces, name, seed) {
5+
stopifnot(is.ggplot(gg))
6+
stopifnot(is.numeric(n_traces))
7+
save_outputs(gg, paste0("coord_fixed-", name))
8+
set.seed(seed)
9+
L <- gg2list(gg)
10+
all_traces <- L$data
11+
no_data <- sapply(all_traces, function(tr) {
12+
is.null(tr[["x"]]) && is.null(tr[["y"]])
13+
})
14+
has_data <- all_traces[!no_data]
15+
expect_equal(length(has_data), n_traces)
16+
list(traces = has_data, layout = L$layout)
17+
}
18+
19+
#head(L$data[[1]]$x)
20+
21+
# get data from mpg dataset
22+
p <- ggplot(mpg, aes(displ, hwy))
23+
24+
# Test 1
25+
# set up the data
26+
set.seed(1001)
27+
p1 <- ggplot() + geom_jitter(data = mpg, aes(displ, hwy), width = 1)
28+
head(ggplot_build2(p1)$data[[1]]$x)
29+
# test
30+
test_that("geom_jitter is working", {
31+
info <- expect_traces(p1, 1, "geom_jitter", 1001)
32+
tr <- info$traces[[1]]
33+
la <- info$layout
34+
expect_identical(tr$type, "scatter")
35+
set.seed(1001)
36+
built <- ggplot_build2(p1)
37+
print(head(tr$x)) # from gg2list
38+
print(head(built$data[[1]]$x)) # from ggplot_build2
39+
expect_identical(tr$x, built$data[[1]]$x)
40+
expect_identical(tr$y, built$data[[1]]$y)
41+
})
42+

0 commit comments

Comments
 (0)