ggplot facet_grid: need lines between outside strip placement and panels

11 Views Asked by At

I am using facet_grid to create a plot with one column and multiple rows, with an outside strip placement to the left of the y-axis tick mark labels. I would like to enclose the y-axis tick mark labels such that each facet strip label is cleanly attached to its associated panel and the y-axis labels are functionally enclosed in a box. The facets are of different sizes. The reproducible example here is stripped down to its most essential elements and will have more than two facets. Thank you for your help!

library(tidyverse)
library(gggenes)

df <- dplyr::tribble(
  ~ sy,  ~ group, ~ cat_label, ~ percent, ~ ca1, ~ ca2, ~pct_change,
  2019, "All", "All", 8.4, 8.4, 15.9, 7.5,
  2022, "All", "All", 15.9, 8.4, 15.9, 7.5,
  2019, "Female", "Gender", 8.3, 8.3, 16.1, 7.8, 
  2022, "Female", "Gender", 16.1, 8.3, 16.1, 7.8,
  2019, "Male", "Gender", 8.5, 8.5, 15.8, 7.3,
  2022, "Male", "Gender", 15.8, 8.5, 15.8, 7.3)

n_rep <- nrow(df) - 2
dumbbell_colors <- c(rep("black", 2), rep("#F3AE3D", n_rep))

ggplot(df %>% arrange(group, sy), aes(y = group, x = percent, group = group, colour = group, fill = group)) + 
  gggenes::geom_gene_arrow(aes(xmin = ifelse(sign(pct_change) > 0, ca1, ca2),
                               xmax = ifelse(sign(pct_change) > 0, ca2, ca1), y = group),
                           arrowhead_height = unit(3, "mm"), arrowhead_width = unit(2, "mm"), fill = dumbbell_colors, colour = "black") +
  facet_grid(cat_label ~ ., scales = "free_y", space = "free_y", switch = "y") +
  scale_color_manual(values = dumbbell_colors, na.value = NA) +
  xlim(0,30) +
  xlab("(%)") +
  theme_bw() +
  theme(axis.title.y=element_blank(),
        legend.position = "none",
        strip.placement = "outside",
        panel.background = element_blank(),
        panel.grid = element_blank())

I attempted to draw rectangles between the facet strip label and the panel using different methods, but these were either placed inside the panels and were scaled to the size of the largest panel. I looked through the lemon package for options and came up dry. I really don't know what else to try.

0

There are 0 best solutions below