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/