1context("Aesthetics (grouping)")
2
3df <- data_frame(
4  x = 1:4,
5  a = c("a", "a", "b", "b"),
6  b = c("a", "b", "a", "b")
7)
8
9group <- function(x) as.vector(layer_data(x, 1)$group)
10groups <- function(x) length(unique(group(x)))
11
12
13test_that("one group per combination of discrete vars", {
14  plot <- ggplot(df, aes(x, x)) + geom_point()
15  expect_equal(group(plot), rep(NO_GROUP, 4))
16
17  plot <- ggplot(df, aes(x, a)) + geom_point()
18  expect_equal(group(plot), c(1, 1, 2, 2))
19  plot <- ggplot(df, aes(x, b)) + geom_point()
20  expect_equal(group(plot), c(1, 2, 1, 2))
21
22  plot <- ggplot(df, aes(a, b)) + geom_point()
23  expect_equal(groups(plot), 4)
24})
25
26test_that("no error for aes(groupS)", {
27  df2 <- data_frame(x = df$a, y = df$b, groupS = 1)
28  g <- add_group(df2)
29
30  expect_equal(nrow(g), nrow(df2))
31  expect_equal(names(g), c("x", "y", "groupS", "group"))
32})
33
34test_that("label is not used as a grouping var", {
35  plot <- ggplot(df, aes(x, x, label = a)) + geom_point()
36  expect_equal(group(plot), rep(NO_GROUP, 4))
37
38  plot <- ggplot(df, aes(x, x, colour = a, label = b)) + geom_point()
39  expect_equal(group(plot), c(1, 1, 2, 2))
40})
41
42test_that("group aesthetic overrides defaults", {
43  plot <- ggplot(df, aes(x, x, group = x)) + geom_point()
44  expect_equal(groups(plot), 4)
45
46  plot <- ggplot(df, aes(a, b, group = 1)) + geom_point()
47  expect_equal(groups(plot), 1)
48})
49
50test_that("group param overrides defaults", {
51  plot <- ggplot(df, aes(a, b)) + geom_point(group = 1)
52  expect_equal(groups(plot), 1)
53})
54