I am attempting to copy an answer from a previous stackoverflow post (Multi-row x-axis labels in ggplot line chart) however, I would like to apply this to a multi-facetted plot.
The toy data.
df <- data.frame(START = rep(seq(1:10),2),
SAMPLE = rep(c("A", "B"), each=10),
yaxis = runif(20, min = 1, max = 10),
xaxis1 = rep(c("E1", "E1", "E2", "E2", "E1", "E2", "E3", "E4", "E1", "E2"),2),
xaxis2 = rep(c("G1", "G1", "G1", "G1", "G2", "G2", "G2", "G2", "G3", "G3"),2))
Here I would like xaxis1 to be present for each tick, and xaxis2 to be appear only once - similar to how the YEAR-QRT plot looks in the post above.
Without any annotations the plots looks as so.
ggplot(data = df, aes(x = interaction(START,xaxis1, xaxis2), y = yaxis,
group = 1, fill=yaxis)) +
geom_col(width = 1, position = "identity") +
facet_wrap(SAMPLE ~., ncol= 1, strip.position="left")
When I attempt to add the annotation code ontop of the plot I get unequal parameter length error.
ggplot(data = df, aes(x = START, y = yaxis,
group = 1, fill=yaxis)) +
geom_col(width = 1, position = "identity") +
annotate(geom = "text", x = seq_len(nrow(df)), y = 34,
label = df$xaxis1, size = 4)+
annotate(geom = "text", x = seq_len(nrow(df)), y = 32,
label = unique(df$xaxis2), size = 6)