Closed Enchufa2 closed 2 years ago
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)
Merging #298 (66590e7) into main (729bc9c) will increase coverage by
0.00%
. The diff coverage is100.00%
.
@@ Coverage Diff @@
## main #298 +/- ##
=======================================
Coverage 92.24% 92.25%
=======================================
Files 19 19
Lines 916 917 +1
=======================================
+ Hits 845 846 +1
Misses 71 71
Impacted Files | Coverage Δ | |
---|---|---|
R/plot.R | 97.22% <100.00%> (-0.08%) |
:arrow_down: |
R/scale_units.R | 89.13% <100.00%> (+0.49%) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 729bc9c...66590e7. Read the comment docs.
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. \n
and \t
) still differ. These are maybe not as critical to get right but if it is easy it might prevent errors for people.
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 make_unit_label
after all... Let's undeprecate it then. :(
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:
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
?~ See below.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.