From 75bcb56eea55d4172c9c20890095cdc2f83c7434 Mon Sep 17 00:00:00 2001 From: thiyangt Date: Mon, 8 Jul 2019 12:19:16 +0200 Subject: [PATCH 1/8] removed repetition in the geom_point document --- R/geom-point.r | 1 - 1 file changed, 1 deletion(-) diff --git a/R/geom-point.r b/R/geom-point.r index 7627c6f940..922b19170a 100644 --- a/R/geom-point.r +++ b/R/geom-point.r @@ -35,7 +35,6 @@ #' often aesthetics, used to set an aesthetic to a fixed value, like #' `colour = "red"` or `size = 3`. They may also be parameters #' to the paired geom/stat. -#' @inheritParams layer #' @export #' @examples #' p <- ggplot(mtcars, aes(wt, mpg)) From 9715b277a8f6f28614581ad66dda7d3ca96dbc51 Mon Sep 17 00:00:00 2001 From: thiyangt Date: Mon, 8 Jul 2019 14:00:56 +0200 Subject: [PATCH 2/8] Improved stat_function() documentation #3362 --- R/stat-function.r | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/stat-function.r b/R/stat-function.r index 89e98d97d2..3b45307595 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -4,13 +4,15 @@ #' The function is called with a grid of evenly spaced values along the x axis, #' and the results are drawn (by default) with a line. #' +#' +#' #' @eval rd_aesthetics("stat", "function") #' @param fun Function to use. Either 1) an anonymous function in the base or #' rlang formula syntax (see [rlang::as_function()]) #' or 2) a quoted or character name referencing a function; see examples. Must #' be vectorised. #' @param n Number of points to interpolate along -#' @param args List of additional arguments to pass to `fun` +#' @param args List of additional arguments passed on to the function defined by `fun`. #' @param xlim Optionally, restrict the range of the function to this range. #' @inheritParams layer #' @inheritParams geom_point From 781b60b48d8627ddfb9393d474dfe41266fd3129 Mon Sep 17 00:00:00 2001 From: thiyangt Date: Mon, 8 Jul 2019 14:01:28 +0200 Subject: [PATCH 3/8] Improved stat_function documentation #3362 --- R/stat-function.r | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/stat-function.r b/R/stat-function.r index 3b45307595..95c4cfe540 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -5,8 +5,6 @@ #' and the results are drawn (by default) with a line. #' #' -#' -#' @eval rd_aesthetics("stat", "function") #' @param fun Function to use. Either 1) an anonymous function in the base or #' rlang formula syntax (see [rlang::as_function()]) #' or 2) a quoted or character name referencing a function; see examples. Must From 8b0f58a7f0cad778925c5528fa383b151a128d14 Mon Sep 17 00:00:00 2001 From: thiyangt Date: Mon, 8 Jul 2019 14:41:54 +0200 Subject: [PATCH 4/8] regenerate stat_function.Rd with devtools::build --- man/stat_function.Rd | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/man/stat_function.Rd b/man/stat_function.Rd index 38793a0577..44c54d7c83 100644 --- a/man/stat_function.Rd +++ b/man/stat_function.Rd @@ -49,7 +49,7 @@ be vectorised.} \item{n}{Number of points to interpolate along} -\item{args}{List of additional arguments to pass to \code{fun}} +\item{args}{List of additional arguments passed on to the function defined by \code{fun}.} \item{na.rm}{If \code{FALSE}, the default, missing values are removed with a warning. If \code{TRUE}, missing values are silently removed.} @@ -70,16 +70,6 @@ This stat makes it easy to superimpose a function on top of an existing plot. The function is called with a grid of evenly spaced values along the x axis, and the results are drawn (by default) with a line. } -\section{Aesthetics}{ - -\code{stat_function()} understands the following aesthetics (required aesthetics are in bold): -\itemize{ -\item \code{group} -\item \code{y} -} -Learn more about setting these aesthetics in \code{vignette("ggplot2-specs")}. -} - \section{Computed variables}{ \describe{ From ed978ab6645e47392b52d91098497ceecc8155ac Mon Sep 17 00:00:00 2001 From: thiyangt Date: Mon, 8 Jul 2019 15:51:28 +0200 Subject: [PATCH 5/8] included functions to warn if the user supplies either the mapping or data --- R/stat-function.r | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/R/stat-function.r b/R/stat-function.r index 95c4cfe540..d12c3a03dc 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -81,6 +81,16 @@ stat_function <- function(mapping = NULL, data = NULL, ... ) ) + + # Warn if supplied mapping and/or data is going to be overwritten + if (!is.null(mapping)) { + warn_overwritten_args("stat_function()", "mapping", "group") + } + if (!is.null(data)) { + warn_overwritten_args("stat_function()", "data", "group") + } + + } #' @rdname ggplot2-ggproto From ab91c1e8decf421c503c5b976acb41b0b311874d Mon Sep 17 00:00:00 2001 From: thiyangt Date: Tue, 9 Jul 2019 05:22:50 +0200 Subject: [PATCH 6/8] included custom warning messages if data and/or mapping is supplied --- R/stat-function.r | 5 ++--- tests/testthat/test-stats-function.r | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/R/stat-function.r b/R/stat-function.r index d12c3a03dc..efb2cdc0c7 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -84,13 +84,12 @@ stat_function <- function(mapping = NULL, data = NULL, # Warn if supplied mapping and/or data is going to be overwritten if (!is.null(mapping)) { - warn_overwritten_args("stat_function()", "mapping", "group") + warning("`mapping` is not used by stat_function()", call. = FALSE) } if (!is.null(data)) { - warn_overwritten_args("stat_function()", "data", "group") + warning("`data` is not used by stat_function()", call. = FALSE) } - } #' @rdname ggplot2-ggproto diff --git a/tests/testthat/test-stats-function.r b/tests/testthat/test-stats-function.r index 2444f4ef99..488a7a2d56 100644 --- a/tests/testthat/test-stats-function.r +++ b/tests/testthat/test-stats-function.r @@ -47,3 +47,15 @@ test_that("works with formula syntax", { expect_equal(ret$x, s) expect_equal(ret$y, s^2) }) + +test_that("can control whether to preserve total or individual width", { + df <- data_frame(x = c("a", "b", "b"), y = c("a", "a", "b")) + + p_total <- ggplot(df, aes(x, fill = y)) + + geom_bar(position = position_dodge(preserve = "total"), width = 1) + p_single <- ggplot(df, aes(x, fill = y)) + + geom_bar(position = position_dodge(preserve = "single"), width = 1) + + expect_equal(layer_data(p_total)$x, c(1, 2.25, 1.75)) + expect_equal(layer_data(p_single)$x, c(0.75, 2.25, 1.75)) +}) From b143ec611eff7745e6aa1947003bb120b8386f65 Mon Sep 17 00:00:00 2001 From: thiyangt Date: Tue, 9 Jul 2019 05:25:24 +0200 Subject: [PATCH 7/8] included unit test for warnings if mapping or data is supplied --- tests/testthat/test-stats-function.r | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-stats-function.r b/tests/testthat/test-stats-function.r index 488a7a2d56..add986d6e2 100644 --- a/tests/testthat/test-stats-function.r +++ b/tests/testthat/test-stats-function.r @@ -48,14 +48,10 @@ test_that("works with formula syntax", { expect_equal(ret$y, s^2) }) -test_that("can control whether to preserve total or individual width", { - df <- data_frame(x = c("a", "b", "b"), y = c("a", "a", "b")) - - p_total <- ggplot(df, aes(x, fill = y)) + - geom_bar(position = position_dodge(preserve = "total"), width = 1) - p_single <- ggplot(df, aes(x, fill = y)) + - geom_bar(position = position_dodge(preserve = "single"), width = 1) +test_that("`mapping` is not used by stat_function()", { + expect_warning(stat_function(aes(), fun = identity), "Ignoring `mapping`") +}) - expect_equal(layer_data(p_total)$x, c(1, 2.25, 1.75)) - expect_equal(layer_data(p_single)$x, c(0.75, 2.25, 1.75)) +test_that("`data` is not used by stat_function()", { + expect_warning(stat_function(aes(), fun = identity), "Ignoring `data`") }) From 6ba927541fcac743e0164c777ed60b27e380721b Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Tue, 9 Jul 2019 10:15:52 +0200 Subject: [PATCH 8/8] Move warnings above `layer()` And fix test of warning messages --- R/stat-function.r | 18 +++++++++--------- tests/testthat/test-stats-function.r | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/stat-function.r b/R/stat-function.r index efb2cdc0c7..8db4c2b64a 100644 --- a/R/stat-function.r +++ b/R/stat-function.r @@ -64,6 +64,15 @@ stat_function <- function(mapping = NULL, data = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { + + # Warn if supplied mapping and/or data is going to be overwritten + if (!is.null(mapping)) { + warning("`mapping` is not used by stat_function()", call. = FALSE) + } + if (!is.null(data)) { + warning("`data` is not used by stat_function()", call. = FALSE) + } + layer( data = data, mapping = mapping, @@ -81,15 +90,6 @@ stat_function <- function(mapping = NULL, data = NULL, ... ) ) - - # Warn if supplied mapping and/or data is going to be overwritten - if (!is.null(mapping)) { - warning("`mapping` is not used by stat_function()", call. = FALSE) - } - if (!is.null(data)) { - warning("`data` is not used by stat_function()", call. = FALSE) - } - } #' @rdname ggplot2-ggproto diff --git a/tests/testthat/test-stats-function.r b/tests/testthat/test-stats-function.r index add986d6e2..7fdc1fd48c 100644 --- a/tests/testthat/test-stats-function.r +++ b/tests/testthat/test-stats-function.r @@ -49,9 +49,9 @@ test_that("works with formula syntax", { }) test_that("`mapping` is not used by stat_function()", { - expect_warning(stat_function(aes(), fun = identity), "Ignoring `mapping`") + expect_warning(stat_function(aes(), fun = identity), "`mapping` is not used") }) test_that("`data` is not used by stat_function()", { - expect_warning(stat_function(aes(), fun = identity), "Ignoring `data`") + expect_warning(stat_function(data = mtcars, fun = identity), "`data` is not used") })