1context("geom_smooth")
2
3test_that("data is ordered by x", {
4  df <- data_frame(x = c(1, 5, 2, 3, 4), y = 1:5)
5
6  ps <- ggplot(df, aes(x, y))+
7    geom_smooth(stat = "identity", se = FALSE)
8
9  expect_equal(layer_data(ps)[c("x", "y")], df[order(df$x), ])
10})
11
12test_that("geom_smooth works in both directions", {
13  p <- ggplot(mpg, aes(displ, hwy)) + geom_smooth()
14  x <- layer_data(p)
15  expect_false(x$flipped_aes[1])
16
17  p <- ggplot(mpg, aes(hwy, displ)) + geom_smooth(orientation = "y")
18  y <- layer_data(p)
19  expect_true(y$flipped_aes[1])
20
21  x$flipped_aes <- NULL
22  y$flipped_aes <- NULL
23  expect_identical(x, flip_data(y, TRUE)[,names(x)])
24})
25
26test_that("default smoothing methods for small and large data sets work", {
27  # Numeric differences on the MLK machine on CRAN makes these test fail
28  # on that particular machine
29  skip_on_cran()
30
31  # test small data set
32  set.seed(6531)
33  x <- rnorm(10)
34  df <- data_frame(
35    x = x,
36    y = x^2 + 0.5 * rnorm(10)
37  )
38
39  m <- loess(y ~ x, data = df, span = 0.75)
40  range <- range(df$x, na.rm = TRUE)
41  xseq <- seq(range[1], range[2], length.out = 80)
42  out <- predict(m, data_frame(x = xseq))
43  p <- ggplot(df, aes(x, y)) + geom_smooth()
44
45  expect_message(
46    plot_data <- layer_data(p),
47    "method = 'loess' and formula 'y ~ x'"
48  )
49  expect_equal(plot_data$y, as.numeric(out))
50
51  # test large data set
52  x <- rnorm(1001) # 1000 is the cutoff point for gam
53  df <- data_frame(
54    x = x,
55    y = x^2 + 0.5 * rnorm(1001)
56  )
57
58  m <- mgcv::gam(y ~ s(x, bs = "cs"), data = df, method = "REML")
59  range <- range(df$x, na.rm = TRUE)
60  xseq <- seq(range[1], range[2], length.out = 80)
61  out <- predict(m, data_frame(x = xseq))
62  p <- ggplot(df, aes(x, y)) + geom_smooth()
63
64  expect_message(
65    plot_data <- layer_data(p),
66    "method = 'gam' and formula 'y ~ s\\(x, bs = \"cs\"\\)"
67  )
68  expect_equal(plot_data$y, as.numeric(out))
69
70  # backwards compatibility of method = "auto"
71  p <- ggplot(df, aes(x, y)) + geom_smooth(method = "auto")
72
73  expect_message(
74    plot_data <- layer_data(p),
75    "method = 'gam' and formula 'y ~ s\\(x, bs = \"cs\"\\)"
76  )
77  expect_equal(plot_data$y, as.numeric(out))
78})
79
80
81# Visual tests ------------------------------------------------------------
82
83test_that("geom_smooth() works with alternative stats", {
84  df <- data_frame(x = c(1, 1, 2, 2, 1, 1, 2, 2),
85                   y = c(1, 2, 2, 3, 2, 3, 1, 2),
86                   fill = c(rep("A", 4), rep("B", 4)))
87
88  expect_doppelganger("ribbon turned on in geom_smooth", {
89    ggplot(df, aes(x, y, color = fill, fill = fill)) +
90      geom_smooth(stat = "summary") # ribbon on by default
91  })
92
93  expect_doppelganger("ribbon turned off in geom_smooth", {
94    ggplot(df, aes(x, y, color = fill, fill = fill)) +
95      geom_smooth(stat = "summary", se = FALSE) # ribbon is turned off via `se = FALSE`
96  })
97})
98