trouble with unit arithmetics #789

ringprince opened this Issue Mar 26, 2013 · 2 comments


None yet
2 participants

Setting the margin using unit arithmetics fails



p <- ggplot(mpg, aes(displ, hwy)) + geom_point()

margin1 <-
  unit(c(0.5, 1, 0.5, 1),
       c("lines", "lines", "lines", "strwidth"),
       data=list(NULL, NULL, NULL, "uplrp"))

margin2 <-
  unit(c(0, 0, 0, 1),
       c("lines", "lines", "lines", "strwidth"),
       data=list(NULL, NULL, NULL, "1.00"))

margin3 <- margin1-margin2

margin3                                ## valid

p + theme(plot.margin=margin1)         ## works
p + theme(plot.margin=margin2)         ## works
p + theme(plot.margin=margin3)         ## does not work

I got an Email from Paul Murrel:

that is happening because add_theme() tries to do the following to margin3 (where 'y' is margin3) ...

        idx <- !vapply(y, is.null, logical(1))
        idx <- names(idx[idx])
        x[idx] <- y[idx]

... which is not a sensible thing to do with a unit arithmetic object. I would argue that this is a problem with add_theme() trying to do something very generic with the low-level structure of an object that it does not understand and should not be poking around in. That block of code should probably be a generic function, for which it may be necessary to write a 'unit' method.


hadley commented Feb 24, 2014

This sounds like a great feature/horrible bug, but unfortunately we don't currently have the development bandwidth to support it/fix it. If you'd like to submit a pull request that implements this feature/fixes this bug, please follow the instructions in the development vignette.

hadley closed this Feb 24, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment