How to compartmentalize labels on the x axis

Hello everyone,
As indicated in the title of the topic, I would like to be able to compartmentalize my labels on the x-axis in a ggplot (geom_col).

For example I have a graph with several attributes on the X axis:
1 2 3 4 5 6 7 8 9 10

For example, I would like to create compartments that will be seen on the X axis
For example having 1,2 and 6 under one compartments, etc.
Here is an example :

thank you very much

1 Like

Will this do the job? I've done something slightly hacky with facets but it seems to do the job. You might want to change the theming of the facet strip.


df = tibble(x = 1:10) %>% 
    area = case_when(
      x %in% c(1,2,6) ~ "Area 1",
      x %in% c(4,5,8) ~ "Area 2",
      x %in% c(3,7,9,10) ~ "Area 3"
    x = factor(x),
    y = 1:10) 

ggplot(df, aes(x, y)) +
  geom_col() +
  facet_wrap(~area, scales = "free_x", strip.position = "bottom") +
  theme_light() +
  theme(strip.placement = "outside",
        panel.border = element_blank(),
        axis.line = element_line()) +
  labs(x = "X LABEL", y = "Y LABEL")

Created on 2022-02-11 by the reprex package (v2.0.1)


Hi @JackDavison Thank you very much, that's exactly it!
How do I find my subcategories in my database?
I have a single database and the 1:10 categories are for example in a "Subarea" column. How would you integrate them from a dataframe that we will call df?

Thanks for your time

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.