Some months ago, I needed to remove one of the strips generated by a ggplot2 faceted plot. I found that someone had already asked in this question, and the answer worked pretty well:
library(ggplot2)
a <- ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_grid(cyl~gear)
strip.remover <- function(ggp, what="x") {
require(gridExtra)
zeroGrob <- function() {
g0 <- grob(name="NULL")
class(g0) <- c("zeroGrob",class(g0))
g0
}
g <- ggplotGrob(ggp)
g$grobs <- lapply(g$grob, function(gr) {
if (any(grepl(paste0("strip.text.", what),names(gr$children)))) {
gr$children[[grep("strip.background",names(gr$children))]] <- zeroGrob()
gr$children[[grep("strip.text",names(gr$children))]] <- zeroGrob()
}
return(gr)
}
)
class(g) = c("arrange", "ggplot",class(g))
g
}
strip.remover(a, "y")
Today, I was trying to regenerate some figures that used this code and it didn't work to my surprise. Apparently, making a grob with ggplotGrob
, modifying its contents and transforming it back to a ggplot2
object does not work anymore.
I am a bit clueless on how to proceed here. Any ideas on why this code does not work anymore?
I suspect that the package gridExtra
may be the culprit. In my work machine where this code works, this package is in version 0.9.1, but in my laptop, where it doesn't work, I have 2.0.0. Since the gap between versions is quite large, I have no idea what change may be related to this problem.