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

FR: lvls_revalue should accept expressions #124

Closed
ashiklom opened this issue Apr 7, 2018 · 1 comment
Closed

FR: lvls_revalue should accept expressions #124

ashiklom opened this issue Apr 7, 2018 · 1 comment

Comments

@ashiklom
Copy link

ashiklom commented Apr 7, 2018

For ggplot, I often want to make my labels use mathematical expressions, and the most straightforward way to do this is to modify the underlying factor levels.

x <- factor(c("a", "b", "c"))
my_units <- c(expression(g ~ cm^{-2}), expression(m^2), expression(mu * g ~ cm^{-2}))

This works exactly as expected:

levels(x) <- my_units

However, this currently fails with an error because my_units is not a character vector.

x <- forcats::lvls_revalue(x, my_units)

I recognize why there is value in forcing levels to be character vectors...but perhaps there could be another function (lvls_revalue2?) that is not as strict, and is basically just a pipe-friendly alias for levels<-?

@hadley
Copy link
Member

hadley commented Jan 4, 2019

That just turns the expressions into strings, so creating them as expressions in the first place didn't really do anything:

x <- factor(c("a", "b", "c"))
my_units <- c(expression(g ~ cm^{-2}), expression(m^2), expression(mu * g ~ cm^{-2}))
levels(x) <- my_units
levels(x)
#> [1] "g ~ cm^{\n    -2\n}"      "m^2"                     
#> [3] "mu * g ~ cm^{\n    -2\n}"

Created on 2019-01-04 by the reprex package (v0.2.1.9000)

@hadley hadley closed this as completed Jan 4, 2019
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

No branches or pull requests

2 participants