78

While trying to modify theme settings this simple code gives the following error:

library(ggplot2)
theme_nogrid <- theme_set(theme_update(  
                  plot.margin=unit(c(.25, .25, .25, .25), "in"),))

Error in do.call(theme, list(...)) : could not find function "unit"

R gives me this error for any element that uses 'unit'. Any other settings that do not call 'unit' work fine. I am running R v.2.15.2 (64-bit Windows).

I extensively searched online about this problem and found nothing. I appreciate any suggestions to the problem.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
opv
  • 881
  • 1
  • 6
  • 3
  • 27
    `require(grid)` – Gregor Thomas Feb 14 '13 at 22:22
  • 4
    ggplot2 now imports grid, as opposed to loading it – baptiste Feb 14 '13 at 22:25
  • 1
    Same thing happens with lattice. Somebody ought to right an answer so it can get checked. It deserves to be "on the record". – IRTFM Feb 14 '13 at 22:31
  • Same thing happened with 'methods' too have a look: http://stackoverflow.com/questions/30266732/error-in-initfieldsscales-scales-could-not-find-function-initreffields Is it the case that the current version of ggplot (I work with 1.0.1) is importing some packages instead of loading them? I believe a comprehensive answer by someone with a knowledge of the inners of gglot should be in place. – Costas B. May 17 '15 at 22:34

1 Answers1

72

This is closely related to, although not exactly identical to, arrow() in ggplot2 no longer supported , which says:

[the] grid [package] was loaded automatically by previous versions of ggplot[2] (making grid functions visible/accessible to the user); now it's referred to via NAMESPACE imports instead, so you need to explicitly load grid if you want to use grid functions (or [to] look at their help pages).

"explicitly load" here means library("grid") or require("grid") (grid is a base package, so doesn't need to be installed separately).

unit() is a function from the grid package, so the answer above (which was about arrow()) applies.

Alternatively you can specify grid::unit(...) or grid::arrow(...) without explicitly loading the entire package.

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Definitely closely related, something similar happened to me with 'methods' too. Take a look: http://stackoverflow.com/questions/30266732/error-in-initfieldsscales-scales-could-not-find-function-initreffields/30292677 – Costas B. May 17 '15 at 22:30