Skip to content
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

remove_geom: a function to remove layers from a ggplot2 object #745

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -205,3 +205,4 @@ Collate:
'translate-qplot-lattice.r' 'translate-qplot-lattice.r'
'annotation-logticks.r' 'annotation-logticks.r'
'utilities-help.r' 'utilities-help.r'
'remove-geom.R'
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -289,6 +289,7 @@ export(position_stack)
export(qplot) export(qplot)
export(quickplot) export(quickplot)
export(rel) export(rel)
export(remove_geom)
export(resolution) export(resolution)
export(scale_alpha) export(scale_alpha)
export(scale_alpha_continuous) export(scale_alpha_continuous)
Expand Down
3 changes: 3 additions & 0 deletions NEWS
@@ -1,6 +1,9 @@
ggplot2 0.9.3.99 ggplot2 0.9.3.99
---------------------------------------------------------------- ----------------------------------------------------------------


* New function `remove_geom` to remove specific layers from a ggplot2
object.

BUG FIXES BUG FIXES


* The theme element `legend.box.just` now can be set. It was not properly * The theme element `legend.box.just` now can be set. It was not properly
Expand Down
28 changes: 28 additions & 0 deletions R/remove-geom.R
@@ -0,0 +1,28 @@
#' Remove a Geom From a ggplot2 Object
#'
#' remove_geom allows you to remove layers of from a ggplot2 object
#' by specifying the name of a geom to remove. This can be useful
#' when dynamically building ggplot2 objects and want to remove
#' cluttered layers that have already been added to the object in
#' previous function calls.
#'
#' @param plot_object the ggplot2 object to work on
#' @param geom name of geom to remove
#' @examples
#' p <- ggplot(data.frame(a=1:10, b=1:10), aes(x=a, y=b)) + geom_point() + geom_line()
#' p_minus_point <- remove_geom(p, "point")
#' stopifnot(
#' setdiff(
#' sapply(p$layers, function(x) x$geom$objname),
#' sapply(p_minus_point$layers, function(x) x$geom$objname)
#' ) == "point"
#' )
#' @export
remove_geom <- function(plot_object, geom) {

layers <- lapply(plot_object$layers, function(x) if(x$geom$objname == geom) NULL else x)
layers <- layers[!sapply(layers, is.null)]

plot_object$layers <- layers
plot_object
}
29 changes: 29 additions & 0 deletions man/remove_geom.Rd
@@ -0,0 +1,29 @@
\name{remove_geom}
\alias{remove_geom}
\title{Remove a Geom From a ggplot2 Object}
\usage{
remove_geom(plot_object, geom)
}
\arguments{
\item{plot_object}{the ggplot2 object to work on}

\item{geom}{name of geom to remove}
}
\description{
remove_geom allows you to remove layers of from a ggplot2
object by specifying the name of a geom to remove. This
can be useful when dynamically building ggplot2 objects
and want to remove cluttered layers that have already
been added to the object in previous function calls.
}
\examples{
p <- ggplot(data.frame(a=1:10, b=1:10), aes(x=a, y=b)) + geom_point() + geom_line()
p_minus_point <- remove_geom(p, "point")
stopifnot(
setdiff(
sapply(p$layers, function(x) x$geom$objname),
sapply(p_minus_point$layers, function(x) x$geom$objname)
) == "point"
)
}