22

What is the best way to add a footnote to the bottom of a plot created with ggplot2? I've tried using a combination of the logic noted here as well as the ggplot2 annotate function.

p + annotate("text",label="Footnote",
  x=unit(1,"npc") - unit(2, "mm"),y=unit(2, "mm"),
  just=c("right", "bottom"),gp=gpar(cex= 0.7, col=grey(.5)))

but I am getting the error

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class c("unit.arithmetic", "unit") into a data.frame

Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
user338714
  • 2,315
  • 5
  • 27
  • 36
  • This works if you view the plot in R but it doesn't look like it works with the ggsave function. – user338714 Jun 14 '10 at 18:18
  • Then open an appropriate graphics device instead of using `ggsave()`, e.g., `pdf("filename.pdf", width=10, height=6); print(p); grid.text(...); dev.off()` – rcs Jun 14 '10 at 18:37

2 Answers2

36

labs(caption = "my caption") adds a footnote:

ggplot(mtcars, aes(mpg, wt, colour = cyl)) + 
  geom_point() + 
  labs(caption = "(Pauloo, et al. 2017)")

enter image description here

Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • 1
    Can we make the caption left-aligned? – akash ansari Jan 29 '21 at 20:48
  • 2
    Try `+ theme(plot.caption = element_text(hjust = 0))`. For a centered legend, use `just = 0.5`. By default, `hjust = 1`. `hjust` stands for "horizontal justification," and ranges from 0-1, left to right, so that 0 is all the way left, 1 is all the way right, and 0.5 is in the middle, and so on. – Rich Pauloo Jan 30 '21 at 00:18
14

I would use something like that:

pdf("filename.pdf", width=10, height=6) # open an appropriate graphics device
print(p)
makeFootnote() # from webpage above (uses grid.text; ggplot2 is based on grid)
dev.off()
rcs
  • 67,191
  • 22
  • 172
  • 153
  • 2
    Yes, this is excellent. Also, if you need ggplot2 to leave a little more margin for your footnote, try this: p + theme(plot.margin = unit(c(1,1,2,1), "lines")) – Owen Jan 28 '14 at 18:56