1context("geom_rug")
2
3n = 10
4df <- data_frame(x = 1:n, y = (1:n)^3)
5p <- ggplot(df, aes(x, y)) + geom_point() + geom_rug(sides = 'l')
6
7test_that("coord_flip flips the rugs", {
8  a <- layer_grob(p, 2)
9  b <- layer_grob(p + coord_flip(), 2)
10
11  # Rugs along y-axis, all x coordinates are the same
12  expect_equal(length(a[[1]]$children[[1]]$x0), 1)
13  expect_equal(length(a[[1]]$children[[1]]$x1), 1)
14  expect_equal(length(a[[1]]$children[[1]]$y0), n)
15  expect_equal(length(a[[1]]$children[[1]]$y1), n)
16
17  # Rugs along x-axis, all y coordinates are the same
18  expect_equal(length(b[[1]]$children[[1]]$x0), n)
19  expect_equal(length(b[[1]]$children[[1]]$x1), n)
20  expect_equal(length(b[[1]]$children[[1]]$y0), 1)
21  expect_equal(length(b[[1]]$children[[1]]$y1), 1)
22})
23
24test_that("Rug length needs unit object", {
25  p <- ggplot(df, aes(x,y))
26  expect_error(print(p + geom_rug(length = 0.01)))
27})
28
29test_that("Rug lengths are correct", {
30  a <- layer_grob(p, 2)
31
32  # Check default lengths
33  expect_equal(a[[1]]$children[[1]]$x0, unit(0, "npc"))
34  expect_equal(a[[1]]$children[[1]]$x1, unit(0.03, "npc"))
35
36  p <- ggplot(df, aes(x, y)) + geom_point() + geom_rug(sides = 'l', length = unit(12, "pt"))
37  b <- layer_grob(p, 2)
38
39  # Check default length is changed
40  expect_equal(a[[1]]$children[[1]]$x0, unit(0, "npc"))
41  expect_equal(b[[1]]$children[[1]]$x1, unit(12, "pt"))
42
43})
44
45