Search code examples
rggplot2facet-grid

How to get rid of a certain column in facet_grid?


I have the following example code:

tmp <- data.frame(
  "channel_topic" = rep(LETTERS[seq(1:16)],9),
  "p1_age_cat_complete" = rep(1:9, each = 16),
  "n" = sample(100, size = 144, replace = TRUE)) |>
  group_by(channel_topic) |>
  mutate(n_group = sum(n))|>
  group_by(n_group) |>
  mutate(group_id = abs(cur_group_id()-17)) |>
  ungroup() |>
  mutate(group_id = ifelse(str_detect(channel_topic, "N"),
                           99,
                           ifelse(str_detect(channel_topic, "O"),
                                  100,
                                  ifelse(str_detect(channel_topic, "P"),
                                         101,
                                         group_id)))) |>
  mutate(facets = ifelse(group_id < 99,"1","2"),
         facets_2 = ifelse(p1_age_cat_complete == "9", "2","1"))
  
  
ggplot(tmp) +
  facet_grid(
    cols = vars(facets_2),
    rows = vars(facets),
    scales = "free_y",
    space = "free_y",
  ) +
  geom_tile(
    aes(
      x = p1_age_cat_complete,
      y = reorder(channel_topic, -group_id),
      fill = n
    )
  ) + theme(
    axis.text.x = element_text(angle = 90)
  ) +
  labs(
    x = "Alter",
    y = "Channel Topic",
    fill = "Anzahl",
    subtitle = paste("N = ", sum(tmp$n), sep = "")
  ) + theme(
    panel.background = element_rect(fill = "white",
                                    colour = "white"),
    panel.grid.major = element_line(colour = "gray90"),
    panel.grid.minor = element_line(colour = "grey95"),
    plot.subtitle = element_text(hjust = 1),
    axis.text.x = element_text(angle = 90),
    strip.background = element_blank(),
    strip.text = element_blank()
  ) +
  scale_x_discrete(limits = 1:9)

The output looks like this:

enter image description here

How can I get rid of the 9 column on the left side of the plot and of all other column exept 9 on the right side of the plot. Basically I want to suppress the empty columns and have only a little space like on the y-axis.

Thank you in advance.

Best,

Aaron


Solution

  • Is this what you're looking for?

    1. p1_age_cat_complete is made into a factor
    2. Set scales and space free for the x axis in addition to the y
    3. Removed scale_x_discrete

    edit I forgot to note I removed the first theme(axis.text.x... call as its repeated after labs().

    library(tidyverse)
    
    tmp <- data.frame(
      "channel_topic" = rep(LETTERS[seq(1:16)],9),
      "p1_age_cat_complete" = rep(1:9, each = 16),
      "n" = sample(100, size = 144, replace = TRUE)) |>
      group_by(channel_topic) |>
      mutate(n_group = sum(n))|>
      group_by(n_group) |>
      mutate(group_id = abs(cur_group_id()-17)) |>
      ungroup() |>
      mutate(group_id = ifelse(str_detect(channel_topic, "N"),
                               99,
                               ifelse(str_detect(channel_topic, "O"),
                                      100,
                                      ifelse(str_detect(channel_topic, "P"),
                                             101,
                                             group_id)))) |>
      mutate(facets = ifelse(group_id < 99,"1","2"),
             facets_2 = ifelse(p1_age_cat_complete == "9", "2","1"),
             p1_age_cat_complete = factor(p1_age_cat_complete))
    
    tmp %>% 
      ggplot() +
      facet_grid(
        cols = vars(facets_2),
        rows = vars(facets),
        scales = "free",
        space = "free"
      ) +
      geom_tile(
        aes(
          x = p1_age_cat_complete,
          y = reorder(channel_topic, -group_id),
          fill = n
        )
      ) +
      labs(
        x = "Alter",
        y = "Channel Topic",
        fill = "Anzahl",
        subtitle = paste("N = ", sum(tmp$n), sep = "")
      ) + theme(
        panel.background = element_rect(fill = "white",
                                        colour = "white"),
        panel.grid.major = element_line(colour = "gray90"),
        panel.grid.minor = element_line(colour = "grey95"),
        plot.subtitle = element_text(hjust = 1),
        axis.text.x = element_text(angle = 90),
        strip.background = element_blank(),
        strip.text = element_blank()
      ) 
    

    Created on 2023-08-04 with reprex v2.0.2