math symbols facet_wrap (facet labels)

1,519 views
Skip to first unread message

Tyler Rinker

unread,
Sep 29, 2012, 4:10:11 PM9/29/12
to ggplot
I asked this same question on stack overflow with no response:



Tyler Rinker

unread,
Sep 29, 2012, 4:16:04 PM9/29/12
to ggplot

Apparently my 2 year old daughter thought my email was ready for submission, my apologies.

Let me start over...

I asked this same question at stackoverflow with no response:

Since the update to 0.9.0 the old trick (LINK) for adding math symbols to labels in facet_wrap no longer works.


library(ggplot2); library(grid)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) 
    xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d <- d + facet_wrap(~ color, ncol = 4)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
grid.draw(grob)


How can we make math annotations to labels in facet grid?
Why does facet_grid not operate like facet wrap with respect to math annotations since they are similar in other respects?

Thank you,
Tyler Rinker

PS often the text in my emails comes through messed up so here is a link to a text version of this question: https://dl.dropbox.com/u/61803503/quest.txt


From: tyler_...@hotmail.com
To: ggp...@googlegroups.com
Subject: math symbols facet_wrap (facet labels)
Date: Sat, 29 Sep 2012 16:10:11 -0400


I asked this same question on stack overflow with no response:




--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Winston Chang

unread,
Oct 4, 2012, 10:53:00 PM10/4/12
to Tyler Rinker, ggplot
To get math expressions in facet_grid labels, you can use
labeller=label_parsed:

m <- mpg
levels(m$drv) <- c("x[1]", "y^2", "z+3")

ggplot(m, aes(x = displ, y = cty)) + geom_point() +
facet_grid(. ~ drv, labeller = label_parsed)


But this doesn't work with facet_wrap -- it's kind of complicated, but
the root reason it doesn't work has to do with the way the facets are
generated when you have two faceting variables. Notice the difference
between these two:

ggplot(mpg, aes(x = displ, y = cty)) + geom_point() +
facet_grid(. ~ drv + fl)

ggplot(mpg, aes(x = displ, y = cty)) + geom_point() +
facet_wrap( ~ drv + fl)

More information on why it won't work here:
https://github.com/hadley/ggplot2/pull/656

-Winston
facet_grid_expressions.png

Tyler Rinker

unread,
Oct 5, 2012, 12:02:37 AM10/5/12
to winsto...@gmail.com, ggplot
Winston,

Thank you for your response.  It sounds like this fix is planned for some future version.  Is there a temporary hackish way to achieve it now (maybe via grid)?

Cheers,
Tyler

Winston Chang

unread,
Oct 5, 2012, 12:20:38 AM10/5/12
to Tyler Rinker, ggplot
Hm... one option is to install the branch from the pull request. If
you have the latest version of devtools, you can do this:
install_github('ggplot2', pull=656, ref=NULL)

It may also require the devel version of scales - I'm not sure.


Another option is to use facet_grid, then use gtable to pull it apart
and put the pieces back together in another gtable.

These recent discussions might give you an idea of how that would be done:
https://groups.google.com/forum/#!msg/ggplot2/ig5hM0F6ewc/o89T8U2SPzQJ
https://groups.google.com/forum/#!msg/ggplot2/TCRmlKRF4ys/5j1IC7LpIsoJ


Both of these options are hacks. They're good for getting it done now,
but not for the long run.

-Winston
Reply all
Reply to author
Forward
0 new messages