Quick way to add annotations to your ggplot graphs

Lately, some of the graphs I have been working on have “strange/erratic” values, so I thought to plot those values in a different color and rather than adding an extra legend line I have decided to add a note to explain the difference. Among the many options available, I have found a very quick and harmless way to add annotations to ggplot graphs. It uses the library “gridExtra”, which employs user-level functions that work with “grid” graphics and draw tables.

1.load ggplot2 library
2. and save your graph as “my_graph”:
my_graph<- qplot(wt, mpg, data = mtcars)
3. load gridExtra and add the text to the graph. Note that x, hjust and vjust give the position of the text in the outer margins. If you want to annotate INSIDE the graph, use annotate:
g <- arrangeGrob(p, sub = textGrob("I pledge my life and honor to the Night's Watch, \nfor this night and all the nights to come.", x=0, hjust=-0.1, vjust=0.1,gp = gpar(fontface = "italic", fontsize = 10)))

5. save the graph
ggsave("my_graph_with_note.pdf", g, width=5,height=5)







here is the graph I have been working on:



Author: acarioli

is a PostDoc at the Geography and Environment department of the University of Southampton, WorldPop project team. She is also affiliated researcher at CED, UAB and Dondena Centre. Her interests include spatial econometrics and modeling, bayesian methods, machine learning processes, forecasting, micro-data simulation, and data visualization. Demo-traveler, Mac enthusiast, R zealot and Rladies member.