diff --git a/NAMESPACE b/NAMESPACE index 495aa33..a993bbd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/as.gglist.R b/R/as.gglist.R index 577bd62..b1ea297 100644 --- a/R/as.gglist.R +++ b/R/as.gglist.R @@ -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 diff --git a/R/remove_geom.R b/R/remove_geom.R index 10dd677..38ab57b 100644 --- a/R/remove_geom.R +++ b/R/remove_geom.R @@ -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]