I'm looking for a way to add some text (a, b, c, I, II, etc.) outside of the strips of a faceted plot. I know it would be possible to use annotate
or geom_text
to add text inside each facet but I'd prefer not to do so because sometimes the text can overlap the data.
This is the desired output. Any help would be appreciated!
Reproducible code with made-up data
library(ggplot2)
dat1 <- structure(list(Year = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L), .Label = c("2001", "2002", "2003"), class = "factor"),
Company = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L), .Label = c("AAA", "BBB", "CCC", "DDD"), class = "factor"),
Rank = c(1, 5, 2, 3, 4, 3, 2, 2, 5, 4, 3, 1), Rank_rev = c(5,
1, 4, 3, 2, 3, 4, 4, 1, 2, 3, 5), Group = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Group1",
"Group2"), class = "factor")), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
company <- ggplot(dat1, aes(x = Year, y = Rank_rev)) +
facet_grid(Group ~ Company, space = "free", scales = "free", switch = 'y') +
geom_col(aes(fill = Company)) +
geom_text(
aes(x = Year, y = Rank_rev,
label = Rank),
nudge_y = 0.5,
size = 5,
hjust = 0.5,
color = "black") +
scale_fill_brewer(palette = "Set2") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = expand_scale(mult = c(0, 0.1))) +
theme_bw() +
theme(axis.text.y = element_blank(),
strip.placement = "outside") +
theme(legend.position = "bottom")
company