I've literally spend a couple of days searching for the correct answer to this. I want to place additional text/annotation on the outside of the plot area--above or below the legend--on a geom_bar that has nested facets like below.
Some things I've tried: annotate_custom, and annotate don't work because they add an annotation in every individual facet. Grid.text worked (I was able to place the text correctly) but I was then unable to use ggsave (which is important) to save the completed plot.
I've also looked at answers that dealt with creating an annotation on a single facet of a multi-faceted plot. Those answers don't help because I want the text/annotation to be outside of the plotting area (above/below the legend) and not in the plotting area itself.
Thanks in advance for your help!
Here are some data:
library(ggplot2)
library(ggh4x) # for the facet_nested function
my.df<-data.frame("top.nest"=sample(c("Group.1","Group.2","Group.3"),26,rep=T),
"bottom.nest"=sample(c("Cat.1","Cat.2","Cat.3","Cat.4","Cat.5"),26,rep=T),
"my.teams"=c(LETTERS),
"quantity"=abs(round(rnorm(26,200,20))),
"my.factor"=sample(c("A","B","C"),26,rep=T))
my.plot<- ggplot(my.df, aes(my.teams, quantity, fill = my.factor)) +
geom_bar(stat = "identity") +
geom_col(width=1) +
scale_fill_manual(values=c("blue","red","green"), labels = c("A","B","C")) +
labs(title = "How do you add text/annotation either below or above the legend",
subtitle="of a facet barplot in ggplot?") +
theme(plot.title = element_text(hjust = 0.5, size=12),
plot.subtitle = element_text(hjust=0.5, size=12)) +
scale_y_continuous(expand = c(0, 0), limits=c(0,250)) +
facet_nested(~top.nest + bottom.nest, scales="free_x", space="free")
I've tried the following suggestion, which doesn't work for me. I added these three lines to the original code.
labs(tag = "XX") +
coord_cartesian(xlim = c(50, 350), ylim = c(10, 35), clip = "off") +
theme(plot.tag.position = c(.01, .95))
Whether I add it before, or after, the facet_nested command, the result is the same (see image below). My hunch is that is has to do with the fact that in the example provided in the first response to this question, the x variable is numeric. Mine is not.
EDIT: Okay, the code now works with the added 3 lines, as long as I remove the xlim=c() part. It works with a ylim=c(), or just remove all limits. It kind of makes sense given that the x-variable was not (as I earlier mentioned) numeric.
Here's the full code, and the successful plot!
my.plot<- ggplot(my.df, aes(my.teams, quantity, fill = my.factor)) +
geom_bar(stat = "identity") +
geom_col(width=1) +
scale_fill_manual(values=c("blue","red","green"), labels = c("A","B","C")) +
labs(title = "Successfully added text/annotation below the legend",
subtitle="of a facet barplot in ggplot") +
theme(plot.title = element_text(hjust = 0.5, size=12),
plot.subtitle = element_text(hjust=0.5, size=12)) +
scale_y_continuous(expand = c(0, 0), limits=c(0,250)) +
facet_nested(~top.nest + bottom.nest, scales="free_x", space="free") +
labs(tag = "XX") +
coord_cartesian(clip = "off") +
theme(plot.tag.position = c(.95, .6))