Luc
Luc

Reputation: 445

How to add a custom label to facet_grid()

I'm trying to add a a custom facet label to a plot that was facetted with facet_grid() as follows:

p <- qplot(wt, mpg, data = mtcars)
p <- p + facet_grid(. ~ vs, labeller = label_bquote(alpha^a==alpha^b))

This still works fine. However, when I add the variable on which I'm splitting to the equation in the facet label, like this:

p <- qplot(wt, mpg, data = mtcars)
p <- p + facet_grid(. ~ vs, labeller = label_bquote(alpha^a==alpha^b==.(x)))

I'm getting the following error:

Error: unexpected '==' in " p <- p + facet_grid(. ~ vs, labeller = label_bquote(alpha^a==alpha^b=="

Could someone help me out on this seemingly trivial problem?

Upvotes: 4

Views: 1348

Answers (2)

shadow
shadow

Reputation: 22293

This will work if you just add appropriate brackets.

p <- p + facet_grid(. ~ vs, labeller = label_bquote({alpha^a==alpha^b}==.(x)))

Upvotes: 2

alexwhan
alexwhan

Reputation: 16026

It's not that you're adding the variable, it's the second == that causes the problem. This is an issue with the way R parses the operators. You can control what R sees with {}:

p <- p + facet_grid(. ~ vs, labeller = label_bquote({alpha^a==alpha^b}==.(x)))

enter image description here

Upvotes: 6

Related Questions