Skip to content

Cannot Use ggplot2 with units #164

@billdenney

Description

@billdenney

I just tried to plot data using ggplot2 with a units vector, and it yielded an error. Diving into the ggplot2 code, the issue comes down to range(NULL, units_value) returning a numeric vector which cannot be compared with a units class object. Specifically, this occurs in https://github.com/tidyverse/ggplot2/blob/8778b48b37d8b7e41c0f4f213031fb47810e70aa/R/range.r#L21-L25 which involves a call to scales::train_continuous which has either NULL or a numeric vector as the first argument to range.

I don't see a way to fix this without either converting the value I'm plotting to numeric (easy, but losing information and losing an opportunity to make a common pair of libraries more usable together) or converting the units library to an S4 class, but maybe you see a simpler way to do this?

library(units)                         
#> udunits system database from C:/Users/Bill Denney/Documents/R/win-library/3.4/units/share/udunits
library(ggplot2)                       
                                       
d <- data.frame(x=set_units(1:3, m),   
y=1:3)                                 
ggplot(d, aes(x=x, y=y)) + geom_point()
#> Don't know how to automatically pick scale for object of type units. Defaulting to continuous.
#> Error in Ops.units(x, range[1]): both operands of the expression should be "units" objects

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions