File: test-stat-sum.R

package info (click to toggle)
r-cran-ggplot2 3.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 8,748 kB
  • sloc: sh: 15; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 2,253 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)
})


# Visual tests ------------------------------------------------------------

test_that("summaries are drawn correctly", {
  expect_doppelganger("summary with color and lines",
    ggplot(mtcars, aes(x = cyl, y = mpg, colour = factor(vs))) +
      geom_point() +
      stat_summary(fun = mean, geom = "line", linewidth = 2)
  )
  expect_doppelganger("summary with crossbars, no grouping",
    ggplot(mtcars, aes(x = cyl, y = mpg)) +
      geom_point() +
      stat_summary(
        fun.data = mean_se,
        colour = "red",
        geom = "crossbar",
        width = 0.2
      )
  )
  expect_doppelganger("summary with crossbars, manual grouping",
    ggplot(mtcars, aes(x = cyl, y = mpg, group = cyl)) +
      geom_point() +
      stat_summary(
        fun.data = mean_se,
        colour = "red",
        geom = "crossbar",
        width = 0.2
      )
  )
})