File: test-stat-summary.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 (82 lines) | stat: -rw-r--r-- 1,656 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
test_that("stat_summary(_bin) work with lambda expressions", {
  # note: stat_summary and stat_summary_bin both use
  # make_summary_fun, so this tests both

  dat <- data_frame(
    x = c(1, 1, 2, 2, 3, 3),
    y = c(0, 2, 1, 3, 2, 4)
  )

  p1 <- ggplot(dat, aes(x, y)) +
    stat_summary(fun.data = mean_se)


  # test fun.data
  p2 <- ggplot(dat, aes(x, y)) +
    stat_summary(fun.data = ~ {
      mean <- mean(.x)
      se <- sqrt(stats::var(.x) / length(.x))
      data_frame(y = mean, ymin = mean - se, ymax = mean + se)
    })

  expect_equal(
    layer_data(p1),
    layer_data(p2)
  )


  # fun, fun.min, fun.max
  p3 <- ggplot(dat, aes(x, y)) +
    stat_summary(
      fun = ~ mean(.x),
      fun.min = ~ mean(.x) - sqrt(stats::var(.x) / length(.x)),
      fun.max = ~ mean(.x) + sqrt(stats::var(.x) / length(.x))
    )

  expect_equal(
    layer_data(p1),
    layer_data(p3)
  )

})




test_that("stat_summary_(2d|hex) work with lambda expressions", {

  dat <- data_frame(
    x = c(0, 0, 0, 0, 1, 1, 1, 1),
    y = c(0, 0, 1, 1, 0, 0, 1, 1),
    z = c(1, 1, 2, 2, 2, 2, 3, 3)
  )


  # stat_summary_2d
  p1 <- ggplot(dat, aes(x, y, z = z)) +
    stat_summary_2d(fun = function(x) mean(x))

  p2 <- ggplot(dat, aes(x, y, z = z)) +
    stat_summary_2d(fun = ~ mean(.x))

  expect_equal(
    layer_data(p1),
    layer_data(p2)
  )



  # stat_summary_hex
  # this plot is a bit funky, but easy to reason through
  p1 <- ggplot(dat, aes(x, y, z = z)) +
    stat_summary_hex(fun = function(x) mean(x))

  p2 <- ggplot(dat, aes(x, y, z = z)) +
    stat_summary_hex(fun = ~ mean(.x))

  expect_equal(
    layer_data(p1),
    layer_data(p2)
  )

})