File: test-ggplot-abline.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (48 lines) | stat: -rw-r--r-- 1,382 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


# 'Abline' refers to the line coefficients, as in y = a  b * x

expect_traces <- function(gg, n.traces, name) {
  stopifnot(is.numeric(n.traces))
  L <- expect_doppelganger_built(gg, paste0("cookbook-axes-", name))
  all.traces <- L$data
  no.data <- sapply(all.traces, function(tr) {
    is.null(tr[["x"]]) && is.null(tr[["y"]])
  })
  has.data <- all.traces[!no.data]
  expect_equivalent(length(has.data), n.traces)
  list(data=has.data, layout=L$layout)
}

test_that("Second trace be the a-b line", {
  skip_if_not_installed("ggplot2", "3.4.0") # linewidth introduced in 3.4.0
  
  x <- seq(0, 3.5, by = 0.5)
  y <- x * 0.95
  df <- data.frame(x, y)
  
  gg <- ggplot(df) + geom_point(aes(x, y, size = x)) +
    geom_abline(intercept = 1.1, slope = 0.9, colour = "red", linewidth = 4)
  
  L <- expect_traces(gg, 2, "single-abline")
  
  dat <- L$data[[2]]
  expect_true(dat$x[1] <= 0)
  expect_true(dat$x[2] >= 3.5)
  expect_identical(dat$mode, "lines")
  expect_identical(dat$showlegend, FALSE)
})

test_that("abline aesthetics", {
  df <- data.frame(
    m = c(2, -3, 0.1),
    b = c(1, 0, -1)
  )
  p <- ggplot(df) + xlim(c(-5, 5)) + ylim(c(-5, 5)) +
    geom_abline(aes(intercept = b, slope = m))
  
  L <- expect_traces(p, 1, "multiple-abline")
  expect_identical(range(L$layout$xaxis$tickvals), c(-5, 5))
  expect_identical(range(L$layout$yaxis$tickvals), c(-5, 5))
})