3

This is a variant of this question and this question that I haven't found a solution to.

Using mtcars as an example I want a plot similar to the following

ggplot(mtcars, aes(x=cyl,y=mpg)) +
geom_point() +
facet_grid(am ~ gear + carb)

However, I want the facet x labels associated with the "carb" variable to be removed while keeping the label for the "gear" variable. The following gets at what I want, but puts "NA" in it's place rather than completely removing it.

ggplot(mtcars, aes(x=cyl,y=mpg)) +
geom_point() +
facet_grid(am ~ gear + carb, labeller = labeller(carb = "")

Of course, using theme(strip.text.x = element_blank()) doesn't work for me either since I want to keep the label for the "gear" variable.

Ultimately, I want to grid using both x layers but for me the label for the second x layer doesn't hold any meaning so it's distracting to include in the plot.

1 Answers1

2

For reasons I also don't understand, the following seems to work. The function just returns a vector of empty strings of equal length as the input.

library(ggplot2)

ggplot(mtcars, aes(x=cyl,y=mpg)) +
  geom_point() +
  facet_grid(am ~ gear + carb, 
             labeller = labeller(carb = function(x) {rep("", length(x))}))

Created on 2021-01-12 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Interesting. I skimmed over [this answer](https://stackoverflow.com/a/42913120/7753022) too quickly which seems to take a similar approach. This works perfectly, thank you! – jeromeResearch Jan 12 '21 at 22:18