Dai
Dai

Reputation: 75

facet_grid2 add headers to top and bottom rows

Given the below data:

df <- data.frame(group = rep(c('A', 'B', 'C'), each = 5),
           treatment = rep(c('a', 'b', 'c', 'd', 'e'), 3)) %>%
  left_join(data.frame(group = rep(c('A', 'B', 'C', 'D'), each = 4),
                       level = rep(1:4, 4))) 
df$header <- df$treatment
df$header[df$group == 'C'] <- paste0(df$treatment, 'extra')
df$est <- rnorm(nrow(df))
df$ci_lower <- df$est - runif(nrow(df), 0,0.7)
df$ci_upper <- df$est + runif(nrow(df), 0,0.7)

I can make a facetted plot with treatment values across columns and group values across rows:

ggplot(df, aes(y = level)) +
  geom_vline(xintercept = 0, linewidth = 0.3, colour = "black", linetype = "dotted") +
  geom_errorbar(aes(xmin = ci_lower, xmax = ci_upper), position = position_dodge(width = 0.8), width = 0, linewidth = 0.5) +
  geom_point(aes(x = est), fill = 'white', position = position_dodge(width = 0.8)) +
  ggh4x::facet_grid2(group ~ treatment, scales = 'free', independent = 'y', render_empty = FALSE) +
  ylab('') + xlab('Estimated AMCE') +
  ggh4x::force_panelsizes(rows = c(1, 0.4, 0.4)) +
  theme_bw()

enter image description here

But what I would like is to add an additional column header on top of the third row using header values, so it looks like this:

enter image description here

Is this possible to do under ggh4x? If not, is there any other alternative?

One option is to just plot the third row plots separately but that changes the axis range and panel size as well. I'm hoping there's an easier solution to this.

Upvotes: 0

Views: 50

Answers (0)

Related Questions