File: test-ggplot-path.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 (79 lines) | stat: -rw-r--r-- 2,998 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
73
74
75
76
77
78
79


test_that("lines are different from paths", {
  df <- data.frame(
    x = c(1, 3, 2),
    y = c(0, 0, 1)
  )
  p <- qplot(x, y, data = df, geom = "path")
  info <- expect_doppelganger_built(p, "path-lines-diff-from-paths")
  expect_identical(info$data[[1]]$x[1:3], c(1, 3, 2))
  expect_identical(info$data[[1]]$y[1:3], c(0, 0, 1))
})

two.paths <- data.frame(
  x = c(1, 2, 1, 2),
  y = c(1, 1, 2, 2)
)

test_that("paths with different colors become different traces", {
  ## Numeric color.
  gg <- ggplot() +
    geom_path(aes(x, y, group = y, color = y), data = two.paths)
  info <- expect_doppelganger_built(gg, "path-colors")
  # one trace is for the colorbar
  expect_equivalent(length(info$data), 3)
  expect_identical(info$data[[1]]$x[1:2], c(1,2))
  expect_identical(info$data[[2]]$x[1:2], c(1,2))
  expect_identical(info$data[[1]]$y[1:2], c(1,1))
  expect_identical(info$data[[2]]$y[1:2], c(2,2))
  ## Categorical color.
  gg <- ggplot() +
    geom_path(aes(x, y, group = y, color = paste0("FOO", y)), data = two.paths)
  info <- expect_doppelganger_built(gg, "path-colors2")
  expect_equivalent(length(info$data), 2)
  expect_identical(info$data[[1]]$x[1:2], c(1,2))
  expect_identical(info$data[[2]]$x[1:2], c(1,2))
  expect_identical(info$data[[1]]$y[1:2], c(1,1))
  expect_identical(info$data[[2]]$y[1:2], c(2,2))
})

four.paths <- rbind(
  data.frame(two.paths, g = "positive", stringsAsFactors = TRUE),
  data.frame(-two.paths, g = "negative", stringsAsFactors = TRUE)
)

test_that("paths with the same color but different groups stay together", {
  gg <- ggplot() +
    geom_path(aes(x, y, group = y, color = g), data = four.paths)
  info <- expect_doppelganger_built(gg, "path-colored-groups-stay-together")
  expect_equivalent(length(info$data), 2)
  expect_identical(info$data[[1]]$name, "positive")
  expect_identical(info$data[[2]]$name, "negative")
  expect_true(any(is.na(info$data[[1]]$x)))
  expect_true(any(is.na(info$data[[1]]$y)))
  expect_true(any(is.na(info$data[[2]]$x)))
  expect_true(any(is.na(info$data[[2]]$y)))
})

test_that("lines & points are merged into markers+lines traces", {
  df1 <- data.frame(
    sex = factor(c("Female", "Female", "Male", "Male")),
    time = factor(c("Lunch", "Dinner", "Lunch", "Dinner"),
                  levels = c("Lunch", "Dinner")),
    total_bill = c(13.53, 16.81, 16.24, 17.42)
  )
  gg <- ggplot(data = df1, aes(x=time, y=total_bill, group=sex, shape=sex)) +
    geom_line() +
    geom_point()
  info <- expect_doppelganger_built(gg, "path-line-symbols")
  expect_equivalent(length(info$data), 2)  # 2 traces
  expect_equivalent(info$data[[1]]$name, "Female")
  expect_equivalent(info$data[[1]]$marker$symbol, "circle")
  expect_equivalent(info$data[[2]]$name, "Male")
  expect_equivalent(info$data[[2]]$marker$symbol, "triangle-up")
  expect_match(info$data[[1]]$mode, "lines")
  expect_match(info$data[[1]]$mode, "markers")
  expect_match(info$data[[2]]$mode, "lines")
  expect_match(info$data[[2]]$mode, "markers")
})