Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mix empty and bquote-d facet labels using a labeller in ggplot2 >= 2.0

Tags:

r

ggplot2

Prior to the 2.0, in ggplot2 I could use element_blank and labeller to label only rows or columns in facet_grid, like:

library(ggplot2)
g <- ggplot(mtcars) + geom_point(aes(mpg, cyl))
g + facet_grid(vs ~ gear, labeller=labeller(vs = element_blank(),
                                             gear = label_bquote(mu == .(x))))

Now, with ggplot2 version 2.0, this doesn't work, giving

Error in if (attr(labels, "facet") == "wrap") { :
  argument is of length zero
Calls: <Anonymous> ... lapply -> FUN -> <Anonymous> -> x -> resolve_labeller

Although the improvements to label_bquote are great, is there any way to make the above work with a labeller?

I've tried:

1) passing NULL, but facets default to label_value (as per ?label_bquote)

  g + facet_grid(vs ~ gear,
               labeller = label_bquote(
                            rows = NULL,
                            cols = mu == .(gear)))

2) passing element_blank but facets say element_blank()

g + facet_grid(vs ~ gear,
           labeller = label_bquote(
                        rows = element_blank(),
                        cols = mu == .(gear)))

3) wrapping element_blank in as_labeller

g + facet_grid(vs ~ gear, labeller=labeller(.rows = as_labeller(element_blank()),
                                            .cols = label_bquote(mu == .(gear))))

Note it is possible to remove facet labels after the fact using theme

g + facet_grid(vs ~ gear,
               labeller = label_bquote(cols = mu == .(gear))) +
    theme(strip.text.y = element_blank())

But I'd like to do it with a labeller.

like image 863
jaimedash Avatar asked Apr 13 '16 01:04

jaimedash


1 Answers

You can do rows="" or rows = ` `. This will get rid of the labels, though you'll still have a blank gray strip.

For clarity, this is the full command:

 g + facet_grid(vs ~ gear,
                labeller = label_bquote(rows = "",
                                        cols = mu == .(gear)))

You can of course also get rid of the gray strip by adding theme(strip.text.y = element_blank()).

like image 120
eipi10 Avatar answered Sep 30 '22 02:09

eipi10