File: test-stat-bin2d.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 (49 lines) | stat: -rw-r--r-- 1,694 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
test_that("binwidth is respected", {
  df <- data_frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2))
  base <- ggplot(df, aes(x, y)) +
    stat_bin2d(geom = "tile", binwidth = 0.25)

  out <- layer_data(base)
  expect_equal(nrow(out), 2)
  # Adjust tolerance to account for fuzzy breaks adjustment
  expect_equal(out$xmin, c(1, 1.75), tolerance = 1e-7)
  expect_equal(out$xmax, c(1.25, 2), tolerance = 1e-7)

  p <- ggplot(df, aes(x, y)) +
    stat_bin2d(geom = "tile", binwidth = c(0.25, 0.5, 0.75))
  expect_snapshot_warning(ggplot_build(p))

  p <- ggplot(df, aes(x, y)) +
    stat_bin2d(geom = "tile", origin = c(0.25, 0.5, 0.75))
  expect_snapshot_warning(ggplot_build(p))
})

test_that("breaks override binwidth", {
  # Test explicitly setting the breaks for x, overriding
  # the binwidth.
  integer_breaks <- (0:4) - 0.5  # Will use for x
  half_breaks <- seq(0, 3.5, 0.5)  # Will test against this for y

  df <- data_frame(x = 0:3, y = 0:3)
  base <- ggplot(df, aes(x, y)) +
    stat_bin2d(
      breaks = list(x = integer_breaks, y = NULL),
      binwidth = c(0.5, 0.5)
    )

  out <- layer_data(base)
  expect_equal(out$xbin, cut(df$x, adjust_breaks(integer_breaks), include.lowest = TRUE, labels = FALSE))
  expect_equal(out$ybin, cut(df$y, adjust_breaks(half_breaks), include.lowest = TRUE, labels = FALSE))
})

test_that("breaks are transformed by the scale", {
  df <- data_frame(x = c(1, 10, 100, 1000), y = 0:3)
  base <- ggplot(df, aes(x, y)) +
    stat_bin_2d(
      breaks = list(x = c(5, 50, 500), y = c(0.5, 1.5, 2.5)))

  out1 <- layer_data(base)
  out2 <- layer_data(base + scale_x_log10())
  expect_equal(out1$x, c(27.5, 275))
  expect_equal(out2$x, c(1.19897, 2.19897))
})