d facet labels using a labeller in ggplot2 >= 2.0

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 .


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()) .

链接地址: http://www.djcxy.com/p/33538.html

上一篇: T中的T(泛型)实例

下一篇: 在ggplot2> = 2.0中使用标签的d facet标签