1context("coord_cartesian")
2
3test_that("clipping can be turned off and on", {
4  # clip on by default
5  p <- ggplot() + coord_cartesian()
6  coord <- ggplot_build(p)$layout$coord
7  expect_equal(coord$clip, "on")
8
9  # clip can be turned on and off
10  p <- ggplot() + coord_cartesian(clip = "off")
11  coord <- ggplot_build(p)$layout$coord
12  expect_equal(coord$clip, "off")
13
14  p <- ggplot() + coord_cartesian(clip = "on")
15  coord <- ggplot_build(p)$layout$coord
16  expect_equal(coord$clip, "on")
17})
18
19
20# Visual tests ------------------------------------------------------------
21
22test_that("cartesian coords draws correctly with limits", {
23  p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
24
25  expect_doppelganger("expand range",
26    p + coord_cartesian(xlim = c(0, 10), ylim = c(0, 50))
27  )
28  expect_doppelganger("contract range",
29    p + coord_cartesian(xlim = c(2, 4), ylim = c(20, 40))
30  )
31})
32
33test_that("cartesian coords draws correctly with clipping on or off", {
34  df.in <- data_frame(label = c("inside", "inside", "inside", "inside"),
35                      x = c(0, 1, 0.5, 0.5),
36                      y = c(0.5, 0.5, 0, 1),
37                      angle = c(90, 270, 0, 0),
38                      hjust = c(0.5, 0.5, 0.5, 0.5),
39                      vjust = c(1.1, 1.1, -0.1, 1.1))
40
41  df.out <- data_frame(label = c("outside", "outside", "outside", "outside"),
42                       x = c(0, 1, 0.5, 0.5),
43                       y = c(0.5, 0.5, 0, 1),
44                       angle = c(90, 270, 0, 0),
45                       hjust = c(0.5, 0.5, 0.5, 0.5),
46                       vjust = c(-0.1, -0.1, 1.1, -0.1))
47
48  p <- ggplot(mapping = aes(x, y, label = label, angle = angle, hjust = hjust, vjust = vjust)) +
49    geom_text(data = df.in) +
50    geom_text(data = df.out) +
51    scale_x_continuous(breaks = NULL, name = NULL) +
52    scale_y_continuous(breaks = NULL, name = NULL) +
53    theme(plot.margin = margin(20, 20, 20, 20),
54          panel.spacing = grid::unit(10, "pt"))
55
56  expect_doppelganger("clip on by default, only 'inside' visible",
57    p + coord_cartesian(xlim = c(0, 1), ylim = c(0, 1), expand = FALSE)
58  )
59
60  expect_doppelganger("clip turned off, both 'inside' and 'outside' visible",
61    p + coord_cartesian(xlim = c(0, 1), ylim = c(0, 1), expand = FALSE, clip = "off")
62  )
63})
64