2

I am using ggplot2 to create a 2D facet plot. I wish to drop the y-axis strip (because it is self-explanatory) but remove the x-axis strip (because it is not).

I found several examples about how to remove both strips, e.g. this. However, in every case, it was showing you how to get rid of all the labels. There is an example of how to remove the labels from one panel and not the other, but this seems more complicated than I imagine (hope) it actually is. (Also, when I copied and pasted the code, R did not recognize the "unit" command that appeared in the code fragment, though I see that this is addressed elsewhere.)

So, let's say I have the facet grid below, and I want to hide the grey strip for cut, but not color.

require(ggplot2)
pdf(file = sprintf("minimal.pdf"))
p <- ggplot(diamonds, aes(carat, price))
p <- p + geom_point()
p <- p + facet_grid(cut ~ color, scales="fixed")
print(p)
dev.off()

Adding the following line turns both strips white, and removes the characters from the y-strip (cut).

p <- p + theme(strip.text.y = element_blank(), strip.background = element_blank())

That's an improvement, but what I really want to do is keep the x-strip as it was, with the original grey background, but remove the y-strip. Manually adjusting the margins every time I resize the figure, as is done in one of the references, does not seem like a nice way to do it. I am wondering if there is a better way.

Community
  • 1
  • 1
David Bruce Borenstein
  • 1,655
  • 2
  • 19
  • 34
  • Can you make this reproducible with fake data? – maloneypatr Oct 06 '14 at 21:49
  • 1
    I dont think that adding `+theme(strip.text.y = element_blank(), strip.background = element_blank())` is that complicated... in any case if you use it often you can save it like `my.theme <- theme(strip.text.y = element_blank(), strip.background = element_blank())` and then all you have to do is `p + my.theme` – konvas Oct 06 '14 at 22:05
  • Maloneypatr, edited to use reproducible data as requested. Konvas, I agree that this is not complicated, but it doesn't do quite the right thing (see edited question). – David Bruce Borenstein Oct 07 '14 at 15:32

1 Answers1

3

You can subset the gtable to remove the column you don't want

g <- ggplotGrob(p)

strips <- subset(g$layout, grepl("strip-right", g$layout$name))

library(grid)
grid.newpage()
grid.draw(g[,-unique(strips$r)])
baptiste
  • 75,767
  • 19
  • 198
  • 294