Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facet labellers not calling default #4031

Closed
waltersom opened this issue May 27, 2020 · 1 comment · Fixed by #4032
Closed

facet labellers not calling default #4031

waltersom opened this issue May 27, 2020 · 1 comment · Fixed by #4032

Comments

@waltersom
Copy link
Contributor

I have a plot using facet_grid, that I want to set the facet labels with.
The faceting variables are stored as integers, and I am trying to use lookup tables to specify some expressions, and then pass those to label_parsed.

As I read the docs for labeller(), the lookup table should be passed to .default.

.default Default labeller for variables not specified. Also used with lookup tables or non-labeller functions.

However, this doesn't seem to be happening (note labels on rows.)

I'm not sure if this is a documentation problem, me misunderstanding something, or a problem with the labeller.


library(ggplot2)
tdf <- data.frame(x=c(1,2,3,4), y=c(4,3,2,1), a=c(0,1,0,1), b=c(1, 2, 2, 1))
ggplot(tdf, aes(x, y))+geom_point()+
  facet_grid(a~b, labeller=labeller(a=c(`0`="a^2", `1`="a[2]"), b=c(`1`="One", `2`="Two"), .default=label_parsed))

Created on 2020-05-27 by the reprex package (v0.3.0)


If I instead use .rows and .cols to name the arguments to labeller, I get the expected result.
(The labels on the rows are now passed to label_parsed)

library(ggplot2)
tdf <- data.frame(x=c(1,2,3,4), y=c(4,3,2,1), a=c(0,1,0,1), b=c(1, 2, 2, 1))
ggplot(tdf, aes(x, y))+geom_point()+
  facet_grid(a~b, labeller=labeller(.rows=c(`0`="a^2", `1`="a[2]"), .cols=c(`1`="One", `2`="Two"), .default=label_parsed))

Created on 2020-05-27 by the reprex package (v0.3.0)

@clauswilke
Copy link
Member

You're correct. The code doesn't reflect the documentation. If you check out line 440 below, the .default argument is not forwarded to as_labeller(), whereas in line 442 it is. Line 440 is run in your first example, line 442 in your second one.

ggplot2/R/labeller.r

Lines 439 to 443 in 3e35c7a

if (is.null(margin_labeller)) {
labellers <- lapply(dots, as_labeller)
} else {
margin_labeller <- as_labeller(margin_labeller, default = .default,
multi_line = .multi_line)

karawoo added a commit that referenced this issue Jun 23, 2020
* Fix labeller to pass .default for lookups (#4031)

Pass .default to as_labeller even when margin_labeller is null, such as
when using named arguements for a lookup table.

* Correct style, and add line to NEWS.md

* Move NEWS bullet into dev version section

Co-authored-by: Kara Woo <woo.kara@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants