-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Function as data #1527
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
Merged
Merged
Function as data #1527
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9cd2da0
Merge pull request #3 from hadley/master
thomasp85 ed3db99
Merge pull request #5 from hadley/master
thomasp85 d3cf225
fortify.function passes function along
thomasp85 b73265b
Add layer_data method to Layer to get the data from the layer or reso…
thomasp85 47e6eb3
Don't resolve waiver - already done in layer_data
thomasp85 6bce655
use layer_data for data extraction rather that direct
thomasp85 304af85
Add unit tests for layer data extraction
thomasp85 4ef8d79
Slight restructure of layer_data
thomasp85 9338d58
call. = FALSE in stop
thomasp85 259f230
Test against error message
thomasp85 ba7501d
Add #1527 to news
thomasp85 4c7b76b
Improved layer documentation with description of different data argum…
thomasp85 6510c45
Roxygenise
thomasp85 13be8a5
Merge remote-tracking branch 'hadley/master' into function_as_data
thomasp85 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
#' Create a new layer | ||
#' | ||
#' A layer is a combination of data, stat and geom with a potential position | ||
#' adjustment. Usually layers are created using \code{geom_*} or \code{stat_*} | ||
#' calls but it can also be created directly using the \code{layer} function. | ||
#' | ||
#' @details | ||
#' The data in a layer can be specified in one of three ways: | ||
#' | ||
#' \itemize{ | ||
#' \item{If the data argument is \code{NULL} (the default) the data is | ||
#' inherited from the global plot data as specified in the call to | ||
#' \code{\link{ggplot}}.} | ||
#' \item{If the data argument is a function, that function is called with the | ||
#' global data as the only argument and the return value is used as the layer | ||
#' data. The function must return a data.frame.} | ||
#' \item{Any other type of value passed to \code{data} will be passed through | ||
#' \code{\link{fortify}}, and there must thus be a \code{fortify} method | ||
#' defined for the class of the value. Passing a data.frame is a special case | ||
#' of this as \code{fortify.data.frame} returns the data.frame untouched.} | ||
#' } | ||
#' | ||
#' @export | ||
#' @inheritParams geom_point | ||
#' @param geom,stat,position Geom, stat and position adjustment to use in | ||
|
@@ -16,6 +36,13 @@ | |
#' layer(geom = "point", stat = "identity", position = "identity", | ||
#' params = list(na.rm = FALSE) | ||
#' ) | ||
#' | ||
#' # use a function as data to plot a subset of global data | ||
#' ggplot(mpg, aes(displ, hwy)) + | ||
#' layer(geom = "point", stat = "identity", position = "identity", | ||
#' data = head, params = list(na.rm = FALSE) | ||
#' ) | ||
#' | ||
layer <- function(geom = NULL, stat = NULL, | ||
data = NULL, mapping = NULL, | ||
position = NULL, params = list(), | ||
|
@@ -101,6 +128,20 @@ Layer <- ggproto("Layer", NULL, | |
cat(snakeize(class(self$position)[[1]]), "\n") | ||
}, | ||
|
||
layer_data = function(self, plot_data) { | ||
if (is.waive(self$data)) { | ||
data <- plot_data | ||
} else if (is.function(self$data)) { | ||
data <- self$data(plot_data) | ||
if (!is.data.frame(data)) { | ||
stop("Data function must return a data.frame", call. = FALSE) | ||
} | ||
} else { | ||
data <- self$data | ||
} | ||
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 this would be better with no returns and a final else block |
||
data | ||
}, | ||
|
||
compute_aesthetics = function(self, data, plot) { | ||
# For annotation geoms, it is useful to be able to ignore the default aes | ||
if (self$inherit.aes) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can drop the
data <-
in all the alternatives