Skip to content

Commit

Permalink
fix remove_geom and add -.gg method
Browse files Browse the repository at this point in the history
  • Loading branch information
yonicd committed Dec 29, 2017
1 parent 51c802c commit 35b9bb5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method("+",gg)
S3method("-",gg)
S3method(merge_element,default)
S3method(merge_element,element)
S3method(print,ggedit)
Expand Down
34 changes: 34 additions & 0 deletions R/as.gglist.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ as.gglist <- function(p) {
structure(p, class = cl)
}

#' @export
#' @import ggplot2
#' @importFrom plyr llply
"-.gg" <- function(e1, e2) {

pf <- proto_features(e2)

if (inherits(e1, "gglist")) {
e3 <- as.gglist(
plyr::llply(e1, function(t1) {
geom <- tolower(gsub('^Geom','',pf$geom))
layers <- t1$layers
layer.type <- lapply(t1$layers, function(x) class(x$geom))
a.rm <- which(grepl(paste0("(?i)", geom), layer.type))
if(length(a.rm)>0){
remove_geom(t1, geom,max(a.rm))
}else{
t1
}
})
)
}else{
geom <- tolower(gsub('^Geom','',pf$geom))
layers <- e1$layers
layer.type <- lapply(e1$layers, function(x) class(x$geom))
a.rm <- which(grepl(paste0("(?i)", geom), layer.type))
e3 <- remove_geom(e1, geom, max(a.rm))
}

attributes(e3) <- attributes(e1)

e3
}

#' @export
#' @import ggplot2
#' @importFrom plyr llply
Expand Down
1 change: 0 additions & 1 deletion R/remove_geom.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ remove_geom <- function(p, geom, idx) {

if (length(a.rm) >= idx) a.rm <- a.rm[idx]

p$labels[[names(layers[[a.rm]]$mapping)]] <- NULL
layers <- layers[-a.rm]


Expand Down

0 comments on commit 35b9bb5

Please sign in to comment.