r-quantities / units

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

Allows units objects to define ggplot2 aesthetics other than x and y #369

Open agila5 opened 1 month ago

agila5 commented 1 month ago

Dear all,

I decided to create this issue since I would like to ask a question (and probably request a new feature). I noticed that after https://github.com/r-quantities/units/issues/164 we can use units objects to define the x and y axis in a ggplot2 thanks to the implementation of scale_x_units and scale_y_units. For example:

library(ggplot2)
library(units)
#> udunits database from C:/Users/user/AppData/Local/R/win-library/4.3/units/share/udunits/udunits2.xml

dat <- data.frame(
  x = set_units(1, m), 
  y = set_units(1, m), 
  z = 1
)

ggplot(dat) + 
  geom_point(aes(x, y, col = z))

However, as soon as I specify that the variable z is recorded with a given unit of measurement, the same code fails

dat$z <- set_units(dat$z, "m")
ggplot(dat) + 
  geom_point(aes(x, y, col = z))
#> Error in Ops.units(x, range[1]): both operands of the expression should be "units" objects

Created on 2024-05-25 with reprex v2.0.2

The problem is that something like scale_colour_units is missing, right? Are there any particular difficulties in implementing it? I'm not really familiar with the ggplot2 internals but, if that's the case, I can try creating a PR sometimes during summer.

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16 ucrt) #> os Windows 11 x64 (build 22631) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United Kingdom.utf8 #> ctype English_United Kingdom.utf8 #> tz Europe/Rome #> date 2024-05-25 #> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.3) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.1) #> curl 5.2.1 2024-03-01 [1] CRAN (R 4.3.3) #> digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.3) #> dplyr 1.1.4 2023-11-17 [1] CRAN (R 4.3.1) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.3.3) #> fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.2) #> farver 2.1.2 2024-05-13 [1] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1) #> fs 1.6.4 2024-04-25 [1] CRAN (R 4.3.3) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.1) #> ggplot2 * 3.5.1.9000 2024-05-25 [1] Github (tidyverse/ggplot2@28aec3a) #> glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.2) #> gtable 0.3.5 2024-04-22 [1] CRAN (R 4.3.1) #> highr 0.10 2022-12-22 [1] CRAN (R 4.3.1) #> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.3) #> knitr 1.46 2024-04-06 [1] CRAN (R 4.3.3) #> labeling 0.4.3 2023-08-29 [1] CRAN (R 4.3.1) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1) #> munsell 0.5.1 2024-04-01 [1] CRAN (R 4.3.3) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.1) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.1) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.1) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1) #> Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.1) #> rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1) #> rmarkdown 2.26 2024-03-05 [1] CRAN (R 4.3.3) #> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.1) #> scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.1) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.1) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.1) #> units * 0.8-5.4 2024-04-27 [1] Github (r-quantities/units@56c0b9d) #> utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1) #> vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.2) #> withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1) #> xfun 0.43 2024-03-25 [1] CRAN (R 4.3.3) #> xml2 1.3.6 2023-12-04 [1] CRAN (R 4.3.2) #> yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.2) #> #> [1] C:/Users/user/AppData/Local/R/win-library/4.3 #> [2] C:/Program Files/R/R-4.3.1/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
edzer commented 1 month ago

That would be great, and have the legend title z [m]!