0

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")

enter image description here

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.

enter image description here

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))

enter image description here

  • 1
    Did you see this question? https://stackoverflow.com/questions/53167616/annotate-outside-plot-area-once-in-ggplot-with-facets – MrFlick Dec 11 '20 at 00:01
  • Yes. It doesn't work for me. My hunch is that this is due to the fact that my x-varible is not numeric (it is a character variable--factors work the same). I think that the clip="off" destroys the bars, like in the image I've added above. – user3029412 Dec 11 '20 at 00:35
  • Okay, it now works for me. but I had to drop the limits. See the edit above. Thanks for your help!! – user3029412 Dec 11 '20 at 00:44
  • Okay, thanks. I will. – user3029412 Dec 11 '20 at 08:04
  • So, I was able to use the response linked to by MrFlick, and tweak it a bit. I am assuming that because my x-variable was not numeric that including an xlim was what was creating the problem in the second image above. Here is the full code (with extra lines of text) and the result: Oh, I've also shown how to add more than one line of text, and to left-justify the text. – user3029412 Dec 11 '20 at 08:22

1 Answers1

2

For completion, here is my final code. On the suggestion of Mr.Flick, I had initially tried using the answer found here. That gave me the second image in my original question above--the one without bars, but with random lines. I am assuming it was because the xlim code was throwing off my plot, given that my x-variable was not numeric. By removing the xlim part, I was able to get what I wanted.

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",
       tag ="My first tag line\nSecond tag line") +
  theme(plot.title = element_text(hjust = 0.5, size=12),
        plot.subtitle = element_text(hjust=0.5, size=12),
        plot.margin = margin(1, 4, 1, 1, "lines"),
        plot.tag.position = c(.91,.6),
        plot.tag = element_text(hjust =0, size=9)) + 
  scale_y_continuous(expand = c(0, 0), limits=c(0,250)) +
  facet_nested(~top.nest + bottom.nest, scales="free_x", space="free") +
  coord_cartesian(clip = "off")

enter image description here