File: test-geom-bar.R

package info (click to toggle)
r-cran-ggplot2 3.5.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 9,944 kB
  • sloc: sh: 15; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 1,323 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
test_that("geom_bar removes bars with parts outside the plot limits", {
  dat <- data_frame(x = c("a", "b", "b", "c", "c", "c"))

  p <- ggplot(dat, aes(x)) + geom_bar()

  expect_warning( # warning created at render stage
    ggplotGrob(p + ylim(0, 2.5)),
    "Removed 1 row containing missing values or values outside the scale range"
  )
})

test_that("geom_bar works in both directions", {
  dat <- data_frame(x = c("a", "b", "b", "c", "c", "c"))

  p <- ggplot(dat, aes(x)) + geom_bar()
  x <- layer_data(p)
  expect_false(x$flipped_aes[1])

  p <- ggplot(dat, aes(y = x)) + geom_bar()
  y <- layer_data(p)
  expect_true(y$flipped_aes[1])

  x$flipped_aes <- NULL
  y$flipped_aes <- NULL
  expect_identical(x, flip_data(y, TRUE))
})

test_that("geom_bar default widths considers panels", {

  dat <- data_frame0(x = c(1:2, 1:2 + 0.1), y = 1,
                    PANEL = factor(rep(1:2, each = 2)))

  layer  <- geom_bar()
  params <- layer$geom_params

  # Default should be panel-wise resolution (0.9), not data-wise resolution (0.1)
  new <- layer$geom$setup_data(dat, params)
  expect_equal(
    new$xmax - new$xmin,
    rep(0.9, 4)
  )

  # Check that default can still be overridden
  params$width <- 0.5
  new <- layer$geom$setup_data(dat, params)
  expect_equal(
    new$xmax - new$xmin,
    rep(0.5, 4)
  )
})