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
Fix plot labels with spaces #298
Conversation
Examples: library(units)
#> udunits database from /usr/share/udunits/udunits2.xml
library(ggplot2)
mtcars$consumption <- set_units(mtcars$mpg, mi / gallon)
mtcars$power <- set_units(mtcars$hp, hp)
plot(consumption~power, mtcars, xlab="asdf asdf") p <- ggplot(mtcars) + geom_point(aes(power^2, consumption))
p + xlab("some^2 custom stuff") p + xlab(NULL) Created on 2022-01-20 by the reprex package (v2.0.1) |
Codecov Report
@@ Coverage Diff @@
## main #298 +/- ##
=======================================
Coverage 92.24% 92.25%
=======================================
Files 19 19
Lines 916 917 +1
=======================================
+ Hits 845 846 +1
Misses 71 71
Continue to review full report at Codecov.
|
Thank you for your effort @Enchufa2 I have played around a bit with specifying the labels, spaces and expressions work as expected. The results for special characters in the string (e.g. require(units)
#> Loading required package: units
#> udunits database from /usr/share/xml/udunits/udunits2.xml
d<-data.frame(b=1:5,a=set_units(5:1,'Hz'))
require(ggplot2)
#> Loading required package: ggplot2
ggplot(d)+geom_point(aes(a,b))+xlab("a b")+ylab("a b") ggplot(d)+geom_point(aes(a,b))+xlab("a\nb")+ylab("a\nb") ggplot(d)+geom_point(aes(a,b))+xlab("a\tb")+ylab("a\tb") ggplot(d)+geom_point(aes(a,b))+xlab(expression(frac(a,b)))+ylab(expression(frac(a,b))) Created on 2022-01-20 by the reprex package (v2.0.1) |
Thanks for testing. To be able to properly show powers, unit labels must be expressions, and thus we lose the ability to insert a newline or a tab character. |
Thanks those new lines could be solved by expressions anyway |
There are projects using |
Done, so if no further comments from @edzer, we are good to go. :) |
Looks good to me - great work! |
Closes #297. @edzer I simplified a bit the logic to add units to labels, with some changes:
I don't think we should exportSee below.make_unit_label
anymore. This was probably exported to enableggforce
to set the labels easily, right? But it was not used correctly anyway. Now that we have includedggforce
's functionality, I have renamed the function asmake_unit_label_internal
and deprecated direct calls tomake_unit_label
(which calls the former one) if this is ok with you. Or are you aware of any other package really requiringmake_unit_label
?make_unit_label
, which replaces spaces with~
if the label is a string.xlab
,ylab
). Instead, I think that we should always add units, as implemented in this PR. And if the user do not want units, they should just drop them.