r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
173 stars 27 forks source link

Fix plot labels with spaces #298

Closed Enchufa2 closed 2 years ago

Enchufa2 commented 2 years ago

Closes #297. @edzer I simplified a bit the logic to add units to labels, with some changes:

Enchufa2 commented 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)

codecov[bot] commented 2 years ago

Codecov Report

Merging #298 (66590e7) into main (729bc9c) will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           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.

bart1 commented 2 years ago

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)

Enchufa2 commented 2 years ago

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.

bart1 commented 2 years ago

Thanks those new lines could be solved by expressions anyway

Enchufa2 commented 2 years ago

There are projects using make_unit_label after all... Let's undeprecate it then. :(

Enchufa2 commented 2 years ago

Done, so if no further comments from @edzer, we are good to go. :)

edzer commented 2 years ago

Looks good to me - great work!