I wonder how to change the facet
label to math formula in ggplot2
.
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d + facet_wrap(~ color, ncol = 4)
For example, I want to change facet label from D
to Y[1]
, where 1 is subscript. Thanks in advance for your help.
I found this answer but it does not work for me. I'm using R 2.15.1
and ggplot2 0.9.1
.
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
For that, we use theme() function, which is used to customize the appearance of plot. We can change size of facet labels, using strip. text it should passed with value to produce labels of desired size.
Facet labelsSetting strip. text to element_blank() will remove all facet labels. You can also remove the labels across rows only with strip.
facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.
You can edit the grobs in the gtable,
ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + facet_wrap(~ color, ncol = 4)
for(ii in 1:7)
grid.gedit(gPath(paste0("strip_t-", ii), "strip.text"),
grep=TRUE, label=bquote(gamma[.(ii)]))
alternatively, if you want to save a grob,
g <- ggplotGrob(d)
gg <- g$grobs
strips <- grep("strip_t", names(gg))
for(ii in strips)
gg[[ii]] <- editGrob(getGrob(gg[[ii]], "strip.text",
grep=TRUE, global=TRUE),
label=bquote(gamma[.(ii)]))
g$grobs <- gg
using ggsave would require extra (ugly) work, since one has to fool the test for class ggplot... I reckon it will be easier to call pdf() ; grid.draw(g); dev.off()
explicitly.
I made a small correction and wrapped it in a function:
facet_wrap_labeller <- function(gg.plot,labels=NULL) {
#works with R 3.0.1 and ggplot2 0.9.3.1
require(gridExtra)
g <- ggplotGrob(gg.plot)
gg <- g$grobs
strips <- grep("strip_t", names(gg))
for(ii in seq_along(labels)) {
modgrob <- getGrob(gg[[strips[ii]]], "strip.text",
grep=TRUE, global=TRUE)
gg[[strips[ii]]]$children[[modgrob$name]] <- editGrob(modgrob,label=labels[ii])
}
g$grobs <- gg
class(g) = c("arrange", "ggplot",class(g))
g
}
This allows to print nicely and even ggsave
can be used.
Perhaps somebody has changed the name of the edit-Grob function at some point. (Edit: It was removed by @hadley about 8 months ago.) There is no geditGrob
but just editGrob
from pkg:grid seems to work:
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
#Note: changes in ggplot2 functions cause this to fail from the very beginning now.
# Frank Harrell's answer this year suggests `facet_warp` now accepts `labeller`
d <- d + facet_wrap(~ color, ncol = 4)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
#strip.text.x.text.1535
#strip.text.x.text.1541
#strip.text.x.text.1547
#strip.text.x.text.1553
#strip.text.x.text.1559
#strip.text.x.text.1565
#strip.text.x.text.1571
grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
grid.draw(grob)
As of ggplot2
2.1.0 labeller
has been implemented for facet_wrap
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With