Skip to content

Commit 2060c21

Browse files
committed
first attempt to testing geom_jitter
1 parent 880f42d commit 2060c21

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

R/trace_generation.R

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -557,33 +557,36 @@ geom2trace <- list(
557557
L
558558
},
559559
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)){
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)) {
568568
L$text <- paste("size:", data$size)
569569
L$marker$sizeref <- default.marker.sizeref
570570
# Make sure sizes are passed as a list even when there is only one element.
571571
s <- data$size
572-
marker.size <- 5 * (s - params$sizemin)/(params$sizemax - params$sizemin) + 0.25
572+
marker.size <- 5 * (s - params$sizemin) /
573+
(params$sizemax - params$sizemin) + 0.25
573574
marker.size <- marker.size * marker.size.mult
574-
L$marker$size <- if (length(s) > 1) marker.size else list(marker.size)
575+
L$marker$size <- ifelse(length(s) > 1, marker.size, list(marker.size))
575576
L$marker$line$width <- 0
576577
}
577578
if (!is.null(params$shape) && params$shape %in% c(21:25)) {
578-
L$marker$color <- ifelse(!is.null(params$fill), toRGB(params$fill), "rgba(0,0,0,0)")
579-
if (!is.null(params$colour))
580-
L$marker$line$color <- toRGB(params$colour)
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+
}
581584
L$marker$line$width <- 1
582585
}
583586
if (!is.null(params$shape) && params$shape %in% c(32)) {
584587
L$visible <- FALSE
585588
}
586-
L
589+
return(L)
587590
},
588591
text=function(data, params){
589592
L <- list(x=data$x,

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)