Skip to content

Commit dfb079a

Browse files
committed
get test working; jitter shouldn't be a basic geom
1 parent e100c07 commit dfb079a

File tree

2 files changed

+15
-54
lines changed

2 files changed

+15
-54
lines changed

R/trace_generation.R

Lines changed: 8 additions & 32 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)
@@ -556,38 +564,6 @@ geom2trace <- list(
556564
}
557565
L
558566
},
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-
},
591567
text=function(data, params){
592568
L <- list(x=data$x,
593569
y=data$y,

tests/testthat/test-ggplot-jitter.R

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
context("geom_jitter")
22

33
# Expect trace function
4-
expect_traces <- function(gg, n_traces, name, seed) {
4+
expect_traces <- function(gg, n_traces, name) {
55
stopifnot(is.ggplot(gg))
66
stopifnot(is.numeric(n_traces))
7-
save_outputs(gg, paste0("coord_fixed-", name))
8-
set.seed(seed)
7+
save_outputs(gg, paste0("jitter-", name))
98
L <- gg2list(gg)
109
all_traces <- L$data
1110
no_data <- sapply(all_traces, function(tr) {
@@ -16,27 +15,13 @@ expect_traces <- function(gg, n_traces, name, seed) {
1615
list(traces = has_data, layout = L$layout)
1716
}
1817

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
2618
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
19+
p <- ggplot(mpg, aes(cyl, hwy)) + geom_jitter()
20+
3021
test_that("geom_jitter is working", {
31-
info <- expect_traces(p1, 1, "geom_jitter", 1001)
22+
info <- expect_traces(p1, 1, "basic")
3223
tr <- info$traces[[1]]
33-
la <- info$layout
3424
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)
25+
# default jitter is 40% of the resolution of the data.
26+
expect_true(all(0 < abs(mpg$cyl - tr$x) < 0.4))
4127
})
42-

0 commit comments

Comments
 (0)