I have two charts: FigA and FigB. Both are facet wrapped. FigA is facetted based on a short factor label, whereas FigB is faceted based on a much longer factor label. Both FigA and FigB share an x-axis, and so I want to display them vertically (via cowplot) with the strip labels positioned to the right of the plot and with horizontal text.
This results in a lot of white space around FigA's short strip labels.
What is the best way to format the strip label backgrounds so that they expand to fill the available horizontal space?
I've been playing around with strip.background and strip.text margins, size and other parameters, but so far have not produced the desired result. Am I missing something obvious?
Below is a minimum example
(I realise I could probably pivot_longer and produce a single chart with this simple example, but is there a way to modify the strip label directly for more complex situations?):
library(tidyverse)
library(cowplot)
df <- data.frame( shortCat = sample(c('a','b'), 10, replace=TRUE),
longCat = sample(c('a really long label','another really long label'), 10, replace=TRUE),
x = sample(seq(as.Date('2020/01/01'), as.Date('2020/12/31'), by="day"), 10),
y = sample(0:25, 10, replace = TRUE) )
figA <- df %>% ggplot( aes(x=x,y=y) ) +
geom_line() +
facet_wrap(vars(shortCat), ncol=1, strip.position ="right", scales="free_y") +
theme( axis.title.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
strip.text.y.right = element_text(angle = 0, hjust=0) )
figB <- df %>% ggplot( aes(x=x,y=y) ) +
geom_bar(stat="identity") +
facet_wrap(vars(longCat), ncol=1, strip.position ="right", scales="free_y") +
theme( axis.title.y=element_blank(),
strip.text.y.right = element_text(angle = 0, hjust=0) )
plot_grid(figA,figB, ncol=1, align="v")



