File: test-scale-date.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 (70 lines) | stat: -rw-r--r-- 2,070 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
base_time <- function(tz = "") {
  as.POSIXct(strptime("2015-06-01", "%Y-%m-%d", tz = tz))
}

df <- data_frame(
  time1 = base_time("") + 0:6 * 3600,
  time2 = base_time("UTC") + 0:6 * 3600,
  time3 = base_time("Australia/Lord_Howe") + (0:6 + 13) * 3600, # has half hour offset
  y = seq_along(base_time)
)

test_that("inherits timezone from data", {
  if (!is.null(attr(df$time1, "tzone")))
     skip("Local time zone not available")

  # Local time
  p <- ggplot(df, aes(y = y)) + geom_point(aes(time1))
  sc <- layer_scales(p)$x

  expect_true(identical(sc$timezone, NULL))
  expect_equal(sc$get_labels()[1], "00:00")

  # UTC
  p <- ggplot(df, aes(y = y)) + geom_point(aes(time2))
  sc <- layer_scales(p)$x
  expect_equal(sc$timezone, "UTC")
  expect_equal(sc$get_labels()[1], "00:00")
})


test_that("first timezone wins", {
  p <- ggplot(df, aes(y = y)) +
    geom_point(aes(time2)) +
    geom_point(aes(time3), colour = "red") +
    scale_x_datetime(date_breaks = "hour", date_labels = "%H:%M")
  sc <- layer_scales(p)$x
  expect_equal(sc$timezone, "UTC")
})

test_that("not cached across calls", {
  scale_x <- scale_x_datetime(date_breaks = "hour", date_labels = "%H:%M")

  p1 <- ggplot(df, aes(y = y)) + geom_point(aes(time2)) + scale_x
  p2 <- ggplot(df, aes(y = y)) + geom_point(aes(time3)) + scale_x

  expect_equal(layer_scales(p1)$x$timezone, "UTC")
  expect_equal(layer_scales(p2)$x$timezone, "Australia/Lord_Howe")
})

test_that("datetime size scales work", {
  p <- ggplot(df, aes(y = y)) + geom_point(aes(time1, size = time1))

  # Default size range is c(1, 6)
  expect_equal(range(layer_data(p)$size), c(1, 6))
})

test_that("datetime alpha scales work", {
  p <- ggplot(df, aes(y = y)) + geom_point(aes(time1, alpha = time1))

  # Default alpha range is c(0.1, 1.0)
  expect_equal(range(layer_data(p)$alpha), c(0.1, 1.0))
})

test_that("datetime colour scales work", {
  p <- ggplot(df, aes(y = y)) +
    geom_point(aes(time1, colour = time1)) +
    scale_colour_datetime()

  expect_equal(range(layer_data(p)$colour), c("#132B43", "#56B1F7"))
})