Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add units to ggplot #8

Closed
mailund opened this issue Sep 29, 2016 · 15 comments
Closed

Add units to ggplot #8

mailund opened this issue Sep 29, 2016 · 15 comments

Comments

@mailund
Copy link

mailund commented Sep 29, 2016

Using the units package we can associate (physical) units to quantities and guarantee that we only do arithmetic on compatible units. Plotting data with associated units, however, is cumbersome in ggplot. Can we make ggplot unit-aware?

@thomasp85
Copy link
Owner

I will need some more context. What type of plot do you wish to be able to produce? Please provide some examples...

@mailund
Copy link
Author

mailund commented Sep 29, 2016

We have implemented unit awareness in the units package and added support for plotting them in basic plots but not with ggplot. @edzer wrote about it here: http://r-spatial.org/r/2016/09/29/plot_units.html

Essentially, we are just talking numbers with attributes that guarantee that units are converted when you multiply or divide them and that you cannot add or subtract units that can't be converted to each other. When plotting values with units we would love it if the labels would display the units together with the variables plotted.

@edzer
Copy link

edzer commented Oct 3, 2016

@thomasp85 what context do you need? We try to reproduce the last plot in the blog post @mailund mentions with the last command, as all information is in there. ggplot gives an error message (also in the blog) that indicates a comparison to a range, but ranges are also units:

> library(units)
> with(ud_units, 1:10 * km/h)
Units: km/h
 [1]  1  2  3  4  5  6  7  8  9 10
> x = with(ud_units, 1:10 * km/h)
> range(x)
Units: km/h
[1]  1 10

or maybe this is the problem because the other argument got converted to numeric? I don't know how to use debug to enter self$oob(x, limits), where the error is triggered. @hadley mentioned we need a scale type, and I can't find documentation or examples on how to create one.

@thomasp85
Copy link
Owner

It was nicely explained in the post above - I just haven't had time to respond as I've just started on a new job...

@edzer
Copy link

edzer commented Oct 21, 2016

Fully understandable. @mailund and I have no clue where to start with this. Maybe one of you or @hrbrmstr could give a hint?

@thomasp85
Copy link
Owner

I'll try to give it some thought this evening - stay put

@thomasp85
Copy link
Owner

I get

Warning in R_ut_init: Can't open installed, default, unit database

with both the CRAN and GitHub version on my mac every time I try to perform an operation with a unit vector...

Does that ring any bell to either of you?

@thomasp85
Copy link
Owner

Okay - I have looked into this a bit more. In terms of making sure that units are compatible: Do we agree that this is already handled since all data changes happens before being passed into ggplot? As I understand it we want to catch situations such as:

ggplot(data, aes(x = var1 + var2, y = var3))

And throw errors when var1 and var2 are not compatible... This should already work.

Now for the tough answer - we cannot make this automagical without a change to ggplot2 itself, meaning that if we want things to work after just adding e.g. geom_point() we'll need to convince @hadley to accept a change...

What we can do without any change to ggplot2 is to add a scale_[x/y]_[c/d]unit() that will automatically add the unit definition to the axis titles if the user defines the title in the constructor e.g.

ggplot(data, aes(x = var1 + var2, y = var3)) + 
  geom_point() + 
  scale_x_cunit("my axis title")

will create an x-axis title as you want... I do think we should convince @hadley to accept a change though and I'll be happy to discuss my idea with him. In essence it requires an additional method to the scale class that allows scales to modify the default label.

Please get back to me with your thoughts on this...

@thomasp85
Copy link
Owner

I've opened a PR in ggplot2 with the required changes - I recommend we wait until these are resolved...

@edzer
Copy link

edzer commented Oct 23, 2016

Thanks for addressing this Thomas! Great to see this moving.

Regarding the warning you see: it comes from udunits2; I wrote a PR to address it in July and brought it up again a few weeks ago but got no response. @jameshiebert: how do you suggest we solve this issue? It is clearly a warning where no warning should happen.

@thomasp85
Copy link
Owner

I think it is more than an ignorable warning - when running the examples for units() I get an error about m/s not convertible to km/h which hopefully isn't intended - can you reproduce this?

@edzer
Copy link

edzer commented Oct 23, 2016

Does it disappear when you do

library(udunits2)

first?

@thomasp85
Copy link
Owner

I'll check when I get home and get back to you

@thomasp85
Copy link
Owner

Loading udunits2 gives this message/warning

Warning in R_ut_init: Can't open installed, default, unit database
Failed to load udunits2 system database: reading shipped version from /Library/Frameworks/R.framework/Versions/3.3/Resources/library/udunits2/share/udunits2.xml

but removes the error when trying to do conversion...

@thomasp85
Copy link
Owner

I've just pushed a fork that adds rudimentary units support. Please check it out and comment there (#15)

thomasp85 added a commit that referenced this issue Nov 19, 2016
Fixes #8

* Add units continuous scales

* Add docs and fix small stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants