Skip to content

Commit 4826838

Browse files
authored
fix stat_function() regression (#4016)
* fix stat_function() regression * fix test
1 parent dbbcd43 commit 4826838

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

R/geom-function.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@
4444
geom_function <- function(mapping = NULL, data = NULL, stat = "function",
4545
position = "identity", ..., na.rm = FALSE,
4646
show.legend = NA, inherit.aes = TRUE) {
47-
# Warn if supplied data is going to be overwritten
48-
if (identical(stat, "function")) {
49-
if (!is.null(data)) {
50-
warn("`data` is not used by stat_function()")
51-
}
47+
if (is.null(data)) {
5248
data <- ensure_nonempty_data
5349
}
5450

R/stat-function.r

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ stat_function <- function(mapping = NULL, data = NULL,
2424
na.rm = FALSE,
2525
show.legend = NA,
2626
inherit.aes = TRUE) {
27-
28-
# Warn if supplied data is going to be overwritten
29-
if (!is.null(data)) {
30-
warn("`data` is not used by stat_function()")
27+
if (is.null(data)) {
28+
data <- ensure_nonempty_data
3129
}
32-
data <- ensure_nonempty_data
3330

3431
layer(
3532
data = data,

tests/testthat/test-stat-function.R

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,34 @@ test_that("Warn when drawing multiple copies of the same function", {
131131
expect_warning(f(), "Multiple drawing groups")
132132
})
133133

134-
test_that("`data` is not used by stat_function()", {
135-
expect_warning(geom_function(data = mtcars, fun = identity), "`data` is not used")
136-
expect_warning(stat_function(data = mtcars, fun = identity), "`data` is not used")
134+
test_that("Line style can be changed via provided data", {
135+
df <- data_frame(fun = "#D55E00")
136+
137+
base <- ggplot(df) +
138+
geom_function(aes(color = fun), fun = identity, n = 6) +
139+
scale_color_identity()
140+
ret <- layer_data(base)
141+
expect_identical(ret$x, seq(0, 1, length.out = 6))
142+
expect_identical(ret$y, ret$x)
143+
expect_identical(ret$colour, rep("#D55E00", 6))
144+
145+
base <- ggplot() +
146+
geom_function(
147+
data = df, aes(color = fun), fun = identity, n = 6
148+
) +
149+
scale_color_identity()
150+
ret <- layer_data(base)
151+
expect_identical(ret$x, seq(0, 1, length.out = 6))
152+
expect_identical(ret$y, ret$x)
153+
expect_identical(ret$colour, rep("#D55E00", 6))
154+
155+
base <- ggplot() +
156+
stat_function(
157+
data = df, aes(color = fun), fun = identity, n = 6
158+
) +
159+
scale_color_identity()
160+
ret <- layer_data(base)
161+
expect_identical(ret$x, seq(0, 1, length.out = 6))
162+
expect_identical(ret$y, ret$x)
163+
expect_identical(ret$colour, rep("#D55E00", 6))
137164
})

0 commit comments

Comments
 (0)