1context("scale_discrete")
2
3# Missing values ----------------------------------------------------------
4
5df <- tibble::tibble(
6  x1 = c("a", "b", NA),
7  x2 = factor(x1),
8  x3 = addNA(x2),
9
10  y = 1:3
11)
12
13test_that("NAs are translated/preserved for position scales", {
14  p1a <- ggplot(df, aes(x1, y)) + geom_point()
15  p2a <- ggplot(df, aes(x2, y)) + geom_point()
16  p3a <- ggplot(df, aes(x3, y)) + geom_point()
17
18  expect_equal(layer_data(p1a)$x, new_mapped_discrete(c(1, 2, 3)))
19  expect_equal(layer_data(p2a)$x, new_mapped_discrete(c(1, 2, 3)))
20  expect_equal(layer_data(p3a)$x, new_mapped_discrete(c(1, 2, 3)))
21
22  rm_na_x <- scale_x_discrete(na.translate = FALSE)
23  p1b <- p1a + rm_na_x
24  p2b <- p2a + rm_na_x
25  p3b <- p3a + rm_na_x
26
27  expect_equal(layer_data(p1b)$x, new_mapped_discrete(c(1, 2, NA)))
28  expect_equal(layer_data(p2b)$x, new_mapped_discrete(c(1, 2, NA)))
29  expect_equal(layer_data(p3b)$x, new_mapped_discrete(c(1, 2, NA)))
30})
31
32test_that("NAs are translated/preserved for non-position scales", {
33  p1a <- ggplot(df, aes(y, y, colour = x1)) + geom_point()
34  p2a <- ggplot(df, aes(y, y, colour = x2)) + geom_point()
35  p3a <- ggplot(df, aes(y, y, colour = x3)) + geom_point()
36  expect_equal(layer_data(p1a)$colour, c("#F8766D", "#00BFC4", "grey50"))
37  expect_equal(layer_data(p2a)$colour, c("#F8766D", "#00BFC4", "grey50"))
38  expect_equal(layer_data(p3a)$colour, c("#F8766D", "#00BFC4", "grey50"))
39
40  rm_na_colour <- scale_colour_discrete(na.translate = FALSE)
41  p1b <- p1a + rm_na_colour
42  p2b <- p2a + rm_na_colour
43  p3b <- p3a + rm_na_colour
44  expect_equal(layer_data(p1b)$colour, c("#F8766D", "#00BFC4", NA))
45  expect_equal(layer_data(p2b)$colour, c("#F8766D", "#00BFC4", NA))
46  expect_equal(layer_data(p3b)$colour, c("#F8766D", "#00BFC4", NA))
47})
48
49# Ranges ------------------------------------------------------------------
50
51test_that("discrete ranges also encompass continuous values", {
52  df <- data_frame(x1 = c("a", "b", "c"), x2 = c(0, 2, 4), y = 1:3)
53
54  base <- ggplot(df, aes(y = y)) + scale_x_discrete()
55
56  x_range <- function(x) {
57    layer_scales(x)$x$dimension()
58  }
59
60  expect_equal(x_range(base + geom_point(aes(x1))), c(1, 3))
61  expect_equal(x_range(base + geom_point(aes(x2))), c(0, 4))
62  expect_equal(x_range(base + geom_point(aes(x1)) + geom_point(aes(x2))), c(0, 4))
63})
64
65test_that("discrete ranges have limits even when all values are continuous", {
66  scale <- scale_x_discrete()
67  scale$train(1:3)
68  expect_identical(scale$get_limits(), integer())
69})
70
71test_that("discrete scale shrinks to range when setting limits", {
72  df <- data_frame(x = letters[1:10], y = 1:10)
73  p <- ggplot(df, aes(x, y)) + geom_point() +
74    scale_x_discrete(limits = c("a", "b"))
75
76  expect_equal(layer_scales(p)$x$dimension(c(0, 1)), c(0, 3))
77})
78
79test_that("discrete position scales can accept functional limits", {
80  scale <- scale_x_discrete(limits = rev)
81  scale$train(c("a", "b", "c"))
82  expect_identical(scale$get_limits(), c("c", "b", "a"))
83})
84
85test_that("discrete non-position scales can accept functional limits", {
86  scale <- scale_colour_discrete(limits = rev)
87  scale$train(c("a", "b", "c"))
88  expect_identical(scale$get_limits(), c("c", "b", "a"))
89})
90
91test_that("discrete scale defaults can be set globally", {
92  df <- data_frame(
93    x = 1:4, y = 1:4,
94    two = c("a", "b", "a", "b"),
95    four = c("a", "b", "c", "d")
96  )
97
98  withr::with_options(
99    list(ggplot2.discrete.fill = c("#FFFFFF", "#000000"),
100         ggplot2.discrete.colour = c("#FFFFFF", "#000000")), {
101      # nlevels == ncodes
102      two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
103      expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
104      expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
105
106      # nlevels > ncodes (so should fallback to scale_fill_hue())
107      four_default <- ggplot(df, aes(x, y, colour = four, fill = four)) +
108        geom_point()
109      four_hue <- four_default + scale_fill_hue()
110      expect_equal(layer_data(four_default)$colour, layer_data(four_hue)$colour)
111    }
112  )
113
114  withr::with_options(
115    list(
116      ggplot2.discrete.fill = list(
117        c("#FFFFFF", "#000000"),
118        c("#FF0000", "#00FF00", "#0000FF", "#FF00FF")
119      ),
120      ggplot2.discrete.colour = list(
121        c("#FFFFFF", "#000000"),
122        c("#FF0000", "#00FF00", "#0000FF", "#FF00FF")
123      )
124    ), {
125      # nlevels == 2
126      two <- ggplot(df, aes(x, y, colour = two, fill = two)) + geom_point()
127      expect_equal(layer_data(two)$colour, rep(c("#FFFFFF", "#000000"), 2))
128      expect_equal(layer_data(two)$fill, rep(c("#FFFFFF", "#000000"), 2))
129
130      # nlevels == 4
131      four <- ggplot(df, aes(x, y, colour = four, fill = four)) + geom_point()
132      expect_equal(layer_data(four)$colour, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
133      expect_equal(layer_data(four)$fill, c("#FF0000", "#00FF00", "#0000FF", "#FF00FF"))
134    }
135  )
136})
137
138test_that("Scale is checked in default colour scale", {
139  # Check scale type
140  expect_error(scale_colour_discrete(type = scale_colour_gradient))
141  expect_error(scale_fill_discrete(type = scale_fill_gradient))
142
143  # Check aesthetic
144  expect_error(scale_colour_discrete(type = scale_fill_hue))
145  expect_error(scale_fill_discrete(type = scale_colour_hue))
146})
147
148# mapped_discrete ---------------------------------------------------------
149
150test_that("mapped_discrete vectors behaves as predicted", {
151  expect_null(new_mapped_discrete(NULL))
152  expect_s3_class(new_mapped_discrete(c(0, 3.5)), "mapped_discrete")
153  expect_s3_class(new_mapped_discrete(seq_len(4)), "mapped_discrete")
154  expect_error(new_mapped_discrete(letters))
155
156  x <- new_mapped_discrete(1:10)
157  expect_s3_class(x[2:4], "mapped_discrete")
158  expect_s3_class(c(x, x), "mapped_discrete")
159  x[5:7] <- new_mapped_discrete(seq_len(3))
160  expect_s3_class(x, "mapped_discrete")
161})
162