forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-stat-sum.R
43 lines (34 loc) · 1.38 KB
/
test-stat-sum.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
context("stat_sum")
test_that("handles grouping correctly", {
d <- diamonds[1:1000, ]
all_ones <- function(x) all.equal(mean(x), 1)
base <- ggplot(d, aes(cut, clarity))
ret <- layer_data(base + stat_sum())
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_true(all_ones(ret$prop))
ret <- layer_data(base + stat_sum(aes(group = 1)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_equal(sum(ret$prop), 1)
ret <- layer_data(base + stat_sum(aes(group = cut)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_true(all_ones(tapply(ret$prop, ret$x, FUN = sum)))
ret <- layer_data(base + stat_sum(aes(group = cut, colour = cut)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_true(all_ones(tapply(ret$prop, ret$x, FUN = sum)))
ret <- layer_data(base + stat_sum(aes(group = clarity)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_true(all_ones(tapply(ret$prop, ret$y, FUN = sum)))
ret <- layer_data(base + stat_sum(aes(group = clarity, colour = cut)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), nrow(d))
expect_true(all_ones(tapply(ret$prop, ret$y, FUN = sum)))
ret <- layer_data(base + stat_sum(aes(group = 1, weight = price)))
expect_equal(nrow(ret), 38)
expect_equal(sum(ret$n), sum(d$price))
expect_equal(sum(ret$prop), 1)
})