0

I would like to create a plot where a variable (say z) defines a colour scheme for some points in a ggplot. For example:

library(ggplot2)
library(units)
#> udunits database from C:/Users/user/AppData/Local/R/win-library/4.3/units/share/udunits/udunits2.xml
set.seed(1)
dat <- data.frame(
 x = 1:5, 
 y = 1:5, 
 z = runif(5)
)
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月23日 with reprex v2.0.2

Is it possible to fix the previous code?

asked May 23, 2024 at 16:39
7
  • color = as.numeric(z)? Commented May 23, 2024 at 16:43
  • Yes, sure but, IMO, that kinda defeats the point of using the units package. Maybe this is just currently not implemented, and I can create a feature request at GitHub page of the units package. Commented May 23, 2024 at 16:45
  • 1
    And the NEWS file suggests that this has been part of the units package since version 0.8.0, and this resolved issues shows use of units::scale_x_units. Commented May 23, 2024 at 17:10
  • 2
    Hmm, yes it does seem they made a strange choice to implement scale_x_units and scale_y_units, but ignore the other scales. I would suggest opening an issue in the units package. Commented May 23, 2024 at 17:13
  • 1
    Just for reference, I opened a new issue here. Commented May 26, 2024 at 7:46

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.