Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Only One of Two facet_grid() X Labels

Tags:

r

ggplot2

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.

like image 714
jeromeResearch Avatar asked Oct 31 '25 09:10

jeromeResearch


1 Answers

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)

like image 182
teunbrand Avatar answered Nov 02 '25 01:11

teunbrand