13

Many of the theme elements in ggplot2 have a .x or .y only extension to remove/alter something on only one axis. strip.background does not have a strip.background.x equivalent as can be seen below.

How can I remove the text and strip.background from the facet labels on only one axis?

a <- ggplot(mtcars, aes(mpg, hp)) +
    geom_point() +
    facet_grid(cyl~gear) 

a + theme(strip.text.y = element_blank(), 
    strip.background.x = element_blank())

##     > a + theme(strip.text.y = element_blank(), strip.background.x = element_blank())
##     Error in (function (el, elname)  : 
##       "strip.background.x" is not a valid theme element name.
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

5 Answers5

8
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")

enter image description here

Roland
  • 127,288
  • 10
  • 191
  • 288
7

Here's a way to remove the relevant strips,

library(grid)  # for the grid functions
g <- ggplotGrob(a)
keep <- !grepl("strip-right", g$layout$name)
g$grobs <- g$grobs[keep]
g$layout <- g$layout[keep, ]
grid.newpage()
grid.draw(g)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
baptiste
  • 75,767
  • 19
  • 198
  • 294
7

At least for the ggplot2 version 2.0.0. if you set the strip.text.x= or strip.text.y= to element_blank() it removes text and the background for particular axis.

a + theme(strip.text. = element_blank())

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
3

Same idea but using grid package

g <- ggplotGrob(a)
gg <- g$grobs
strip_right.index <- which(grepl('strip-right',g$layout$name))
for(ii in strip_right.index)
  gg[[ii]] <- editGrob(getGrob(gg[[ii]],'strip.back'
                               ,grep=TRUE,global=TRUE)
                       ,gp = gpar(fill=NA))

g$grobs <- gg
grid.draw(g)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • But the panels will still be there, i.e., take up graph space, won't they? I think you could at least set `fill=NA`. – Roland Sep 28 '13 at 07:50
  • @Roland my mistake..I mean NA in fact. – agstudy Sep 28 '13 at 07:51
  • @agstudy I think this solution is a bit more generalizable in that it doesn't rely on non exported functions. Could you extend it to remove the other side as well (the x side)? – Tyler Rinker Sep 28 '13 at 13:26
2

In ggplot2 version 3.0.0 this works as you expected. theme(strip.background.x = element_blank()) is now a valid theme element name.

Citing from https://github.com/tidyverse/ggplot2/releases/tag/v3.0.0:

Minor bug fixes and improvements

Faceting

You can now style the background of horizontal and vertical strips independently with strip.background.x and strip.background.y theme settings (#2249).

The underlying feature request can be found here: https://github.com/tidyverse/ggplot2/issues/2249

Community
  • 1
  • 1
fabern
  • 318
  • 2
  • 10