diff --git a/R/layers2traces.R b/R/layers2traces.R
index 0387d34c5c..7bacb539d5 100644
--- a/R/layers2traces.R
+++ b/R/layers2traces.R
@@ -170,6 +170,12 @@ to_basic <- function(data, prestats_data, layout, params, p, ...) {
UseMethod("to_basic")
}
+#' @export
+to_basic.GeomFunction <- function (data, prestats_data, layout, params, p, ...) {
+ data$y <- params$fun(data$x)
+ prefix_class(data, "GeomPath")
+}
+
#' @export
to_basic.GeomCol <- function(data, prestats_data, layout, params, p, ...) {
prefix_class(data, "GeomBar")
diff --git a/tests/testthat/_snaps/ggplot-function/geomfunction.svg b/tests/testthat/_snaps/ggplot-function/geomfunction.svg
new file mode 100644
index 0000000000..7c6eebb138
--- /dev/null
+++ b/tests/testthat/_snaps/ggplot-function/geomfunction.svg
@@ -0,0 +1 @@
+
diff --git a/tests/testthat/_snaps/ggplot-function/stat-function.svg b/tests/testthat/_snaps/ggplot-function/stat-function.svg
new file mode 100644
index 0000000000..fe783e2eb8
--- /dev/null
+++ b/tests/testthat/_snaps/ggplot-function/stat-function.svg
@@ -0,0 +1 @@
+
diff --git a/tests/testthat/test-ggplot-function.R b/tests/testthat/test-ggplot-function.R
new file mode 100644
index 0000000000..738260f21d
--- /dev/null
+++ b/tests/testthat/test-ggplot-function.R
@@ -0,0 +1,18 @@
+p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
+
+test_that("ggplotly correctly handles stat_function", {
+ pl <- p +
+ stat_function(fun = function(x) x^2+3) +
+ xlim(1, 5)
+
+ expect_doppelganger(ggplotly(pl), "stat-function")
+})
+
+test_that("ggplotly correctly handles stat_function", {
+ pl <- p +
+ geom_function(fun = function(x) x^2+3) +
+ stat_function(fun = function(x) 5) +
+ xlim(1, 5)
+
+ expect_doppelganger(ggplotly(pl), "geomfunction")
+})
\ No newline at end of file