diff --git a/R/geom-function.R b/R/geom-function.R index 6e2ea95336..5cc1399945 100644 --- a/R/geom-function.R +++ b/R/geom-function.R @@ -44,11 +44,7 @@ geom_function <- function(mapping = NULL, data = NULL, stat = "function", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { - # Warn if supplied data is going to be overwritten - if (identical(stat, "function")) { - if (!is.null(data)) { - warn("`data` is not used by stat_function()") - } + if (is.null(data)) { data <- ensure_nonempty_data } diff --git a/R/stat-function.r b/R/stat-function.r index c4d1b504ec..99bd7137e0 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -24,12 +24,9 @@ stat_function <- function(mapping = NULL, data = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { - - # Warn if supplied data is going to be overwritten - if (!is.null(data)) { - warn("`data` is not used by stat_function()") + if (is.null(data)) { + data <- ensure_nonempty_data } - data <- ensure_nonempty_data layer( data = data, diff --git a/tests/testthat/test-stat-function.R b/tests/testthat/test-stat-function.R index 46c4176c1e..b965876c0c 100644 --- a/tests/testthat/test-stat-function.R +++ b/tests/testthat/test-stat-function.R @@ -131,7 +131,34 @@ test_that("Warn when drawing multiple copies of the same function", { expect_warning(f(), "Multiple drawing groups") }) -test_that("`data` is not used by stat_function()", { - expect_warning(geom_function(data = mtcars, fun = identity), "`data` is not used") - expect_warning(stat_function(data = mtcars, fun = identity), "`data` is not used") +test_that("Line style can be changed via provided data", { + df <- data_frame(fun = "#D55E00") + + base <- ggplot(df) + + geom_function(aes(color = fun), fun = identity, n = 6) + + scale_color_identity() + ret <- layer_data(base) + expect_identical(ret$x, seq(0, 1, length.out = 6)) + expect_identical(ret$y, ret$x) + expect_identical(ret$colour, rep("#D55E00", 6)) + + base <- ggplot() + + geom_function( + data = df, aes(color = fun), fun = identity, n = 6 + ) + + scale_color_identity() + ret <- layer_data(base) + expect_identical(ret$x, seq(0, 1, length.out = 6)) + expect_identical(ret$y, ret$x) + expect_identical(ret$colour, rep("#D55E00", 6)) + + base <- ggplot() + + stat_function( + data = df, aes(color = fun), fun = identity, n = 6 + ) + + scale_color_identity() + ret <- layer_data(base) + expect_identical(ret$x, seq(0, 1, length.out = 6)) + expect_identical(ret$y, ret$x) + expect_identical(ret$colour, rep("#D55E00", 6)) })