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
agila
3,5472 gold badges12 silver badges27 bronze badges
lang-r
color = as.numeric(z)?unitspackage since version 0.8.0, and this resolved issues shows use ofunits::scale_x_units.scale_x_unitsandscale_y_units, but ignore the other scales. I would suggest opening an issue in theunitspackage.