r-quantities / units

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

support for scale_*_reverse() #343

Closed jkeuskamp closed 1 year ago

jkeuskamp commented 1 year ago

A feature request rather than a bug: Units facilitates the usage of ggplot with units rather seamlessly. A small shortcoming is the usage of scale_*_reverse(). This fails with

Error in Ops.units(x, range[1]) : 
  both operands of the expression should be "units" objects

because scale_*_reverse() calls scale_y_continuous(trans = scales::reverse_trans()) to invert the axis. It would be convenient if scale_*_reverse() would call scale_y_units(trans = scales::reverse_trans()) when applicable.

Enchufa2 commented 1 year ago

But this works fine:

library(units)

mtcars$consumption <- set_units(mtcars$mpg, mi / gallon)
mtcars$power <- set_units(mtcars$hp, hp)

ggplot(mtcars) +
  geom_point(aes(power, consumption)) +
  scale_x_units(unit = "W", trans="reverse")

So I'm not sure what are you proposing exactly.

jkeuskamp commented 1 year ago

It does indeed but takes some digging, but it would be more convenient if units would support all ggplot varieties of scale_*_continous. I agree it is not a very pressing issue though 👍

Enchufa2 commented 1 year ago

So you propose having scale_x_units_reverse, scale_x_units_log10 and so on? Just to clarify, because clearly we cannot make scale_x_reverse call scale_x_units, because scale_x_reverse is, by definition, an alias of scale_x_continuous with that transformation applied.

jkeuskamp commented 1 year ago

I would propose to edit the documentation of scale_units to say that the transformation convenience functions included with ggplot (scalelog10()⁠, ⁠ scalesqrt()⁠, scale_reverse()⁠, are not supported and include an example like:

transform axes using the trans argument

ggplot(mtcars) + geom_point(aes(power, 1 / consumption)) + scale_y_units(trans= reverse_trans()) }

From: Iñaki Ucar @.> Date: Friday, 24 February 2023 at 10:34 To: r-quantities/units @.> Cc: Keuskamp, J.A. (Joost) @.>, Author @.> Subject: Re: [r-quantities/units] support for scale_*_reverse() (Issue #343)

So you propose having scale_x_units_reverse, scale_x_units_log10 and so on?

— Reply to this email directly, view it on GitHubhttps://github.com/r-quantities/units/issues/343#issuecomment-1443345170, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAHKBHAEDY7DN3MIJT4RKWLWZB6D5ANCNFSM6AAAAAAVGVAEA4. You are receiving this because you authored the thread.Message ID: @.***>