I modify your function to allow choosing which facets for tagging
tag_facet_flex <- function(p, position = 'both',
open = c("(", ""), close = c(")", ")"),
tag_fun_top = function(i) letters[i],
tag_fun_right = utils::as.roman,
x = c(0, 0), y = c(0.5, 1),
hjust = c(0, 0), vjust = c(0.5, 1),
fontface = c(1, 1), ...) {
gb <- ggplot_build(p)
lay <- gb$layout$layout
if (grepl(position, 'top')) {
lay <- gb$layout$layout
tags_top <- paste0(open[1], tag_fun_top(unique(lay$COL)), close[1])
tl <- lapply(tags_top, grid::textGrob,
x = x[1], y = y[1],
hjust = hjust[1], vjust = vjust[1],
gp = grid::gpar(fontface = fontface[1], ...)
)
g <- ggplot_gtable(gb)
g <- gtable::gtable_add_rows(g, grid::unit(1, "line"), pos = 0)
lm <- unique(g$layout[grepl("panel", g$layout$name), "l"])
g <- gtable::gtable_add_grob(g, grobs = tl, t = 1, l = lm)
} else if (grepl(position, 'right')) {
tags_right <- paste0(open[2], tag_fun_right(unique(lay$ROW)), close[2])
rl <- lapply(tags_right, grid::textGrob,
x = x[2], y = y[2],
hjust = hjust[2], vjust = vjust[2],
gp = grid::gpar(fontface = fontface[2], ...)
)
g <- ggplot_gtable(gb)
l <- unique(g$layout[grepl("panel", g$layout$name), "l"])
wm <- do.call(grid::unit.pmax, lapply(rl, grid::grobWidth))
g <- gtable::gtable_add_cols(g, wm, pos = max(l))
t <- unique(g$layout[grepl("panel", g$layout$name), "t"])
g <- gtable::gtable_add_grob(g, grobs = rl, t = t, l = max(l) + 1)
g <- gtable::gtable_add_cols(g, unit(2, "mm"), pos = max(l))
} else {
print('Use default tagging option: both top and right sides')
tags_top <- paste0(open[1], tag_fun_top(unique(lay$COL)), close[1])
tags_right <- paste0(open[2], tag_fun_right(unique(lay$ROW)), close[2])
tl <- lapply(tags_top, grid::textGrob,
x = x[1], y = y[1],
hjust = hjust[1], vjust = vjust[1],
gp = grid::gpar(fontface = fontface[1], ...)
)
rl <- lapply(tags_right, grid::textGrob,
x = x[2], y = y[2],
hjust = hjust[2], vjust = vjust[2],
gp = grid::gpar(fontface = fontface[2], ...)
)
g <- ggplot_gtable(gb)
g <- gtable::gtable_add_rows(g, grid::unit(1, "line"), pos = 0)
l <- unique(g$layout[grepl("panel", g$layout$name), "l"])
g <- gtable::gtable_add_grob(g, grobs = tl, t = 1, l = l)
wm <- do.call(grid::unit.pmax, lapply(rl, grid::grobWidth))
g <- gtable::gtable_add_cols(g, wm, pos = max(l))
t <- unique(g$layout[grepl("panel", g$layout$name), "t"])
g <- gtable::gtable_add_grob(g, grobs = rl, t = t, l = max(l) + 1)
g <- gtable::gtable_add_cols(g, unit(2, "mm"), pos = max(l))
}
if (!is.null(g)) {
grid::grid.newpage()
grid::grid.draw(g)
}
return(g)
}
Testing
company_top <- tag_facet_flex(company, position = 'top')

company_right <- tag_facet_flex(company, position = 'right')

company_both <- tag_facet_flex(company, position = 'both')
#> [1] "Use default tagging option: both top and right sides"
