Reputation: 1745
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.
Upvotes: 2
Views: 338
Reputation: 77106
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)])
Upvotes: 3