使用ggplot2> = 2.0中的贴标机混合空和bquote-d facet标签

时间:2016-04-13 01:16:20

标签: r ggplot2

在2.0之前,在ggplot2中,我可以使用element_blanklabeller来标记facet_grid中的行或列,例如:

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

现在,对于ggplot2版本2.0,这不起作用,给出

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

虽然improvements to label_bquote很棒,但有没有办法让labeller完成上述工作?

我尝试过:

1)传递NULL,但方面默认为label_value(根据?label_bquote

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

2)传递element_blank,但方面说element_blank()

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

3)将element_blank包裹在as_labeller

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

注意可以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())

但我想用labeller

来做

1 个答案:

答案 0 :(得分:4)

您可以rows=""rows = ` `。这将删除标签,但你仍然会有一个空白的灰色条。

为清楚起见,这是完整的命令:

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

您当然可以通过添加theme(strip.text.y = element_blank())来消除灰色条带。