This is going to be difficult with ggplot2 because the creation of
viewports and grobs occur in completely separate places. Until the
grob is actually rendered, there is no way to tell how much space the
panel will have.  I think the only solution is to write a custom grid
grob that re-calculate position every time it is redrawn (this would
also mean the labels would adjust if you resized the plot).  Do you
have a copy of Paul's R graphics book?  I think that's the best
introduction to writing new grobs.  Once you have that, I can help you
write a new geom that uses the new grob.

> Actually I don't have a copy of Paul's book, but I looked at some
> pages on Google books, and it seems like the relevant information is
> in Ch.7, notably section 7.3. So if I understand you correctly I would
> have to write a new version of GeomText (I guess I would copy your
> example from geom-text.r), say GeomTextAdjusted, and replace textGrob
> with some textAdjustedGrob that has a custom drawDetails method? In
> what context/viewport would that be called in?

That's right. There are some details in vignette("grid"), which might
be enough to get you started without the book.  I don't know about the
details of the viewport the code would be called in, but it's called
every time the plot is redrawn and should be in the context of the
deepest viewport (see p. 254) for an example.Paul is usually pretty
helpful if you email him, but I think he's currently on summer
holiday.  If you get stuck, I'd be happy to take a look.

