1context("Labels")
2
3test_that("setting guide labels works", {
4
5    expect_identical(xlab("my label")$x, "my label")
6    expect_identical(labs(x = "my label")$x, "my label")
7
8    expect_identical(ylab("my label")$y, "my label")
9    expect_identical(labs(y = "my label")$y, "my label")
10
11    # Plot titles
12    expect_identical(labs(title = "my title")$title, "my title")
13    expect_identical(labs(title = "my title",
14                          subtitle = "my subtitle")$subtitle, "my subtitle")
15
16    # whole plot annotations
17    expect_identical(labs(caption = "my notice")$caption, "my notice")
18    expect_identical(labs(title = "my title",
19                          caption = "my notice")$caption, "my notice")
20    expect_identical(labs(tag = "A)")$tag, "A)")
21    expect_identical(labs(title = "my title",
22                          tag = "A)")$tag, "A)")
23
24    # Colour
25    expect_identical(labs(colour = "my label")$colour, "my label")
26    # American spelling
27    expect_identical(labs(color = "my label")$colour, "my label")
28
29    # No extra elements exists
30    expect_equivalent(labs(title = "my title"), list(title = "my title"))     # formal argument
31    expect_equivalent(labs(colour = "my label"), list(colour = "my label"))   # dot
32    expect_equivalent(labs(foo = "bar"), list(foo = "bar"))                   # non-existent param
33
34    # labs() has list-splicing semantics
35    params <- list(title = "my title", tag = "A)")
36    expect_identical(labs(!!!params)$tag, "A)")
37
38    # NULL is preserved
39    expect_equivalent(labs(title = NULL), list(title = NULL))
40
41    # ggtitle works in the same way as labs()
42    expect_identical(ggtitle("my title")$title, "my title")
43    expect_identical(
44      ggtitle("my title", subtitle = "my subtitle")$subtitle,
45      "my subtitle"
46    )
47    expect_equivalent(
48      ggtitle("my title", subtitle = NULL),
49      list(title = "my title", subtitle = NULL)
50    )
51})
52
53test_that("Labels from default stat mapping are overwritten by default labels", {
54  p <- ggplot(mpg, aes(displ, hwy)) +
55    geom_density2d()
56
57  expect_equal(p$labels$colour[1], "colour")
58  expect_true(attr(p$labels$colour, "fallback"))
59
60  p <- p + geom_smooth(aes(color = drv))
61
62  expect_equal(p$labels$colour, "drv")
63})
64
65test_that("alt text is returned", {
66  p <- ggplot(mtcars, aes(mpg, disp)) +
67    geom_point()
68  expect_equal(get_alt_text(p), "")
69  p <- p + labs(alt = "An alt text")
70  expect_equal(get_alt_text(p), "An alt text")
71})
72
73
74# Visual tests ------------------------------------------------------------
75
76test_that("tags are drawn correctly", {
77  dat <- data_frame(x = 1:10, y = 10:1)
78  p <- ggplot(dat, aes(x = x, y = y)) + geom_point() + labs(tag = "Fig. A)")
79
80  expect_doppelganger("defaults", p)
81  expect_doppelganger("Other position", p + theme(plot.tag.position = 'bottom'))
82  expect_doppelganger("Manual", p + theme(plot.tag.position = c(0.05, 0.05)))
83})
84