Accessing grobs in gtable

330 views
Skip to first unread message

Winston Chang

unread,
Sep 6, 2012, 10:56:43 PM9/6/12
to ggplot2-dev
Hi everyone -

A couple questions on the main ggplot2 mailing list have gotten me
thinking about how to access grobs in gtable objects. I'm looking for
some feedback about how to go about doing this.

As you may know, ggplot now generates a gtable object. Here's an
example of how to access and view a gtable:

library(ggplot2)
library(grid)

p <- qplot(1:3, 1:3, colour=c("a", "b", "c"))

# Generate the gtable grob
g <- ggplotGrob(p)

# display it
grid.newpage()
grid.draw(g)

# Print gtable
g
# TableGrob (6 x 6) "layout": 9 grobs
# (1-6,1-6) background: rect[plot.background.rect.489]
# (4-4,3-3) spacer: absoluteGrob[GRID.absoluteGrob.462]
# (3-3,4-4) panel: zeroGrob[NULL]
# (3-3,3-3) axis-l: gTree[GRID.gTree.450]
# (4-4,4-4) axis-b: absoluteGrob[GRID.absoluteGrob.456]
# (5-5,4-4) xlab: text[axis.title.x.text.464]
# (3-3,2-2) ylab: text[axis.title.y.text.466]
# (3-3,5-5) guide-box: gtable[guide-box]
# (2-2,4-4) title: text[plot.title.text.487]

# Layout information is stored in the data frame 'layout'
g$layout
# t l b r z clip name
# 9 1 1 6 6 0 on background
# 1 3 3 3 3 3 off axis-l
# 2 4 3 4 3 1 off spacer
# 3 3 4 3 4 2 on panel
# 4 4 4 4 4 4 off axis-b
# 5 5 4 5 4 5 off xlab
# 6 3 2 3 2 6 off ylab
# 7 3 5 3 5 7 off guide-box
# 8 2 4 2 4 8 off title

# Access a single grob -- this will get the first one listed in the table
g$grobs[[1]]
# rect[plot.background.rect.489]


As you can see, when you print the gtable object, it displays the name
(taken from the 'layout' column in the data frame) followed by the
type of grob, and finally the grob name (in square brackets). For
example, the first entry in the table is named in the gtable layout
"background", but the name of the grob is "plot.background.rec.489".
I'll call these the layoutName and grobName, respectively. The
layoutName is something used by gtable; the grobName is a property of
the grob itself.

Previously the grobs had grobNames like "background", "axis-l", and so
on. These are now used as the layoutNames, and the grobNames generally
aren't nice and simple like this.

Here are a couple things that seem to be more difficult now:
- I don't think there's a simple way of accessing the grobs by name.
- grid.ls() no longer produces a nice tree of the grob hierarchy.

Any thoughts on how these things could be improved, and what else
could be improved? There may be some other things I'm missing -- feel
free to chime in.


-Winston

baptiste auguie

unread,
Sep 8, 2012, 4:54:37 AM9/8/12
to Winston Chang, ggplot2-dev
Hi,

i can't find it anymore but in a previous discussion I suggested that accessing an element of a gtable be similar to subsetting a data.frame (see also https://github.com/hadley/gtable/issues/10)

You could either specify l, r, t, b, z indices, or a name with partial matching, or a mix of both (e.g. find a grob that starts with panel*, in the first row, and has z-order > 2).

Cheers,

baptiste
Reply all
Reply to author
Forward
0 new messages