-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
breaks
argument in stat_contour()
accepts function.
#2320
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, bins and binwidth 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 = breaks_default, | ||
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, | ||
... | ||
) | ||
) | ||
|
@@ -31,26 +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 = 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 | ||
} | ||
# If necessary, compute breaks from binwidth | ||
if (is.null(breaks)) { | ||
breaks <- fullseq(range(data$z), binwidth) | ||
} | ||
compute_group = function(data, scales, bins = NULL, binwidth = NULL, | ||
breaks = breaks_default, | ||
complete = FALSE, na.rm = FALSE) { | ||
# Check is.null(breaks) for backwards compatibility | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we've gone off the rails a bit here because I wasn't clear exactly what I was thinking. I'd prefer not to make such a big change and instead of |
||
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) | ||
} | ||
|
||
) | ||
|
||
|
@@ -68,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( | ||
|
@@ -116,3 +125,27 @@ 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, 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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please preserve the existing indentation?