3

I use ggplot to make most of my graphics. These can be single panels, or faceted. To make it easier to track revisions, I would like to generate a small label in the corner of the plot that includes some text.

In pseudo code, I am looking for something like this:

# generate the initial plot
p <- ggplot()
# add the label
p + someAnnotationFunction(label = "Version 1.0", x = 1, y = 0, 
                           hjust = "right", vjust = "bottom" )
# print
print(p) 

Or: plot my label nestled in the lower right corner of my figure without messing up the existing ggplot graphics.

So far I'm not having any luck finding a solution. This (very interesting) method doesn't work if you have a full m x n table of facets. Methods using gridExtra tend to mess with the plots too much. So, does anyone have a way to add arbitrary text anywhere on a plot that was generated using ggplot?

Community
  • 1
  • 1
Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
  • [annotate](http://docs.ggplot2.org/0.9.3/annotate.html) might be what you're looking for – hrbrmstr Apr 23 '14 at 23:20
  • @hrb good idea, but `annotate()` applies to all facets at the same time. I want a single label even when I have lots of facets, if possible. – Andy Clifton Apr 23 '14 at 23:21
  • Missed that part. Apologies. [This](http://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2) might be what you need, then. – hrbrmstr Apr 23 '14 at 23:24
  • 4
    in what sense does gridExtra mess with the plots? I was about to [suggest this](http://stackoverflow.com/a/16375160/471093): `grid.arrange(qplot(1,1), sub="Version 1.0")` – baptiste Apr 23 '14 at 23:44
  • @baptiste it doesn't allow a direct overlay (as far as I can tell). But it does the job I need, and it's eminently customizable. [This](http://stackoverflow.com/questions/17059099/saving-grid-arrange-plot-to-file) helped a lot with saving the resultant graphics. If you add this as an answer, I'll accept it. Alternatively, mark my question as a duplicate! – Andy Clifton Apr 24 '14 at 04:06

2 Answers2

2

Here's a worked solution using gridExtra(), based on Baptiste's comment:

require("ggplot2")
require("gridExtra")

# set our working directory 
working.dir <- '/Users/aclifton/Documents/projects/Rcode' 
setwd(working.dir)

# create a data frame
df <- data.frame(x =runif(100, 1, 10),
                 y = runif(100, 1, 10))
#create a plot
p <- ggplot(data = df,
            aes(x = x,
                y = y)) +
  geom_point()

print(p)        

We now have our plot, and the trick is adding that label and saving the overall plot using ggsave():

# label to show
sub.label = textGrob("Some kind of label", 
               gp=gpar(fontsize=6),
               x = unit(1, "npc"),
               hjust = 1,
               vjust = 0)

ggsave(filename=file.path(working.dir,'DemoPlot.png'),
       plot = arrangeGrob(p,
                          sub = sub.label,
                          clip = FALSE),
       scale = 1,
       width = 6.5,
       height = 3.5, 
       units = c("in"),
       dpi = 300)

Which gives you this: enter image description here

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
1

By making a data frame of your annotations, you can add them on top of your plot using geom_text.

note <- data.frame(xVarName = c(1, 5), yVarName = c(1, 10), 
    text = c("Version 1.0", "April 26, 2014")

 p + geom_text(data = anno, aes(label = text))

"Version 1.0" will show up in the bottom left and "April 26, 2014" will show up in the top right.

By making your notes in a separate dataframe, you can add multiple notes to one graph if desired.

A Toll
  • 647
  • 7
  • 15
  • If you have facets, adding the variable associated with your facet in the note data frame will allow separate annotations for facets. – A Toll Apr 27 '14 at 04:23
  • Nice idea. I like the approach and its closer to the idea of a directly overlaid label, but it's not as flexible as the functions included in gridExtra. – Andy Clifton Apr 27 '14 at 16:37