From 7892d0d87a2312728efafdfaeb7ff43f59d7ae2e Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 1 Nov 2017 17:23:16 -0300 Subject: [PATCH 1/4] `breaks` argument in `stat_contour()` accepts function. --- NAMESPACE | 1 + R/geom-contour.r | 15 ++++++++++++++ R/stat-contour.r | 48 ++++++++++++++++++++++++++++++++++----------- man/geom_contour.Rd | 25 ++++++++++++++++++++--- 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4ee9a7336b..b856fdc4cc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -219,6 +219,7 @@ export(autolayer) export(autoplot) export(benchplot) export(borders) +export(breaks_default) export(calc_element) export(combine_vars) export(continuous_scale) diff --git a/R/geom-contour.r b/R/geom-contour.r index 92fe17d8f5..1f719e8f57 100644 --- a/R/geom-contour.r +++ b/R/geom-contour.r @@ -14,6 +14,8 @@ #' @inheritParams layer #' @inheritParams geom_point #' @inheritParams geom_path +#' @inheritParams stat_contour +#' #' @seealso [geom_density_2d()]: 2d density contours #' @export #' @export @@ -36,6 +38,13 @@ #' v + geom_contour(binwidth = 0.01) #' v + geom_contour(binwidth = 0.001) #' +#' # Passing your own function to breaks +#' my_breaks <- function(range) { +#' b <- ggplot2::breaks_default(binwidth = 0.001, NULL)(range) +#' b[b != 0.004] +#' } +#' v + geom_contour(breaks = my_breaks) +#' #' # Other parameters #' v + geom_contour(aes(colour = ..level..)) #' v + geom_contour(colour = "red") @@ -48,6 +57,9 @@ geom_contour <- function(mapping = NULL, data = NULL, lineend = "butt", linejoin = "round", linemitre = 1, + breaks = waiver(), + bins = NULL, + binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { @@ -63,6 +75,9 @@ geom_contour <- function(mapping = NULL, data = NULL, lineend = lineend, linejoin = linejoin, linemitre = linemitre, + breaks = breaks, + bins = bins, + binwidth = binwidth, na.rm = na.rm, ... ) diff --git a/R/stat-contour.r b/R/stat-contour.r index a421973251..2a9ce74d21 100644 --- a/R/stat-contour.r +++ b/R/stat-contour.r @@ -1,4 +1,10 @@ #' @inheritParams stat_identity +#' @param breaks One of: +#' - A numeric vector of breaks +#' - A function that takes the range of the data as input and returns breaks +#' as output +#' @param bins Number of evenly spaced breaks. +#' @param binwidth Distance between breaks. #' @export #' @section Computed variables: #' \describe{ @@ -8,6 +14,9 @@ stat_contour <- function(mapping = NULL, data = NULL, geom = "contour", position = "identity", ..., + breaks = waiver(), + bins = NULL, + binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) { @@ -21,6 +30,9 @@ stat_contour <- function(mapping = NULL, data = NULL, inherit.aes = inherit.aes, params = list( na.rm = na.rm, + breaks = breaks, + bins = bins, + binwidth = binwidth, ... ) ) @@ -35,18 +47,13 @@ StatContour <- ggproto("StatContour", Stat, default_aes = aes(order = ..level..), compute_group = function(data, scales, bins = NULL, binwidth = NULL, - breaks = NULL, complete = FALSE, na.rm = FALSE) { - # If no parameters set, use pretty bins - if (is.null(bins) && is.null(binwidth) && is.null(breaks)) { - breaks <- pretty(range(data$z), 10) - } - # If provided, use bins to calculate binwidth - if (!is.null(bins)) { - binwidth <- diff(range(data$z)) / bins + breaks = waiver(), complete = FALSE, na.rm = FALSE) { + # Check is.null(breaks) for backwards compatibility + if (is.waive(breaks) | is.null(breaks)) { + breaks <- breaks_default(binwidth, bins) } - # If necessary, compute breaks from binwidth - if (is.null(breaks)) { - breaks <- fullseq(range(data$z), binwidth) + if (is.function(breaks)) { + breaks <- breaks(range(data$z)) } contour_lines(data, breaks, complete = complete) @@ -116,3 +123,22 @@ poly_dir <- function(x, y) { # geom_path(aes(group = piece, colour = factor(dir))) # last_plot() + facet_wrap(~ level) + +#' @export +breaks_default <- function(binwidth, bins) { + function(range) { + # If no parameters set, use pretty bins + if (is.null(bins) && is.null(binwidth)) { + breaks <- pretty(range, 10) + } + # If provided, use bins to calculate binwidth + if (!is.null(bins)) { + binwidth <- diff(range) / bins + } + # If necessary, compute breaks from binwidth + if(!is.null(binwidth)) { + breaks <- fullseq(range, binwidth) + } + return(breaks) + } +} diff --git a/man/geom_contour.Rd b/man/geom_contour.Rd index 1172f11314..03f186d03f 100644 --- a/man/geom_contour.Rd +++ b/man/geom_contour.Rd @@ -7,11 +7,12 @@ \usage{ geom_contour(mapping = NULL, data = NULL, stat = "contour", position = "identity", ..., lineend = "butt", linejoin = "round", - linemitre = 1, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) + linemitre = 1, breaks = waiver(), bins = NULL, binwidth = NULL, + na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) stat_contour(mapping = NULL, data = NULL, geom = "contour", - position = "identity", ..., na.rm = FALSE, show.legend = NA, - inherit.aes = TRUE) + position = "identity", ..., breaks = waiver(), bins = NULL, + binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) } \arguments{ \item{mapping}{Set of aesthetic mappings created by \code{\link[=aes]{aes()}} or @@ -50,6 +51,17 @@ to the paired geom/stat.} \item{linemitre}{Line mitre limit (number greater than 1)} +\item{breaks}{One of: +\itemize{ +\item A numeric vector of breaks +\item A function that takes the range of the data as input and returns breaks +as output +}} + +\item{bins}{Number of evenly spaced breaks.} + +\item{binwidth}{Distance between breaks.} + \item{na.rm}{If \code{FALSE}, the default, missing values are removed with a warning. If \code{TRUE}, missing values are silently removed.} @@ -106,6 +118,13 @@ v + geom_contour(bins = 10) v + geom_contour(binwidth = 0.01) v + geom_contour(binwidth = 0.001) +# Passing your own function to breaks +my_breaks <- function(range) { + b <- ggplot2::breaks_default(binwidth = 0.001, NULL)(range) + b[b != 0.004] +} +v + geom_contour(breaks = my_breaks) + # Other parameters v + geom_contour(aes(colour = ..level..)) v + geom_contour(colour = "red") From 4ac0f5216c3d7d47f4789cc217fc3a7d82caf946 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 3 Nov 2017 12:10:51 -0300 Subject: [PATCH 2/4] added `binwidth` and `bins` to arguments passed to breaks --- R/geom-contour.r | 6 ++--- R/stat-contour.r | 64 ++++++++++++++++++++++----------------------- man/geom_contour.Rd | 12 ++++----- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/R/geom-contour.r b/R/geom-contour.r index 1f719e8f57..0eaa1a58f3 100644 --- a/R/geom-contour.r +++ b/R/geom-contour.r @@ -39,8 +39,8 @@ #' v + geom_contour(binwidth = 0.001) #' #' # Passing your own function to breaks -#' my_breaks <- function(range) { -#' b <- ggplot2::breaks_default(binwidth = 0.001, NULL)(range) +#' my_breaks <- function(range, binwidth, bins) { +#' b <- ggplot2::breaks_default(range, binwidth, bins) #' b[b != 0.004] #' } #' v + geom_contour(breaks = my_breaks) @@ -57,7 +57,7 @@ geom_contour <- function(mapping = NULL, data = NULL, lineend = "butt", linejoin = "round", linemitre = 1, - breaks = waiver(), + breaks = breaks_default, bins = NULL, binwidth = NULL, na.rm = FALSE, diff --git a/R/stat-contour.r b/R/stat-contour.r index 2a9ce74d21..fee477dd1c 100644 --- a/R/stat-contour.r +++ b/R/stat-contour.r @@ -1,8 +1,8 @@ #' @inheritParams stat_identity #' @param breaks One of: #' - A numeric vector of breaks -#' - A function that takes the range of the data as input and returns breaks -#' as output +#' - A function that takes the range of the data, bins and binwidth as input +#' and returns breaks as output #' @param bins Number of evenly spaced breaks. #' @param binwidth Distance between breaks. #' @export @@ -14,7 +14,7 @@ stat_contour <- function(mapping = NULL, data = NULL, geom = "contour", position = "identity", ..., - breaks = waiver(), + breaks = breaks_default, bins = NULL, binwidth = NULL, na.rm = FALSE, @@ -43,21 +43,23 @@ stat_contour <- function(mapping = NULL, data = NULL, #' @usage NULL #' @export StatContour <- ggproto("StatContour", Stat, - required_aes = c("x", "y", "z"), - default_aes = aes(order = ..level..), + required_aes = c("x", "y", "z"), + default_aes = aes(order = ..level..), - compute_group = function(data, scales, bins = NULL, binwidth = NULL, - breaks = waiver(), complete = FALSE, na.rm = FALSE) { - # Check is.null(breaks) for backwards compatibility - if (is.waive(breaks) | is.null(breaks)) { - breaks <- breaks_default(binwidth, bins) - } - if (is.function(breaks)) { - breaks <- breaks(range(data$z)) - } + compute_group = function(data, scales, bins = NULL, binwidth = NULL, + breaks = breaks_default, + complete = FALSE, na.rm = FALSE) { + # Check is.null(breaks) for backwards compatibility + if (is.null(breaks)) { + breaks <- breaks_default + } + if (is.function(breaks)) { + breaks <- breaks(range(data$z), bins = bins, + binwidth = binwidth) + } - contour_lines(data, breaks, complete = complete) - } + contour_lines(data, breaks, complete = complete) + } ) @@ -75,7 +77,7 @@ contour_lines <- function(data, breaks, complete = FALSE) { if (is.list(z)) { stop("Contour requires single `z` at each combination of `x` and `y`.", - call. = FALSE) + call. = FALSE) } cl <- grDevices::contourLines( @@ -125,20 +127,18 @@ poly_dir <- function(x, y) { #' @export -breaks_default <- function(binwidth, bins) { - function(range) { - # If no parameters set, use pretty bins - if (is.null(bins) && is.null(binwidth)) { - breaks <- pretty(range, 10) - } - # If provided, use bins to calculate binwidth - if (!is.null(bins)) { - binwidth <- diff(range) / bins - } - # If necessary, compute breaks from binwidth - if(!is.null(binwidth)) { - breaks <- fullseq(range, binwidth) - } - return(breaks) +breaks_default <- function(range, binwidth, bins) { + # If no parameters set, use pretty bins + if (is.null(bins) && is.null(binwidth)) { + breaks <- pretty(range, 10) + } + # If provided, use bins to calculate binwidth + if (!is.null(bins)) { + binwidth <- diff(range) / bins + } + # If necessary, compute breaks from binwidth + if(!is.null(binwidth)) { + breaks <- fullseq(range, binwidth) } + return(breaks) } diff --git a/man/geom_contour.Rd b/man/geom_contour.Rd index 03f186d03f..61527665b9 100644 --- a/man/geom_contour.Rd +++ b/man/geom_contour.Rd @@ -7,11 +7,11 @@ \usage{ geom_contour(mapping = NULL, data = NULL, stat = "contour", position = "identity", ..., lineend = "butt", linejoin = "round", - linemitre = 1, breaks = waiver(), bins = NULL, binwidth = NULL, + linemitre = 1, breaks = breaks_default, bins = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) stat_contour(mapping = NULL, data = NULL, geom = "contour", - position = "identity", ..., breaks = waiver(), bins = NULL, + position = "identity", ..., breaks = breaks_default, bins = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) } \arguments{ @@ -54,8 +54,8 @@ to the paired geom/stat.} \item{breaks}{One of: \itemize{ \item A numeric vector of breaks -\item A function that takes the range of the data as input and returns breaks -as output +\item A function that takes the range of the data, bins and binwidth as input +and returns breaks as output }} \item{bins}{Number of evenly spaced breaks.} @@ -119,8 +119,8 @@ v + geom_contour(binwidth = 0.01) v + geom_contour(binwidth = 0.001) # Passing your own function to breaks -my_breaks <- function(range) { - b <- ggplot2::breaks_default(binwidth = 0.001, NULL)(range) +my_breaks <- function(range, binwidth, bins) { + b <- ggplot2::breaks_default(range, binwidth, bins) b[b != 0.004] } v + geom_contour(breaks = my_breaks) From 6ad9d4512819dcaa50d892c7e4539d7c08a91d0c Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 3 Nov 2017 12:17:54 -0300 Subject: [PATCH 3/4] Documented `breaks_default()` --- R/stat-contour.r | 11 +++++++++-- man/breaks_default.Rd | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 man/breaks_default.Rd diff --git a/R/stat-contour.r b/R/stat-contour.r index fee477dd1c..6534a4efb9 100644 --- a/R/stat-contour.r +++ b/R/stat-contour.r @@ -125,9 +125,16 @@ poly_dir <- function(x, y) { # geom_path(aes(group = piece, colour = factor(dir))) # last_plot() + facet_wrap(~ level) - +#' Default breaks +#' +#' Default behaviour for computing breaks in [stat_contour()]. +#' +#' @param range The range of the data +#' @param bins Number of evenly spaced breaks. +#' @param binwidth Distance between breaks. +#' #' @export -breaks_default <- function(range, binwidth, bins) { +breaks_default <- function(range, bins, binwidth) { # If no parameters set, use pretty bins if (is.null(bins) && is.null(binwidth)) { breaks <- pretty(range, 10) diff --git a/man/breaks_default.Rd b/man/breaks_default.Rd new file mode 100644 index 0000000000..56654f87d8 --- /dev/null +++ b/man/breaks_default.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/stat-contour.r +\name{breaks_default} +\alias{breaks_default} +\title{Default breaks} +\usage{ +breaks_default(range, bins, binwidth) +} +\arguments{ +\item{range}{The range of the data} + +\item{bins}{Number of evenly spaced breaks.} + +\item{binwidth}{Distance between breaks.} +} +\description{ +Default behaviour for computing breaks in \code{\link[=stat_contour]{stat_contour()}}. +} From 59046339a3c87c47904a4764fb824fec9d329d86 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 15 Nov 2017 17:09:50 -0300 Subject: [PATCH 4/4] `breaks` default is fullseq --- NAMESPACE | 1 - R/geom-contour.r | 2 +- R/stat-contour.r | 67 ++++++++++++++++--------------------------- man/breaks_default.Rd | 18 ------------ man/geom_contour.Rd | 6 ++-- 5 files changed, 29 insertions(+), 65 deletions(-) delete mode 100644 man/breaks_default.Rd diff --git a/NAMESPACE b/NAMESPACE index b856fdc4cc..4ee9a7336b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -219,7 +219,6 @@ export(autolayer) export(autoplot) export(benchplot) export(borders) -export(breaks_default) export(calc_element) export(combine_vars) export(continuous_scale) diff --git a/R/geom-contour.r b/R/geom-contour.r index 0eaa1a58f3..ca99a08951 100644 --- a/R/geom-contour.r +++ b/R/geom-contour.r @@ -57,7 +57,7 @@ geom_contour <- function(mapping = NULL, data = NULL, lineend = "butt", linejoin = "round", linemitre = 1, - breaks = breaks_default, + breaks = fullseq, bins = NULL, binwidth = NULL, na.rm = FALSE, diff --git a/R/stat-contour.r b/R/stat-contour.r index 6534a4efb9..4d30399f36 100644 --- a/R/stat-contour.r +++ b/R/stat-contour.r @@ -1,7 +1,7 @@ #' @inheritParams stat_identity #' @param breaks One of: #' - A numeric vector of breaks -#' - A function that takes the range of the data, bins and binwidth as input +#' - A function that takes the range of the data and binwidth as input #' and returns breaks as output #' @param bins Number of evenly spaced breaks. #' @param binwidth Distance between breaks. @@ -14,7 +14,7 @@ stat_contour <- function(mapping = NULL, data = NULL, geom = "contour", position = "identity", ..., - breaks = breaks_default, + breaks = fullseq, bins = NULL, binwidth = NULL, na.rm = FALSE, @@ -43,24 +43,32 @@ stat_contour <- function(mapping = NULL, data = NULL, #' @usage NULL #' @export StatContour <- ggproto("StatContour", Stat, - required_aes = c("x", "y", "z"), - default_aes = aes(order = ..level..), + required_aes = c("x", "y", "z"), + default_aes = aes(order = ..level..), - compute_group = function(data, scales, bins = NULL, binwidth = NULL, - breaks = breaks_default, - complete = FALSE, na.rm = FALSE) { - # Check is.null(breaks) for backwards compatibility - if (is.null(breaks)) { - breaks <- breaks_default - } - if (is.function(breaks)) { - breaks <- breaks(range(data$z), bins = bins, - binwidth = binwidth) - } + compute_group = function(data, scales, bins = NULL, binwidth = NULL, + breaks = fullseq, complete = FALSE, + na.rm = FALSE) { + # Check is.null(breaks) for backwards compatibility + if (is.null(breaks)) { + breaks <- fullseq + } - contour_lines(data, breaks, complete = complete) - } + if (is.function(breaks)) { + # If no parameters set, use pretty bins to calculate binwidth + if (is.null(bins) && is.null(binwidth)) { + binwidth <- diff(pretty(range(data$z), 10))[1] + } + # If provided, use bins to calculate binwidth + if (!is.null(bins)) { + binwidth <- diff(range(data$z)) / bins + } + breaks <- breaks(range(data$z), binwidth) + } + + contour_lines(data, breaks, complete = complete) + } ) @@ -124,28 +132,3 @@ poly_dir <- function(x, y) { # ggplot(contours, aes(x, y)) + # geom_path(aes(group = piece, colour = factor(dir))) # last_plot() + facet_wrap(~ level) - -#' Default breaks -#' -#' Default behaviour for computing breaks in [stat_contour()]. -#' -#' @param range The range of the data -#' @param bins Number of evenly spaced breaks. -#' @param binwidth Distance between breaks. -#' -#' @export -breaks_default <- function(range, bins, binwidth) { - # If no parameters set, use pretty bins - if (is.null(bins) && is.null(binwidth)) { - breaks <- pretty(range, 10) - } - # If provided, use bins to calculate binwidth - if (!is.null(bins)) { - binwidth <- diff(range) / bins - } - # If necessary, compute breaks from binwidth - if(!is.null(binwidth)) { - breaks <- fullseq(range, binwidth) - } - return(breaks) -} diff --git a/man/breaks_default.Rd b/man/breaks_default.Rd deleted file mode 100644 index 56654f87d8..0000000000 --- a/man/breaks_default.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/stat-contour.r -\name{breaks_default} -\alias{breaks_default} -\title{Default breaks} -\usage{ -breaks_default(range, bins, binwidth) -} -\arguments{ -\item{range}{The range of the data} - -\item{bins}{Number of evenly spaced breaks.} - -\item{binwidth}{Distance between breaks.} -} -\description{ -Default behaviour for computing breaks in \code{\link[=stat_contour]{stat_contour()}}. -} diff --git a/man/geom_contour.Rd b/man/geom_contour.Rd index 61527665b9..d657148da2 100644 --- a/man/geom_contour.Rd +++ b/man/geom_contour.Rd @@ -7,11 +7,11 @@ \usage{ geom_contour(mapping = NULL, data = NULL, stat = "contour", position = "identity", ..., lineend = "butt", linejoin = "round", - linemitre = 1, breaks = breaks_default, bins = NULL, binwidth = NULL, + linemitre = 1, breaks = fullseq, bins = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) stat_contour(mapping = NULL, data = NULL, geom = "contour", - position = "identity", ..., breaks = breaks_default, bins = NULL, + position = "identity", ..., breaks = fullseq, bins = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) } \arguments{ @@ -54,7 +54,7 @@ to the paired geom/stat.} \item{breaks}{One of: \itemize{ \item A numeric vector of breaks -\item A function that takes the range of the data, bins and binwidth as input +\item A function that takes the range of the data and binwidth as input and returns breaks as output }}