How can I use grid to edit a ggplot2 object to add math expressions to facet labels?
Asked Answered
S

2

6

I need to put Greek letters into facet labels using facet_wrap() in ggplot2. I found a Link describing the same for facet_grid(). I applied this for my data, using the following code:

levels(parameters) <- c(expression(alpha), expression(beta))  
p + facet_grid(.~parameters, labeller = label_parsed)

This works great and does exactly what I want. However, I need to use facet_wrap() instead (to get separate y-axes for both paramters, and also to plot even more parameters in different columns and rows). I tried the following:

p + facet_wrap(.~parameters, labeller = label_parsed)  ,  or  
p + facet_wrap(.~parameters)

but this didn't work because there is no "labeller" function in facet_wrap. How could this be done using grid?

Scrawly answered 29/6, 2011 at 19:4 Comment(3)
This thread on the ggplot2 mailing list suggests this feature isn't supported (unless you know how to delve into grid objects). That was a while back (Dec 2009) but I'm fairly certain it's still unresolved.Swinish
Thanks! Do you have a suggestion about what grid-function I may look at to get access to the facet labels? Again, thanks much for your help!Scrawly
I don't, sorry, I really need to learn more about grid myself. But there are definitely folks around here (or the ggplot2 mailing list) who'd know. You might try editing this question (or asking a new one) to be more like: 'How can I use grid to edit a ggplot2 object to add math expressions to facet labels?'.Swinish
K
9

This example should get you started:

library("ggplot2")
library("grid")

d <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
            geom_point() +
            facet_wrap(~Species)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name

grob <- grid::editGrob(grob, strip_elem[1], label=expression(alpha[1]))
grob <- grid::editGrob(grob, strip_elem[2], label=expression(beta^2))
grob <- grid::editGrob(grob, strip_elem[3], label=expression(hat(gamma)))

grid.draw(grob)

modified grob

Update: this works with ggplot2 version 0.9.3 (although using grid is a fragile way to modify ggplot2 graphics)

grob[["grobs"]][["strip_t.1"]][["children"]][[2]][["label"]] <- expression(alpha[1])
grob[["grobs"]][["strip_t.2"]][["children"]][[2]][["label"]] <- expression(beta^2)
grob[["grobs"]][["strip_t.3"]][["children"]][[2]][["label"]] <- expression(hat(gamma))
grid.draw(grob)
Kuehnel answered 30/6, 2011 at 19:18 Comment(2)
geditGrob has been removed from ggplot2: github.com/hadley/ggplot2/commit/…Kuehnel
This example is failing for me with 0.9.3.1 with Error in getGrob(grob, "strip.text.x", grep = TRUE, global = TRUE) : It is only valid to get a child from a 'gTree'. Halvaard
W
0

I tried it using the code below, and it works for me.

library("ggplot2")
library("grid")

## 1. Plot d and print d
d <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + facet_wrap(~Species)
d

## 2. Put Greek letters into facet labels using grid.edit function
grid.force()
grid.ls() #You should find the exact names for facet labels in the printed output!
grid.edit("GRID.text.77",label=expression(alpha[1])) #"GRID.text.77" = name for the first facet label
grid.edit("GRID.text.80",label=expression(beta^2)) #"GRID.text.80" = name for the second facet label
grid.edit("GRID.text.83",label=expression(hat(gamma))) #"GRID.text.83" = name for the third facet label
Warp answered 25/3, 2022 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.