File: test-ggplot-dynamicTicks.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 (106 lines) | stat: -rw-r--r-- 3,052 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# note: test-ggplot-lines.R has some tests for date dynamicTicks

test_that("Discrete axis maps to categorical type", {
  g <- ggplot(mpg, aes(class, color = class)) + geom_bar()
  p <- ggplotly(g, dynamicTicks = "x")
  
  classes <- getLevels(mpg[["class"]])
  
  axisActual <- with(
    p$x$layout$xaxis, list(type, tickmode, categoryorder, categoryarray)
  )
  axisExpect <- list("category", "auto", "array", classes)
  expect_equivalent(axisActual, axisExpect)
  # trace data reflects the "domain" values
  expect_equivalent(
    sort(sapply(p$x$data, "[[", "x")), classes
  )
  
  # y-axis is left as expected
  axisActual <- with(
    p$x$layout$yaxis, list(type, tickmode)
  )
  axisExpect <- list("linear", "array")
  expect_equivalent(axisActual, axisExpect)
})

test_that("Categorical axis reflects custom scale mapping", {
  
  lims <- c("2seater", "suv")
  
  g <- ggplot(mpg, aes(class, color = class)) + 
    geom_bar() +
    scale_x_discrete(limits = lims)
  
  expect_warning(p <- ggplotly(g, dynamicTicks = "x"), 
                 regexp = "non-finite")
  
  axisActual <- with(
    p$x$layout$xaxis, list(type, tickmode, categoryorder, categoryarray)
  )
  axisExpect <- list("category", "auto", "array", lims)
  expect_equivalent(axisActual, axisExpect)
  expect_equivalent(
    sort(sapply(p$x$data, "[[", "x")), sort(lims)
  )
  
  labs <- c("small", "large")
  g <- ggplot(mpg, aes(class, color = class)) + 
    geom_bar() +
    scale_x_discrete(limits = lims, labels = labs)
  expect_warning(p <- ggplotly(g, dynamicTicks = "x"), 
                 regexp = "non-finite")
  
  axisActual <- with(
    p$x$layout$xaxis, list(type, tickmode, categoryorder, categoryarray)
  )
  axisExpect <- list("category", "auto", "array", labs)
  expect_equivalent(axisActual, axisExpect)
  expect_equivalent(
    sort(sapply(p$x$data, "[[", "x")), sort(labs)
  )
  
})

test_that("Time axis inverse transforms correctly", {
  
  d <- data.frame(
    x = seq(Sys.Date(), Sys.Date() + 9, length.out = 10), 
    y = rnorm(10)
  )
  
  l <- ggplotly(ggplot(d, aes(x, y)) + geom_line(), dynamicTicks = TRUE)$x
  
  expect_length(l$data, 1)
  expect_equivalent(l$layout$xaxis$type, "date")
  expect_equivalent(l$layout$xaxis$tickmode, "auto")
  expect_is(l$layout$xaxis$range, "Date")
  expect_equivalent(l$data[[1]][["x"]], d$x)
  
  d2 <- data.frame(
    x = seq(Sys.time(), Sys.time() + 9000, length.out = 10), 
    y = rnorm(10)
  )
  
  l2 <- ggplotly(ggplot(d2, aes(x, y)) + geom_line(), dynamicTicks = TRUE)$x
  
  expect_length(l2$data, 1)
  expect_equivalent(l2$layout$xaxis$type, "date")
  expect_equivalent(l2$layout$xaxis$tickmode, "auto")
  expect_is(l2$layout$xaxis$range, "POSIXt")
  expect_equivalent(l2$data[[1]][["x"]], d2$x)
  
})


test_that("Inverse maps colorbar data", {
  
  p <- ggplot(mpg, aes(hwy, manufacturer)) + 
    stat_bin2d(aes(fill = after_stat(density)), binwidth = c(3,1))
  
  l <- ggplotly(p, dynamicTicks = TRUE)$x
  
  expect_length(l$data, 2)
  expect_true(l$data[[2]]$y %in% unique(mpg$manufacturer))
  
})