ggplot with facets: show only x axis labels where data

I have a ggplot-plot with facets. And I would like to see only those x axis labels for which data points exist in each single facet. I am aware that I could construct the plots individually and put them together with e.g. patchwork. But I was wondering whether there is a general solution to this without going the extra mile. Maybe I am missing something very obvious?


x <- mtcars %>% 
  mutate(group=case_when(carb %in% c(1,2,4) ~ "A",
                         TRUE ~ "B")) %>% 
  group_by(group, carb) %>% 
x %>% 
  geom_point(aes(x=carb, y=qsec.mean))+
  facet_wrap(vars(group), scales="free_x", nrow=2)

For example, in the plot below I would like to see only the labels for 1,2, and 4 for group A, and 3, 6, and 8 for group B.

Many thanks.

UPDATE: I am aware that I can add scale_x_continuous(breaks=x$carb, labels=x$carb). This removes 'empty' x labels, but keeps those overlapping with another facet (e.g. label for the value 4 shows up in facet A and B, although the data point is only in fact A).

I'm not aware of a way of doing this without stitching together separate plots using grid.arrange() or a package like patchwork or cowplot.

many thanks! quite a pity. had hoped that e.g. a function to the labels option could do the trick.

Actually thinking about it again, there might be a hacky way to do this.

The x axis variable could be converted to a factor which would work at the cost of the labels not being spaced correctly.

The hacky bit would be finding a way of correcting this by adding dummy data for the intermediate values, e.g. plotting either transparent or out of the range values on the y axis, and excluding the dummy labels from the x axis.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.