get viewport positions for facet labels?

456 views
Skip to first unread message

Joshua Wiley

unread,
Jul 8, 2012, 1:02:11 PM7/8/12
to ggplo...@googlegroups.com
Hi,

I was trying to come up with a way to add another layer of facet
labels to a graph. Here is a little example:

require(ggplot2)
require(grid)
p <- ggplot(mtcars, aes(hp, mpg)) + geom_point() + facet_grid(am ~ vs)
p

and on the top and right strip labels, I would like to add an outer
strip (spanning both columns or both rows, respectively) saying "am"
or "vs". I can see the four viewports I want:

grob <- ggplotGrob(p)

grep("strip", grid.ls(grob)$name, value = TRUE)

presumably, I should be able to extract absolute position and
width/height of those, right? I am thinking to use that information
to properly align another viewport using grid that would contain the
additional "strip" and labels. Something like:

x <- grid.layout(2, 2,
widths = unit(c(.95, .05), "npc"),
heights = unit(c(.05, .95), "npc"))

grid.show.layout(x)

grid.newpage()
pushViewport(viewport(layout = x))
pushViewport(viewport(layout.pos.row = 2, layout.pos.col = 1))
grid.draw(grob)

popViewport()

pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
grid.rect(gp=gpar(fill="grey"))
grid.text("vs")
popViewport()

pushViewport(viewport(layout.pos.row = 2, layout.pos.col = 2))
grid.rect(gp=gpar(fill="grey"))
grid.text("am", rot=-90)

but not so darn ugly. I'm struggling to access information from the
grob though even though I can "see" the names. Anyone have experience
doing something similar or pointers for mucking with an already made
plot?

Thanks!

Josh

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

Winston Chang

unread,
Jul 8, 2012, 3:57:07 PM7/8/12
to Joshua Wiley, ggplo...@googlegroups.com
This doesn't answer your question about viewports, but it might address the issue of facet titles/headers:

I've created a branch here that gives a very, very rough idea of what needs to be added to add facet titles. I've only added in code for the top labels, but the side labels should work the same way.
https://github.com/wch/ggplot2/tree/facet-headers

This code will generate the attached output.
ggplot(mtcars, aes(wt,mpg)) + geom_point() + facet_grid(am~cyl)


Properly implementing it would require actually adding the labels, and it would warrant adding theme elements (like strip.title.x and .y, and strip.title.background).

-Winston
facet-title.png

Joshua Wiley

unread,
Jul 11, 2012, 12:30:14 AM7/11/12
to Winston Chang, ggplo...@googlegroups.com
Hi Winston,

Thankts for the response. That does seem nicer :) With theme
elements, I think this would be great. I would still be interested in
any pointers on the viewport issue (both for this and because I think
it would allow more flexible altering of ggplot2 graphs). I read over
the changes in code from that branch, but creating seems to work
rather differently from at least how I envisioned modifying a plot.

Thanks,

Josh

MrKite Pepper

unread,
Dec 3, 2012, 6:10:06 AM12/3/12
to ggplo...@googlegroups.com, Joshua Wiley
And how hard is making that on the axis X model for exemple:

Reply all
Reply to author
Forward
0 new messages